Skip to content

Commit

Permalink
Unpub internal trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj committed Mar 12, 2021
1 parent ce38cf1 commit c6a5621
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/sass/functions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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));
}}
}
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));
Expand Down
4 changes: 1 addition & 3 deletions src/sass/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ impl fmt::Debug for FuncImpl {
}
}

/// ...
pub trait Functions {
/// ...
trait Functions {
fn builtin_fn(
&mut self,
name: Name,
Expand Down
2 changes: 1 addition & 1 deletion src/sass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
82 changes: 40 additions & 42 deletions tests/rust_functions.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,51 +22,23 @@ fn simple_value() {
);
}

#[cfg(test)]
lazy_static! {
static ref TEST: Scope = {
fn get_number(s: &Scope, name: Name) -> Result<Numeric, Error> {
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 {
style: output::Style::Compressed,
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(
Expand All @@ -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,
Expand Down

0 comments on commit c6a5621

Please sign in to comment.