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()
.map(|a| {
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>>>()?
.join(", "),

View File

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

View File

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

View File

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

View File

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