Error messages in builtins
This commit is contained in:
parent
7c451e333e
commit
1995b5ec6e
@ -22,7 +22,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
let alpha = match arg!(args, 3, "alpha"=Value::Dimension(Number::from(1), Unit::None)) {
|
let alpha = match arg!(args, 3, "alpha"=Value::Dimension(Number::from(1), Unit::None)) {
|
||||||
Value::Dimension(n, Unit::None) => n,
|
Value::Dimension(n, Unit::None) => n,
|
||||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||||
_ => todo!("non-number alpha given to builtin function `rgb()`")
|
v @ Value::Dimension(..) => return Err(format!("$alpha: Expected {} to have no units or \"%\".", v).into()),
|
||||||
|
v => return Err(format!("$alpha: {} is not a number.", v).into()),
|
||||||
};
|
};
|
||||||
Ok(Value::Color(Color::from_hsla(hue, saturation, luminance, alpha)))
|
Ok(Value::Color(Color::from_hsla(hue, saturation, luminance, alpha)))
|
||||||
});
|
});
|
||||||
@ -42,7 +43,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
let alpha = match arg!(args, 3, "alpha").eval() {
|
let alpha = match arg!(args, 3, "alpha").eval() {
|
||||||
Value::Dimension(n, Unit::None) => n,
|
Value::Dimension(n, Unit::None) => n,
|
||||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||||
_ => todo!("$alpha: Expected ____ to have no units or \"%\"."),
|
v @ Value::Dimension(..) => return Err(format!("$alpha: Expected {} to have no units or \"%\".", v).into()),
|
||||||
|
v => return Err(format!("$alpha: {} is not a number.", v).into()),
|
||||||
};
|
};
|
||||||
Ok(Value::Color(Color::from_hsla(hue, saturation, luminance, alpha)))
|
Ok(Value::Color(Color::from_hsla(hue, saturation, luminance, alpha)))
|
||||||
});
|
});
|
||||||
@ -70,9 +72,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
v => return Err(format!("$color: {} is not a color.", v).into()),
|
v => return Err(format!("$color: {} is not a color.", v).into()),
|
||||||
};
|
};
|
||||||
let degrees = match arg!(args, 1, "degrees").eval() {
|
let degrees = match arg!(args, 1, "degrees").eval() {
|
||||||
Value::Dimension(n, Unit::None)
|
Value::Dimension(n, _) => n,
|
||||||
| Value::Dimension(n, Unit::Percent)
|
|
||||||
| Value::Dimension(n, Unit::Deg) => n,
|
|
||||||
_ => todo!("expected either unitless or % number for degrees"),
|
_ => todo!("expected either unitless or % number for degrees"),
|
||||||
};
|
};
|
||||||
Ok(Value::Color(color.adjust_hue(degrees)))
|
Ok(Value::Color(color.adjust_hue(degrees)))
|
||||||
@ -83,9 +83,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
v => return Err(format!("$color: {} is not a color.", v).into()),
|
v => return Err(format!("$color: {} is not a color.", v).into()),
|
||||||
};
|
};
|
||||||
let amount = match arg!(args, 1, "amount").eval() {
|
let amount = match arg!(args, 1, "amount").eval() {
|
||||||
Value::Dimension(n, Unit::None) => n,
|
Value::Dimension(n, _) => n / Number::from(100),
|
||||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
v => return Err(format!("$amount: {} is not a number.", v).into())
|
||||||
_ => todo!("expected either unitless or % number for amount"),
|
|
||||||
};
|
};
|
||||||
Ok(Value::Color(color.lighten(amount)))
|
Ok(Value::Color(color.lighten(amount)))
|
||||||
});
|
});
|
||||||
@ -95,9 +94,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
v => return Err(format!("$color: {} is not a color.", v).into()),
|
v => return Err(format!("$color: {} is not a color.", v).into()),
|
||||||
};
|
};
|
||||||
let amount = match arg!(args, 1, "amount").eval() {
|
let amount = match arg!(args, 1, "amount").eval() {
|
||||||
Value::Dimension(n, Unit::None) => n,
|
Value::Dimension(n, _) => n / Number::from(100),
|
||||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
v => return Err(format!("$amount: {} is not a number.", v).into())
|
||||||
_ => todo!("expected either unitless or % number for amount"),
|
|
||||||
};
|
};
|
||||||
Ok(Value::Color(color.darken(amount)))
|
Ok(Value::Color(color.darken(amount)))
|
||||||
});
|
});
|
||||||
@ -119,7 +117,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
};
|
};
|
||||||
let amount = match arg!(args, 1, "amount").eval() {
|
let amount = match arg!(args, 1, "amount").eval() {
|
||||||
Value::Dimension(n, _) => n / Number::from(100),
|
Value::Dimension(n, _) => n / Number::from(100),
|
||||||
v => return Err(format!("$amount: {} is not a number.", v).into())
|
v => return Err(format!("$amount: {} is not a number.", v).into())
|
||||||
};
|
};
|
||||||
Ok(Value::Color(color.desaturate(amount)))
|
Ok(Value::Color(color.desaturate(amount)))
|
||||||
});
|
});
|
||||||
|
@ -122,7 +122,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
let alpha = match arg!(args, 1, "alpha").eval() {
|
let alpha = match arg!(args, 1, "alpha").eval() {
|
||||||
Value::Dimension(n, Unit::None) => n,
|
Value::Dimension(n, Unit::None) => n,
|
||||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||||
_ => todo!("expected either unitless or % number for alpha"),
|
v @ Value::Dimension(..) => return Err(format!("$alpha: Expected {} to have no units or \"%\".", v).into()),
|
||||||
|
v => return Err(format!("$alpha: {} is not a number.", v).into()),
|
||||||
};
|
};
|
||||||
Ok(Value::Color(color.with_alpha(alpha)))
|
Ok(Value::Color(color.with_alpha(alpha)))
|
||||||
} else {
|
} else {
|
||||||
@ -147,7 +148,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
let alpha = match arg!(args, 3, "alpha"=Value::Dimension(Number::from(1), Unit::None)).eval() {
|
let alpha = match arg!(args, 3, "alpha"=Value::Dimension(Number::from(1), Unit::None)).eval() {
|
||||||
Value::Dimension(n, Unit::None) => n,
|
Value::Dimension(n, Unit::None) => n,
|
||||||
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
|
||||||
_ => todo!("expected either unitless or % number for alpha")
|
v @ Value::Dimension(..) => return Err(format!("$alpha: Expected {} to have no units or \"%\".", v).into()),
|
||||||
|
v => return Err(format!("$alpha: {} is not a number.", v).into()),
|
||||||
};
|
};
|
||||||
Ok(Value::Color(Color::from_rgba(red, green, blue, alpha)))
|
Ok(Value::Color(Color::from_rgba(red, green, blue, alpha)))
|
||||||
}
|
}
|
||||||
|
@ -59,8 +59,8 @@ use std::path::Path;
|
|||||||
use crate::atrule::{AtRule, AtRuleKind};
|
use crate::atrule::{AtRule, AtRuleKind};
|
||||||
use crate::common::{Keyword, Op, Pos, Scope, Symbol, Whitespace};
|
use crate::common::{Keyword, Op, Pos, Scope, Symbol, Whitespace};
|
||||||
use crate::css::Css;
|
use crate::css::Css;
|
||||||
pub use crate::error::SassResult;
|
|
||||||
use crate::error::SassError;
|
use crate::error::SassError;
|
||||||
|
pub use crate::error::SassResult;
|
||||||
use crate::format::PrettyPrinter;
|
use crate::format::PrettyPrinter;
|
||||||
use crate::function::Function;
|
use crate::function::Function;
|
||||||
use crate::imports::import;
|
use crate::imports::import;
|
||||||
|
@ -69,7 +69,9 @@ fn main() {
|
|||||||
eprintln!("{}", b);
|
eprintln!("{}", b);
|
||||||
std::process::exit(1)
|
std::process::exit(1)
|
||||||
}
|
}
|
||||||
}.print_as_css(&mut stdout).unwrap();
|
}
|
||||||
|
.print_as_css(&mut stdout)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
35
src/units.rs
35
src/units.rs
@ -117,13 +117,9 @@ impl Unit {
|
|||||||
|
|
||||||
pub fn kind(&self) -> UnitKind {
|
pub fn kind(&self) -> UnitKind {
|
||||||
match self {
|
match self {
|
||||||
Unit::Px
|
Unit::Px | Unit::Mm | Unit::In | Unit::Cm | Unit::Q | Unit::Pt | Unit::Pc => {
|
||||||
| Unit::Mm
|
UnitKind::Absolute
|
||||||
| Unit::In
|
}
|
||||||
| Unit::Cm
|
|
||||||
| Unit::Q
|
|
||||||
| Unit::Pt
|
|
||||||
| Unit::Pc => UnitKind::Absolute,
|
|
||||||
Unit::Em
|
Unit::Em
|
||||||
| Unit::Rem
|
| Unit::Rem
|
||||||
| Unit::Lh
|
| Unit::Lh
|
||||||
@ -132,24 +128,13 @@ impl Unit {
|
|||||||
| Unit::Cap
|
| Unit::Cap
|
||||||
| Unit::Ic
|
| Unit::Ic
|
||||||
| Unit::Rlh => UnitKind::FontRelative,
|
| Unit::Rlh => UnitKind::FontRelative,
|
||||||
Unit::Vw
|
Unit::Vw | Unit::Vh | Unit::Vmin | Unit::Vmax | Unit::Vi | Unit::Vb => {
|
||||||
| Unit::Vh
|
UnitKind::ViewportRelative
|
||||||
| Unit::Vmin
|
}
|
||||||
| Unit::Vmax
|
Unit::Deg | Unit::Grad | Unit::Rad | Unit::Turn => UnitKind::Angle,
|
||||||
| Unit::Vi
|
Unit::S | Unit::Ms => UnitKind::Time,
|
||||||
| Unit::Vb => UnitKind::ViewportRelative,
|
Unit::Hz | Unit::Khz => UnitKind::Frequency,
|
||||||
Unit::Deg
|
Unit::Dpi | Unit::Dpcm | Unit::Dppx | Unit::X => UnitKind::Resolution,
|
||||||
| Unit::Grad
|
|
||||||
| Unit::Rad
|
|
||||||
| Unit::Turn => UnitKind::Angle,
|
|
||||||
Unit::S
|
|
||||||
| Unit::Ms => UnitKind::Time,
|
|
||||||
Unit::Hz
|
|
||||||
| Unit::Khz => UnitKind::Frequency,
|
|
||||||
Unit::Dpi
|
|
||||||
| Unit::Dpcm
|
|
||||||
| Unit::Dppx
|
|
||||||
| Unit::X => UnitKind::Resolution,
|
|
||||||
Unit::None => UnitKind::None,
|
Unit::None => UnitKind::None,
|
||||||
_ => UnitKind::Other,
|
_ => UnitKind::Other,
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use std::fmt::{self, Display, Write};
|
use std::fmt::{self, Display, Write};
|
||||||
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign, Neg};
|
use std::ops::{
|
||||||
|
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
|
||||||
|
};
|
||||||
|
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use num_rational::BigRational;
|
use num_rational::BigRational;
|
||||||
@ -201,8 +203,6 @@ impl Neg for Number {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn neg(self) -> Self {
|
fn neg(self) -> Self {
|
||||||
Number {
|
Number { val: -self.val }
|
||||||
val: -self.val,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,9 +298,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
Ok(Value::Ident(s, QuoteKind::Single))
|
Ok(Value::Ident(s, QuoteKind::Single))
|
||||||
}
|
}
|
||||||
TokenKind::Variable(ref v) => {
|
TokenKind::Variable(ref v) => Ok(scope.get_var(v).expect("expected variable").clone()),
|
||||||
Ok(scope.get_var(v).expect("expected variable").clone())
|
|
||||||
}
|
|
||||||
TokenKind::Interpolation => {
|
TokenKind::Interpolation => {
|
||||||
let mut s = parse_interpolation(toks, scope)
|
let mut s = parse_interpolation(toks, scope)
|
||||||
.iter()
|
.iter()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user