Value::to_css_string returns a Cow<'static, str>

This commit is contained in:
ConnorSkees 2020-05-05 11:08:34 -04:00
parent 96e916e750
commit 26df276266
6 changed files with 35 additions and 32 deletions

View File

@ -88,7 +88,7 @@ impl CallArgs {
.iter() .iter()
.map(|a| { .map(|a| {
span = span.merge(a.span); span = span.merge(a.span);
Ok(a.node.to_css_string(a.span)?) Ok(a.node.to_css_string(a.span)?.into())
}) })
.collect::<SassResult<Vec<String>>>()? .collect::<SassResult<Vec<String>>>()?
.join(", "), .join(", "),

View File

@ -89,7 +89,7 @@ impl AtRule {
devour_whitespace(toks); devour_whitespace(toks);
Spanned { Spanned {
node: AtRule::Warn(Spanned { node: AtRule::Warn(Spanned {
node: message.to_css_string(span)?, node: message.to_css_string(span)?.into(),
span, span,
}), }),
span, span,

View File

@ -204,7 +204,7 @@ pub(crate) fn eat_ident<I: Iterator<Item = Token>>(
text.push_str( text.push_str(
&match parse_interpolation(toks, scope, super_selector)?.node { &match parse_interpolation(toks, scope, super_selector)?.node {
Value::Ident(s, ..) => s, Value::Ident(s, ..) => s,
v => v.to_css_string(span)?, v => v.to_css_string(span)?.into(),
}, },
); );
} else { } else {
@ -279,7 +279,7 @@ pub(crate) fn parse_quoted_string<I: Iterator<Item = Token>>(
let interpolation = parse_interpolation(toks, scope, super_selector)?; let interpolation = parse_interpolation(toks, scope, super_selector)?;
s.push_str(&match interpolation.node { s.push_str(&match interpolation.node {
Value::Ident(s, ..) => s, Value::Ident(s, ..) => s,
v => v.to_css_string(interpolation.span)?, v => v.to_css_string(interpolation.span)?.into(),
}); });
continue; continue;
} else { } else {

View File

@ -112,7 +112,7 @@ pub(crate) fn try_eat_url<I: Iterator<Item = Token>>(
peek_counter += count; peek_counter += count;
buf.push_str(&match interpolation.node { buf.push_str(&match interpolation.node {
Value::Ident(s, ..) => s, Value::Ident(s, ..) => s,
v => v.to_css_string(interpolation.span)?, v => v.to_css_string(interpolation.span)?.into(),
}); });
} else { } else {
buf.push('#'); buf.push('#');

View File

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::iter::Iterator; use std::iter::Iterator;
@ -121,14 +122,14 @@ impl Value {
}) })
} }
pub fn to_css_string(&self, span: Span) -> SassResult<String> { pub fn to_css_string(&self, span: Span) -> SassResult<Cow<'static, str>> {
Ok(match self { Ok(match self {
Self::Important => "!important".to_string(), Self::Important => Cow::Borrowed("!important"),
Self::Dimension(num, unit) => match unit { Self::Dimension(num, unit) => match unit {
Unit::Mul(..) => { Unit::Mul(..) => {
return Err((format!("{}{} isn't a valid CSS value.", num, unit), span).into()); return Err((format!("{}{} isn't a valid CSS value.", num, unit), span).into());
} }
_ => format!("{}{}", num, unit), _ => Cow::Owned(format!("{}{}", num, unit)),
}, },
Self::Map(..) => { Self::Map(..) => {
return Err(( return Err((
@ -145,22 +146,23 @@ impl Value {
.into()) .into())
} }
Self::List(vals, sep, brackets) => match brackets { Self::List(vals, sep, brackets) => match brackets {
Brackets::None => vals Brackets::None => Cow::Owned(
.iter() vals.iter()
.filter(|x| !x.is_null(span).unwrap()) .filter(|x| !x.is_null(span).unwrap())
.map(|x| x.to_css_string(span)) .map(|x| x.to_css_string(span))
.collect::<SassResult<Vec<String>>>()? .collect::<SassResult<Vec<Cow<'static, str>>>>()?
.join(sep.as_str()), .join(sep.as_str()),
Brackets::Bracketed => format!( ),
Brackets::Bracketed => Cow::Owned(format!(
"[{}]", "[{}]",
vals.iter() vals.iter()
.filter(|x| !x.is_null(span).unwrap()) .filter(|x| !x.is_null(span).unwrap())
.map(|x| x.to_css_string(span)) .map(|x| x.to_css_string(span))
.collect::<SassResult<Vec<String>>>()? .collect::<SassResult<Vec<Cow<'static, str>>>>()?
.join(sep.as_str()), .join(sep.as_str()),
), )),
}, },
Self::Color(c) => format!("{}", c), Self::Color(c) => Cow::Owned(c.to_string()),
Self::UnaryOp(..) | Self::BinaryOp(..) => { Self::UnaryOp(..) | Self::BinaryOp(..) => {
self.clone().eval(span)?.to_css_string(span)? self.clone().eval(span)?.to_css_string(span)?
} }
@ -185,22 +187,23 @@ impl Value {
} }
} }
} }
buf Cow::Owned(buf)
} }
Self::Ident(string, QuoteKind::Quoted) => { Self::Ident(string, QuoteKind::Quoted) => {
let mut buf = String::with_capacity(string.len()); let mut buf = String::with_capacity(string.len());
visit_quoted_string(&mut buf, false, string)?; visit_quoted_string(&mut buf, false, string)?;
buf Cow::Owned(buf)
} }
Self::True => "true".to_string(), Self::True => Cow::Borrowed("true"),
Self::False => "false".to_string(), Self::False => Cow::Borrowed("false"),
Self::Null => String::new(), Self::Null => Cow::Borrowed(""),
Self::ArgList(args) => args Self::ArgList(args) => Cow::Owned(
.iter() args.iter()
.filter(|x| !x.is_null(span).unwrap()) .filter(|x| !x.is_null(span).unwrap())
.map(|a| Ok(a.node.to_css_string(span)?)) .map(|a| Ok(a.node.to_css_string(span)?.into()))
.collect::<SassResult<Vec<String>>>()? .collect::<SassResult<Vec<String>>>()?
.join(", "), .join(", "),
),
}) })
} }
@ -306,7 +309,7 @@ impl Value {
.join(", ") .join(", ")
), ),
Value::Paren(v) => v.inspect(span)?, Value::Paren(v) => v.inspect(span)?,
v => v.to_css_string(span)?, v => v.to_css_string(span)?.into(),
}) })
} }

View File

@ -20,7 +20,7 @@ impl Value {
format!("{}{}", self.to_css_string(span)?, s), format!("{}{}", self.to_css_string(span)?, s),
QuoteKind::Quoted, QuoteKind::Quoted,
), ),
Self::Null => Value::Ident(self.to_css_string(span)?, QuoteKind::None), Self::Null => Value::Ident(self.to_css_string(span)?.into(), QuoteKind::None),
_ => Value::Ident( _ => Value::Ident(
format!( format!(
"{}{}", "{}{}",
@ -32,7 +32,7 @@ impl Value {
}, },
Self::Null => match other { Self::Null => match other {
Self::Null => Self::Null, Self::Null => Self::Null,
_ => Value::Ident(other.to_css_string(span)?, QuoteKind::None), _ => Value::Ident(other.to_css_string(span)?.into(), QuoteKind::None),
}, },
Self::Dimension(num, unit) => match other { Self::Dimension(num, unit) => match other {
Self::Dimension(num2, unit2) => { Self::Dimension(num2, unit2) => {