diff --git a/src/builtin/color/hsl.rs b/src/builtin/color/hsl.rs index a1da9d4..a0c2ee2 100644 --- a/src/builtin/color/hsl.rs +++ b/src/builtin/color/hsl.rs @@ -53,21 +53,21 @@ pub(crate) fn register(f: &mut BTreeMap) { max_args!(args, 1); match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.hue(), Unit::Deg)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "saturation", |args, _| { max_args!(args, 1); match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.saturation(), Unit::Percent)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "lightness", |args, _| { max_args!(args, 1); match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.lightness(), Unit::Percent)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "adjust-hue", |args, _| { @@ -157,8 +157,8 @@ pub(crate) fn register(f: &mut BTreeMap) { match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Color(c.invert(weight))), Value::Dimension(n, Unit::Percent) => Ok(Value::Ident(format!("invert({}%)", n), QuoteKind::None)), - Value::Dimension(..) => return Err("Only one argument may be passed to the plain-CSS invert() function.".into()), - v => return Err(format!("$color: {} is not a color.", v).into()), + Value::Dimension(..) => Err("Only one argument may be passed to the plain-CSS invert() function.".into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); } diff --git a/src/builtin/color/opacity.rs b/src/builtin/color/opacity.rs index b87520f..f469cd9 100644 --- a/src/builtin/color/opacity.rs +++ b/src/builtin/color/opacity.rs @@ -11,7 +11,7 @@ pub(crate) fn register(f: &mut BTreeMap) { max_args!(args, 1); match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "opacity", |args, _| { @@ -19,7 +19,7 @@ pub(crate) fn register(f: &mut BTreeMap) { match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), Value::Dimension(num, unit) => Ok(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "opacify", |args, _| { diff --git a/src/builtin/color/other.rs b/src/builtin/color/other.rs index 00febc7..2440239 100644 --- a/src/builtin/color/other.rs +++ b/src/builtin/color/other.rs @@ -67,7 +67,7 @@ pub(crate) fn register(f: &mut BTreeMap) { }); decl!(f "adjust-color", |args, _| { let color = match arg!(args, 0, "color").eval() { - Value::Color(c) => c.clone(), + Value::Color(c) => c, v => return Err(format!("$color: {} is not a color.", v).into()), }; @@ -121,7 +121,7 @@ pub(crate) fn register(f: &mut BTreeMap) { }); decl!(f "scale-color", |args, _| { let color = match arg!(args, 0, "color").eval() { - Value::Color(c) => c.clone(), + Value::Color(c) => c, v => return Err(format!("$color: {} is not a color.", v).into()), }; @@ -176,7 +176,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "ie-hex-str", |args, _| { max_args!(args, 1); let color = match arg!(args, 0, "color").eval() { - Value::Color(c) => c.clone(), + Value::Color(c) => c, v => return Err(format!("$color: {} is not a color.", v).into()), }; Ok(Value::Ident(color.to_ie_hex_str(), QuoteKind::None)) diff --git a/src/builtin/color/rgb.rs b/src/builtin/color/rgb.rs index 5715567..ebc7f37 100644 --- a/src/builtin/color/rgb.rs +++ b/src/builtin/color/rgb.rs @@ -158,21 +158,21 @@ pub(crate) fn register(f: &mut BTreeMap) { max_args!(args, 1); match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.red(), Unit::None)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "green", |args, _| { max_args!(args, 1); match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.green(), Unit::None)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "blue", |args, _| { max_args!(args, 1); match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.blue(), Unit::None)), - v => return Err(format!("$color: {} is not a color.", v).into()), + v => Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "mix", |args, _| { @@ -191,6 +191,6 @@ pub(crate) fn register(f: &mut BTreeMap) { Value::Dimension(n, u) => bound!("weight", n, u, 0, 100) / Number::from(100), v => return Err(format!("$weight: {} is not a number.", v).into()), }; - Ok(Value::Color(color1.mix(color2, weight))) + Ok(Value::Color(color1.mix(&color2, weight))) }); } diff --git a/src/color/mod.rs b/src/color/mod.rs index 9a088bc..0b8e128 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -67,7 +67,7 @@ struct Rgba { } impl Rgba { - pub fn new(red: Number, green: Number, blue: Number, alpha: Number) -> Self { + pub const fn new(red: Number, green: Number, blue: Number, alpha: Number) -> Self { Rgba { red, green, @@ -90,7 +90,7 @@ struct Hsla { } impl Hsla { - pub fn new(hue: Number, saturation: Number, luminance: Number, alpha: Number) -> Self { + pub const fn new(hue: Number, saturation: Number, luminance: Number, alpha: Number) -> Self { Hsla { hue, saturation, @@ -172,7 +172,7 @@ impl Color { /// Mix two colors together with weight /// Algorithm adapted from /// - pub fn mix(self, other: Color, weight: Number) -> Self { + pub fn mix(self, other: &Color, weight: Number) -> Self { let weight = clamp!(weight, 0, 100); let normalized_weight = weight.clone() * Number::from(2) - Number::from(1); let alpha_distance = self.alpha() - other.alpha(); @@ -432,7 +432,7 @@ impl Color { let blue = Number::from(u8::max_value()) - self.blue(); let repr = repr(&red, &green, &blue, &self.alpha()); let inverse = Color::new_rgba(red, green, blue, self.alpha(), repr); - inverse.mix(self.clone(), weight) + inverse.mix(self, weight) } pub fn complement(&self) -> Self { diff --git a/src/lib.rs b/src/lib.rs index aa0e34b..50cc82b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,8 @@ clippy::cast_possible_truncation, clippy::single_match_else, clippy::indexing_slicing, - clippy::match_same_arms + clippy::match_same_arms, + clippy::or_fun_call, )] #![cfg_attr(feature = "nightly", feature(track_caller))] // todo! handle erroring on styles at the toplevel @@ -213,7 +214,7 @@ enum Expr { /// A full selector `a > h1` Selector(Selector), /// A variable declaration `$var: 1px` - VariableDecl(String, Value), + VariableDecl(String, Box), /// A mixin declaration `@mixin foo {}` MixinDecl(String, Box), FunctionDecl(String, Box), @@ -472,10 +473,10 @@ impl<'a> StyleSheetParser<'a> { } Expr::VariableDecl(name, val) => { if self.scope == 0 { - scope.insert_var(&name, val.clone()); - self.global_scope.insert_var(&name, val); + scope.insert_var(&name, *val.clone()); + self.global_scope.insert_var(&name, *val); } else { - scope.insert_var(&name, val); + scope.insert_var(&name, *val); } } Expr::Include(rules) => stmts.extend(rules), @@ -559,7 +560,7 @@ pub(crate) fn eat_expr>( devour_whitespace(toks); let VariableDecl { val, default } = eat_variable_value(toks, scope)?; if !default || scope.get_var(&name).is_err() { - return Ok(Some(Expr::VariableDecl(name, val))); + return Ok(Some(Expr::VariableDecl(name, Box::new(val)))); } } else { values.push(Token { diff --git a/src/mixin.rs b/src/mixin.rs index 5e4633b..2accce0 100644 --- a/src/mixin.rs +++ b/src/mixin.rs @@ -111,7 +111,7 @@ impl Mixin { })); } Expr::VariableDecl(name, val) => { - self.scope.insert_var(&name, val); + self.scope.insert_var(&name, *val); } Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)), }