replace std::borrow::Cow with beef::Cow

This commit is contained in:
ConnorSkees 2020-05-25 13:09:20 -04:00
parent 6d76e1518a
commit bb53aaab8a
9 changed files with 55 additions and 47 deletions

View File

@ -49,6 +49,7 @@ rand = { version = "0.7.3", optional = true }
codemap = "0.1.3"
peekmore = "0.4.0"
wasm-bindgen = { version = "0.2.60", optional = true }
beef = "0.4.4"
[features]
default = ["commandline", "random"]

View File

@ -5,6 +5,7 @@ use codemap::{Span, Spanned};
use peekmore::PeekMoreIterator;
use crate::Cow;
use crate::common::Identifier;
use crate::error::SassResult;
use crate::scope::Scope;
@ -90,9 +91,9 @@ impl CallArgs {
.iter()
.map(|a| {
span = span.merge(a.span);
Ok(a.node.to_css_string(a.span)?.into())
Ok(a.node.to_css_string(a.span)?)
})
.collect::<SassResult<Vec<String>>>()?
.collect::<SassResult<Vec<Cow<'static, str>>>>()?
.join(", "),
);
string.push(')');

View File

@ -10,7 +10,7 @@ use crate::utils::{
read_until_semicolon_or_closing_curly_brace,
};
use crate::value::Value;
use crate::{RuleSet, Stmt, Token};
use crate::{Cow, RuleSet, Stmt, Token};
use each_rule::{parse_each, Each};
use for_rule::For;
@ -36,8 +36,8 @@ mod while_rule;
#[derive(Debug, Clone)]
pub(crate) enum AtRule {
Warn(Spanned<String>),
Debug(Spanned<String>),
Warn(Spanned<Cow<'static, str>>),
Debug(Spanned<Cow<'static, str>>),
Mixin(String, Box<Mixin>),
Function(String, Box<Function>),
Return(Vec<Token>),
@ -94,7 +94,7 @@ impl AtRule {
devour_whitespace(toks);
Spanned {
node: AtRule::Warn(Spanned {
node: message.to_css_string(span)?.into(),
node: message.to_css_string(span)?,
span,
}),
span,
@ -117,7 +117,7 @@ impl AtRule {
devour_whitespace(toks);
Spanned {
node: AtRule::Debug(Spanned {
node: message.inspect(span)?.into(),
node: message.inspect(span)?,
span,
}),
span,

View File

@ -98,7 +98,7 @@ fn inspect(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> Sass
Ok(Value::String(
arg!(args, scope, super_selector, 0, "value")
.inspect(args.span())?
.into(),
.into_owned(),
QuoteKind::None,
))
}

View File

@ -84,6 +84,11 @@ grass input.scss
use std::convert::TryFrom;
use std::iter::Iterator;
#[cfg(target_pointer_width = "64")]
pub(crate) use beef::lean::Cow;
#[cfg(not(target_pointer_width = "64"))]
pub(crate) use beef::Cow;
use codemap::{Span, Spanned};
use peekmore::{PeekMore, PeekMoreIterator};

View File

@ -1,4 +1,5 @@
use std::iter::Iterator;
use std::borrow::Borrow;
use codemap::{Span, Spanned};
@ -216,12 +217,10 @@ pub(crate) fn eat_ident<I: Iterator<Item = Token>>(
};
if kind == '{' {
toks.next();
text.push_str(
&match parse_interpolation(toks, scope, super_selector, pos)?.node {
Value::String(s, ..) => s,
v => v.to_css_string(span)?.into(),
},
);
match parse_interpolation(toks, scope, super_selector, pos)?.node {
Value::String(ref s, ..) => text.push_str(s),
v => text.push_str(v.to_css_string(span)?.borrow()),
}
} else {
return Err(("Expected identifier.", pos).into());
}
@ -300,10 +299,10 @@ pub(crate) fn parse_quoted_string<I: Iterator<Item = Token>>(
if let Some(Token { kind: '{', pos }) = toks.peek().cloned() {
toks.next();
let interpolation = parse_interpolation(toks, scope, super_selector, pos)?;
s.push_str(&match interpolation.node {
Value::String(s, ..) => s,
v => v.to_css_string(interpolation.span)?.into(),
});
match interpolation.node {
Value::String(ref v, ..) => s.push_str(v),
v => s.push_str(v.to_css_string(interpolation.span)?.borrow()),
};
continue;
} else {
s.push('#');

View File

@ -1,3 +1,5 @@
use std::borrow::Borrow;
use codemap::{Span, Spanned};
use peekmore::PeekMoreIterator;
@ -115,10 +117,10 @@ pub(crate) fn try_eat_url<I: Iterator<Item = Token>>(
peek_counter += 1;
let (interpolation, count) = peek_interpolation(toks, scope, super_selector, pos)?;
peek_counter += count;
buf.push_str(&match interpolation.node {
Value::String(s, ..) => s,
v => v.to_css_string(interpolation.span)?.into(),
});
match interpolation.node {
Value::String(ref s, ..) => buf.push_str(s),
v => buf.push_str(v.to_css_string(interpolation.span)?.borrow()),
};
} else {
buf.push('#');
}

View File

@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::iter::Iterator;
use codemap::{Span, Spanned};
@ -8,6 +7,7 @@ use crate::common::{Brackets, ListSeparator, Op, QuoteKind};
use crate::error::SassResult;
use crate::unit::Unit;
use crate::utils::hex_char_for;
use crate::Cow;
use css_function::is_special_function;
pub(crate) use map::SassMap;
@ -128,12 +128,12 @@ impl Value {
pub fn to_css_string(&self, span: Span) -> SassResult<Cow<'static, str>> {
Ok(match self {
Self::Important => Cow::Borrowed("!important"),
Self::Important => Cow::const_str("!important"),
Self::Dimension(num, unit) => match unit {
Unit::Mul(..) => {
return Err((format!("{}{} isn't a valid CSS value.", num, unit), span).into());
}
_ => Cow::Owned(format!("{}{}", num, unit)),
_ => Cow::owned(format!("{}{}", num, unit)),
},
Self::Map(..) | Self::Function(..) => {
return Err((
@ -143,14 +143,14 @@ impl Value {
.into())
}
Self::List(vals, sep, brackets) => match brackets {
Brackets::None => Cow::Owned(
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!(
Brackets::Bracketed => Cow::owned(format!(
"[{}]",
vals.iter()
.filter(|x| !x.is_null(span).unwrap())
@ -159,7 +159,7 @@ impl Value {
.join(sep.as_str()),
)),
},
Self::Color(c) => Cow::Owned(c.to_string()),
Self::Color(c) => Cow::owned(c.to_string()),
Self::UnaryOp(..) | Self::BinaryOp(..) => {
self.clone().eval(span)?.to_css_string(span)?
}
@ -184,21 +184,21 @@ impl Value {
}
}
}
Cow::Owned(buf)
Cow::owned(buf)
}
Self::String(string, QuoteKind::Quoted) => {
let mut buf = String::with_capacity(string.len());
visit_quoted_string(&mut buf, false, string)?;
Cow::Owned(buf)
Cow::owned(buf)
}
Self::True => Cow::Borrowed("true"),
Self::False => Cow::Borrowed("false"),
Self::Null => Cow::Borrowed(""),
Self::ArgList(args) => Cow::Owned(
Self::True => Cow::const_str("true"),
Self::False => Cow::const_str("false"),
Self::Null => Cow::const_str(""),
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>>>()?
.map(|a| Ok(a.node.to_css_string(span)?))
.collect::<SassResult<Vec<Cow<'static, str>>>>()?
.join(", "),
),
})
@ -265,20 +265,20 @@ impl Value {
pub fn inspect(&self, span: Span) -> SassResult<Cow<'static, str>> {
Ok(match self {
Value::List(v, _, brackets) if v.is_empty() => match brackets {
Brackets::None => Cow::Borrowed("()"),
Brackets::Bracketed => Cow::Borrowed("[]"),
Brackets::None => Cow::const_str("()"),
Brackets::Bracketed => Cow::const_str("[]"),
},
Value::List(v, sep, brackets) if v.len() == 1 => match brackets {
Brackets::None => match sep {
ListSeparator::Space => v[0].inspect(span)?,
ListSeparator::Comma => Cow::Owned(format!("({},)", v[0].inspect(span)?)),
ListSeparator::Comma => Cow::owned(format!("({},)", v[0].inspect(span)?)),
},
Brackets::Bracketed => match sep {
ListSeparator::Space => Cow::Owned(format!("[{}]", v[0].inspect(span)?)),
ListSeparator::Comma => Cow::Owned(format!("[{},]", v[0].inspect(span)?)),
ListSeparator::Space => Cow::owned(format!("[{}]", v[0].inspect(span)?)),
ListSeparator::Comma => Cow::owned(format!("[{},]", v[0].inspect(span)?)),
},
},
Self::List(vals, sep, brackets) => Cow::Owned(match brackets {
Self::List(vals, sep, brackets) => Cow::owned(match brackets {
Brackets::None => vals
.iter()
.map(|x| x.inspect(span))
@ -292,9 +292,9 @@ impl Value {
.join(sep.as_str()),
),
}),
Value::Function(f) => Cow::Owned(format!("get-function(\"{}\")", f.name())),
Value::Null => Cow::Borrowed("null"),
Value::Map(map) => Cow::Owned(format!(
Value::Function(f) => Cow::owned(format!("get-function(\"{}\")", f.name())),
Value::Null => Cow::const_str("null"),
Value::Map(map) => Cow::owned(format!(
"({})",
map.iter()
.map(|(k, v)| Ok(format!(

View File

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