Skip to content

Commit

Permalink
Use a NumOrSpecial type to improve css functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj committed May 3, 2024
1 parent c314ba7 commit d28d685
Show file tree
Hide file tree
Showing 34 changed files with 766 additions and 752 deletions.
8 changes: 8 additions & 0 deletions rsass/src/css/call_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ impl CallArgs {
}
}

pub(crate) fn from_iter<T, I>(positional: I) -> Self
where
T: Into<Value>,
I: IntoIterator<Item = T>,
{
Self::from_list(positional.into_iter().map(Into::into).collect())
}

pub(crate) fn add_from_value_map(
&mut self,
map: OrderMap<Value, Value>,
Expand Down
2 changes: 1 addition & 1 deletion rsass/src/css/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ pub use self::value::{InvalidCss, Value, ValueMap, ValueToMapError};
pub(crate) use self::selectors::{
CssSelectorSet, LogicalSelector, SelectorCtx,
};
pub(crate) use self::util::{is_calc_name, is_function_name, is_not};
pub(crate) use self::util::{is_calc_name, is_function_name, is_not, IsNot};
7 changes: 7 additions & 0 deletions rsass/src/css/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ impl Value {
Value::Numeric(Numeric::scalar(v), true)
}

pub(crate) fn call<T: Into<Value>, I>(name: &str, args: I) -> Value
where
I: IntoIterator<Item = T>,
{
Value::Call(name.into(), CallArgs::from_iter(args))
}

/// Check that the value is valid in css.
pub fn valid_css(self) -> Result<Self, InvalidCss> {
match self {
Expand Down
12 changes: 8 additions & 4 deletions rsass/src/sass/functions/call_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ impl CallError {
}

/// The values were expected to be compatible, but wasn't.
pub fn incompatible_values(a: &Value, b: &Value) -> Self {
pub fn incompatible_values<T: Into<Value>>(a: T, b: T) -> Self {
Self::msg(format!(
"{} and {} are incompatible.",
a.format(Format::introspect()),
b.format(Format::introspect()),
a.into().format(Format::introspect()),
b.into().format(Format::introspect()),
))
}

Expand All @@ -46,7 +46,11 @@ impl CallError {
Error::BadCall(format!("{err:?}"), call_pos.clone(), None)
}
CallError::BadArgument(name, problem) => Error::BadCall(
format!("${name}: {problem}"),
if name.as_ref().is_empty() {
problem
} else {
format!("${name}: {problem}")
},
call_pos.clone(),
None,
),
Expand Down
7 changes: 1 addition & 6 deletions rsass/src/sass/functions/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,7 @@ fn num2chan(v: &Numeric) -> Result<Rational, String> {
}

fn make_call(name: &str, args: Vec<Value>) -> Value {
Value::Call(
name.into(),
CallArgs::from_list(
args.into_iter().filter(|v| v != &Value::Null).collect(),
),
)
Value::call(name, args.into_iter().filter(|v| v != &Value::Null))
}

pub(crate) fn eval_inner(
Expand Down
6 changes: 3 additions & 3 deletions rsass/src/sass/functions/color/other.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
check_alpha_pm, check_alpha_range, check_channel_pm, check_channel_range,
check_expl_pct, check_hue, expected_to, make_call, CallError, CheckedArg,
check_expl_pct, check_hue, expected_to, CallError, CheckedArg,
FunctionMap, Name,
};
use crate::css::{CallArgs, Value};
Expand Down Expand Up @@ -136,12 +136,12 @@ pub fn register(f: &mut Scope) {

def!(f, opacity(color), |s| match s.get(name!(color))? {
Value::Color(ref col, _) => Ok(Value::scalar(col.get_alpha())),
v => Ok(make_call("opacity", vec![v])),
v => Ok(Value::call("opacity", [v])),
});
def!(f, alpha(color), |s| {
let v = s.get(name!(color))?;
if ok_as_filterarg(&v) {
Ok(make_call("alpha", vec![v]))
Ok(Value::call("alpha", [v]))
} else {
let color = Color::try_from(v).named(name!(color))?;
Ok(Value::scalar(color.get_alpha()))
Expand Down
14 changes: 6 additions & 8 deletions rsass/src/sass/functions/color/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn register(f: &mut Scope) {
if w == one() {
match col {
v @ Value::Numeric(..) => {
Ok(make_call("invert", vec![v]))
Ok(Value::call("invert", [v]))
}
v => Err(is_not(&v, "a color")).named(name!(color)),
}
Expand Down Expand Up @@ -101,11 +101,9 @@ pub fn expose(m: &Scope, global: &mut FunctionMap) {
if w == one() {
match col {
v @ Value::Numeric(..) => {
Ok(make_call("invert", vec![v]))
}
v if is_special(&v) => {
Ok(make_call("invert", vec![v]))
Ok(Value::call("invert", [v]))
}
v if is_special(&v) => Ok(Value::call("invert", [v])),
v => Err(is_not(&v, "a color")).named(name!(color)),
}
} else {
Expand Down Expand Up @@ -172,17 +170,17 @@ fn do_rgba(fn_name: &Name, s: &ResolvedArgs) -> Result<Value, CallError> {
if is_special(&c) || is_special(&a) {
if let Ok(c) = Color::try_from(c.clone()) {
let c = c.to_rgba();
Ok(make_call(
Ok(Value::call(
fn_name.as_ref(),
vec![
[
Value::scalar(c.red()),
Value::scalar(c.green()),
Value::scalar(c.blue()),
a,
],
))
} else {
Ok(make_call(fn_name.as_ref(), vec![c, a]))
Ok(Value::call(fn_name.as_ref(), [c, a]))
}
} else {
let mut c = Color::try_from(c).named(name!(color))?;
Expand Down
4 changes: 2 additions & 2 deletions rsass/src/sass/functions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn create_module() -> Scope {
Ok(sep.into())
});
def_va!(f, slash(elements), |s| {
let list = s.get_map(name!(elements), check::va_list)?;
let list = s.get_va(name!(elements))?;
if list.len() < 2 {
return Err(CallError::msg(
"At least two elements are required.",
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn create_module() -> Scope {
});
def_va!(f, zip(lists), |s| {
let lists = s
.get_map(name!(lists), check::va_list)?
.get_va(name!(lists))?
.into_iter()
.map(Value::iter_items)
.collect::<Vec<_>>();
Expand Down
Loading

0 comments on commit d28d685

Please sign in to comment.