diff --git a/src/sass/functions/macros.rs b/src/sass/functions/macros.rs index 19cb71390..9df14b470 100644 --- a/src/sass/functions/macros.rs +++ b/src/sass/functions/macros.rs @@ -23,7 +23,7 @@ macro_rules! one_arg { macro_rules! def { ($f:expr, $name:ident( $($arg:ident$(=$val:expr)* ),* ), $body:expr) => {{ - use crate::sass::{Functions, FormalArgs}; + use crate::sass::functions::{Functions, FormalArgs}; use std::sync::Arc; let args = FormalArgs::new(vec![ $(one_arg!($arg $(=$val)*)),* ]); $f.builtin_fn(name!($name), args, Arc::new($body)); @@ -31,7 +31,7 @@ macro_rules! def { } macro_rules! def_va { ($f:expr, $name:ident( $($arg:ident$(=$val:expr)* ),*), $body:expr) => {{ - use crate::sass::{Functions, FormalArgs}; + use crate::sass::functions::{Functions, FormalArgs}; use std::sync::Arc; let args = FormalArgs::new_va(vec![ $(one_arg!($arg $(=$val)*)),* ]); $f.builtin_fn(name!($name), args, Arc::new($body)); diff --git a/src/sass/functions/mod.rs b/src/sass/functions/mod.rs index 0bcda419e..a2528523e 100644 --- a/src/sass/functions/mod.rs +++ b/src/sass/functions/mod.rs @@ -89,9 +89,7 @@ impl fmt::Debug for FuncImpl { } } -/// ... -pub trait Functions { - /// ... +trait Functions { fn builtin_fn( &mut self, name: Name, diff --git a/src/sass/mod.rs b/src/sass/mod.rs index c272446b0..07aacad67 100644 --- a/src/sass/mod.rs +++ b/src/sass/mod.rs @@ -10,7 +10,7 @@ mod value; pub use self::call_args::CallArgs; pub use self::formal_args::{ArgsError, FormalArgs}; -pub use self::functions::{get_global_module, Function, Functions}; +pub use self::functions::{get_global_module, Function}; pub use self::item::{Item, UseAs}; pub use self::mixin::Mixin; pub use self::name::Name; diff --git a/tests/rust_functions.rs b/tests/rust_functions.rs index 213f1b7ff..52b1305d3 100644 --- a/tests/rust_functions.rs +++ b/tests/rust_functions.rs @@ -1,5 +1,4 @@ -use lazy_static::lazy_static; -use rsass::sass::{FormalArgs, Functions, Name}; +use rsass::sass::{Function, FormalArgs, Name}; use rsass::value::{Number, Numeric, Rgba}; use rsass::*; use std::sync::Arc; @@ -23,42 +22,6 @@ fn simple_value() { ); } -#[cfg(test)] -lazy_static! { - static ref TEST: Scope = { - fn get_number(s: &Scope, name: Name) -> Result { - s.get(name.as_ref())? - .numeric_value() - .map_err(|v| Error::bad_arg(name, &v, "is not a number")) - } - let mut scope = Scope::builtin_module("test"); - scope.builtin_fn( - Name::from_static("get_answer"), - FormalArgs::none(), - Arc::new(|_| Ok(css::Value::scalar(42))), - ); - scope.builtin_fn( - Name::from_static("halfway"), - FormalArgs::new(vec![ - ("a".into(), None), - ("b".into(), Some(sass::Value::scalar(0))), - ]), - Arc::new(|s| { - let a = get_number(s, "a".into())?; - let b = get_number(s, "b".into())?; - if a.unit == b.unit || b.unit.is_none() { - Ok(Numeric::new(avg(a.value, b.value), a.unit).into()) - } else if a.unit.is_none() { - Ok(Numeric::new(avg(a.value, b.value), b.unit).into()) - } else { - Err(Error::S("Incopatible args".into())) - } - }), - ); - scope - }; -} - #[test] fn simple_function() { let format = output::Format { @@ -66,8 +29,16 @@ fn simple_function() { precision: 5, }; let scope = ScopeRef::new_global(format); - scope.define_module(Name::from_static("test"), ScopeRef::Builtin(&TEST)); - let parsed = parse_scss_data(b"p { x: test.get_answer(); }").unwrap(); + scope.define_function( + Name::from_static("get_answer"), + Function::builtin( + &Name::from_static(""), + &Name::from_static("get_answer"), + FormalArgs::none(), + Arc::new(|_| Ok(css::Value::scalar(42))), + ), + ); + let parsed = parse_scss_data(b"p { x: get_answer(); }").unwrap(); let file_context = FsFileContext::new(); assert_eq!( String::from_utf8( @@ -86,8 +57,35 @@ fn avg(a: Number, b: Number) -> Number { #[test] fn function_with_args() { let scope = ScopeRef::new_global(Default::default()); - scope.define_module(Name::from_static("test"), ScopeRef::Builtin(&TEST)); - let parsed = parse_scss_data(b"p { x: test.halfway(10, 18); }").unwrap(); + scope.define_function( + Name::from_static("halfway"), + Function::builtin( + &Name::from_static(""), + &Name::from_static("halfway"), + FormalArgs::new(vec![ + ("a".into(), None), + ("b".into(), Some(sass::Value::scalar(0))), + ]), + Arc::new(|s| { + let a = s + .get("a")? + .numeric_value() + .map_err(|v| Error::bad_arg("a".into(), &v, "is not a number"))?; + let b = s + .get("b")? + .numeric_value() + .map_err(|v| Error::bad_arg("b".into(), &v, "is not a number"))?; + if a.unit == b.unit || b.unit.is_none() { + Ok(Numeric::new(avg(a.value, b.value), a.unit).into()) + } else if a.unit.is_none() { + Ok(Numeric::new(avg(a.value, b.value), b.unit).into()) + } else { + Err(Error::error("Incopatible args")) + } + }), + ), + ); + let parsed = parse_scss_data(b"p { x: halfway(10, 18); }").unwrap(); let format = output::Format { style: output::Style::Compressed, precision: 5,