From 83a0b8bfbee62825c1786d1c97fa57ae8b6427cf Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Thu, 25 Aug 2022 19:50:53 +0200 Subject: [PATCH] Prepare for rsass 0.26. * MRSV is now 1.56.1. * The implementation of the "static_name" sass function is now much shorter and nicer. --- .github/workflows/ci.yml | 5 ++-- Cargo.toml | 2 +- src/lib.rs | 12 ++++----- src/staticfiles.rs | 58 ++++++++++++++++------------------------ 4 files changed, 32 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4b435e8..5d4c953 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,9 +12,8 @@ jobs: matrix: rust: - stable - - 1.52.1 - - 1.48.0 - - 1.46.0 + - 1.60.0 + - 1.56.1 - beta - nightly steps: diff --git a/Cargo.toml b/Cargo.toml index a5d3e05..540303c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ itertools = "0.10.0" md5 = "0.7" nom = "7.1.0" -rsass = { version = "0.25.0", optional = true } +rsass = { git = "https://github.com/kaj/rsass", optional = true } mime = { version = "0.3", optional = true } [badges] diff --git a/src/lib.rs b/src/lib.rs index a8338ff..aae1c40 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -455,20 +455,20 @@ impl Error for RucteError { } impl Display for RucteError { + fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { + write!(out, "Error: {:?}", self) + } +} +impl Debug for RucteError { fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { match self { RucteError::Io(err) => Display::fmt(err, out), RucteError::Env(var, err) => write!(out, "{:?}: {}", var, err), #[cfg(feature = "sass")] - RucteError::Sass(err) => Display::fmt(err, out), + RucteError::Sass(err) => Debug::fmt(err, out), } } } -impl Debug for RucteError { - fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { - Display::fmt(self, out) - } -} impl From for RucteError { fn from(e: io::Error) -> RucteError { diff --git a/src/staticfiles.rs b/src/staticfiles.rs index a25cd7b..7379d7f 100644 --- a/src/staticfiles.rs +++ b/src/staticfiles.rs @@ -467,10 +467,10 @@ impl StaticFile { where P: AsRef, { - let src = self.path_for(src); use rsass::css::CssString; + use rsass::input::CargoContext; use rsass::output::{Format, Style}; - use rsass::sass::FormalArgs; + use rsass::sass::{CallError, FormalArgs}; use rsass::value::Quotes; use rsass::*; use std::sync::Arc; @@ -478,49 +478,37 @@ impl StaticFile { style: Style::Compressed, precision: 4, }; - let scope = ScopeRef::new_global(format); - - // TODO Find any referenced files! - println!("cargo:rerun-if-changed={}", src.display()); - let existing_statics = Arc::new(self.get_names().clone()); - scope.define_function( + let src = self.path_for(src); + let (context, scss) = + CargoContext::for_path(&src).map_err(rsass::Error::from)?; + let mut context = context.with_format(format); + let existing_statics = self.get_names().clone(); + context.get_scope().define_function( "static_name".into(), sass::Function::builtin( "", &"static_name".into(), FormalArgs::new(vec![("name".into(), None)]), - Arc::new(move |s| match s.get(&"name".into())? { - css::Value::Literal(name) => { - let name = - name.value().replace('-', "_").replace('.', "_"); - for (n, v) in existing_statics.as_ref() { - if name == *n { - return Ok(CssString::new( - v.into(), - Quotes::Double, - ) - .into()); - } - } - Err(Error::S(format!( - "Static file {} not found", - name, - ))) - } - name => Err(Error::BadArgument( - "name".into(), - format!( - "{} is not a string", - name.format(Format::introspect()) - ), - )), + Arc::new(move |s| { + let name: String = s.get("name".into())?; + let rname = name.replace('-', "_").replace('.', "_"); + existing_statics + .iter() + .find(|(n, _v)| *n == &rname) + .map(|(_n, v)| { + CssString::new(v.into(), Quotes::Double).into() + }) + .ok_or_else(|| { + CallError::msg(format!( + "Static file {name:?} not found", + )) + }) }), ), ); - let (file_context, scss) = FsFileContext::for_path(&src)?; - let css = format.write_root(scss.parse()?, scope, &file_context)?; + let css = context.transform(scss)?; self.add_file_data(&src.with_extension("css"), &css) }