Error messages in builtins

This commit is contained in:
ConnorSkees 2020-02-16 11:59:04 -05:00
parent 7c451e333e
commit 1995b5ec6e
7 changed files with 34 additions and 49 deletions

View File

@ -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)) {
Value::Dimension(n, Unit::None) => n,
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)))
});
@ -42,7 +43,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
let alpha = match arg!(args, 3, "alpha").eval() {
Value::Dimension(n, Unit::None) => n,
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)))
});
@ -70,9 +72,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
v => return Err(format!("$color: {} is not a color.", v).into()),
};
let degrees = match arg!(args, 1, "degrees").eval() {
Value::Dimension(n, Unit::None)
| Value::Dimension(n, Unit::Percent)
| Value::Dimension(n, Unit::Deg) => n,
Value::Dimension(n, _) => n,
_ => todo!("expected either unitless or % number for 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()),
};
let amount = match arg!(args, 1, "amount").eval() {
Value::Dimension(n, Unit::None) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
_ => todo!("expected either unitless or % number for amount"),
Value::Dimension(n, _) => n / Number::from(100),
v => return Err(format!("$amount: {} is not a number.", v).into())
};
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()),
};
let amount = match arg!(args, 1, "amount").eval() {
Value::Dimension(n, Unit::None) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
_ => todo!("expected either unitless or % number for amount"),
Value::Dimension(n, _) => n / Number::from(100),
v => return Err(format!("$amount: {} is not a number.", v).into())
};
Ok(Value::Color(color.darken(amount)))
});

View File

@ -122,7 +122,8 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
let alpha = match arg!(args, 1, "alpha").eval() {
Value::Dimension(n, Unit::None) => n,
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)))
} 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() {
Value::Dimension(n, Unit::None) => n,
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)))
}

View File

@ -59,8 +59,8 @@ use std::path::Path;
use crate::atrule::{AtRule, AtRuleKind};
use crate::common::{Keyword, Op, Pos, Scope, Symbol, Whitespace};
use crate::css::Css;
pub use crate::error::SassResult;
use crate::error::SassError;
pub use crate::error::SassResult;
use crate::format::PrettyPrinter;
use crate::function::Function;
use crate::imports::import;

View File

@ -69,7 +69,9 @@ fn main() {
eprintln!("{}", b);
std::process::exit(1)
}
}.print_as_css(&mut stdout).unwrap();
}
.print_as_css(&mut stdout)
.unwrap();
}
}
}

View File

@ -117,13 +117,9 @@ impl Unit {
pub fn kind(&self) -> UnitKind {
match self {
Unit::Px
| Unit::Mm
| Unit::In
| Unit::Cm
| Unit::Q
| Unit::Pt
| Unit::Pc => UnitKind::Absolute,
Unit::Px | Unit::Mm | Unit::In | Unit::Cm | Unit::Q | Unit::Pt | Unit::Pc => {
UnitKind::Absolute
}
Unit::Em
| Unit::Rem
| Unit::Lh
@ -132,24 +128,13 @@ impl Unit {
| Unit::Cap
| Unit::Ic
| Unit::Rlh => UnitKind::FontRelative,
Unit::Vw
| Unit::Vh
| Unit::Vmin
| Unit::Vmax
| Unit::Vi
| Unit::Vb => UnitKind::ViewportRelative,
Unit::Deg
| 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::Vw | Unit::Vh | Unit::Vmin | Unit::Vmax | Unit::Vi | Unit::Vb => {
UnitKind::ViewportRelative
}
Unit::Deg | 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,
_ => UnitKind::Other,
}

View File

@ -1,6 +1,8 @@
use std::convert::From;
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_rational::BigRational;
@ -201,8 +203,6 @@ impl Neg for Number {
type Output = Self;
fn neg(self) -> Self {
Number {
val: -self.val,
}
Number { val: -self.val }
}
}

View File

@ -298,9 +298,7 @@ impl Value {
}
Ok(Value::Ident(s, QuoteKind::Single))
}
TokenKind::Variable(ref v) => {
Ok(scope.get_var(v).expect("expected variable").clone())
}
TokenKind::Variable(ref v) => Ok(scope.get_var(v).expect("expected variable").clone()),
TokenKind::Interpolation => {
let mut s = parse_interpolation(toks, scope)
.iter()