refactor quote normalization

This commit is contained in:
ConnorSkees 2020-03-23 12:12:08 -04:00
parent 6460c97ef4
commit 98a99684b7
2 changed files with 40 additions and 121 deletions

View File

@ -361,6 +361,17 @@ pub(crate) enum QuoteKind {
None,
}
impl QuoteKind {
/// SASS will prefer double quotes over single quotes after
/// operations, e.g. `'foo' + red` => `"foored"`
pub fn normalize(self) -> QuoteKind {
match self {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
}
}
}
impl Display for QuoteKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View File

@ -42,13 +42,7 @@ impl Add for Value {
)
}
}
Self::Ident(s, q) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}{}", num, unit, s), quotes)
}
Self::Ident(s, q) => Value::Ident(format!("{}{}{}", num, unit, s), q.normalize()),
Self::Null => Value::Ident(format!("{}{}", num, unit), QuoteKind::None),
Self::List(..) => {
Value::Ident(format!("{}{}{}", num, unit, other), QuoteKind::None)
@ -60,12 +54,7 @@ impl Add for Value {
}
},
Self::Color(c) => match other {
Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => {
Value::Ident(format!("{}{}", c, s), QuoteKind::Double)
}
Self::Ident(s, QuoteKind::None) => {
Value::Ident(format!("{}{}", c, s), QuoteKind::None)
}
Self::Ident(s, q) => Value::Ident(format!("{}{}", c, s), q.normalize()),
Self::Null => Value::Ident(c.to_string(), QuoteKind::None),
Self::List(..) => Value::Ident(format!("{}{}", c, other), QuoteKind::None),
_ => return Err(format!("Undefined operation \"{} + {}\".", c, other).into()),
@ -83,37 +72,15 @@ impl Add for Value {
Value::Ident(format!("{}{}", s1, s2), quotes)
}
Self::Important | Self::True | Self::False | Self::Dimension(..) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}", s1, other), quotes)
}
Self::Null => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(s1, quotes)
}
Self::Color(c) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}", s1, c), quotes)
Value::Ident(format!("{}{}", s1, other), quotes1.normalize())
}
Self::Null => Value::Ident(s1, quotes1.normalize()),
Self::Color(c) => Value::Ident(format!("{}{}", s1, c), quotes1.normalize()),
Self::List(..) => Value::Ident(format!("{}{}", s1, other), quotes1),
Self::UnaryOp(..) | Self::BinaryOp(..) | Self::Paren(..) => todo!(),
},
Self::List(..) => match other {
Self::Ident(s, q) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}", self, s), quotes)
}
Self::Ident(s, q) => Value::Ident(format!("{}{}", self, s), q.normalize()),
Self::Paren(..) => (self + other.eval()?)?,
_ => Value::Ident(format!("{}{}", self, other), QuoteKind::None),
},
@ -154,13 +121,10 @@ impl Sub for Value {
_ => todo!(),
},
Self::Color(c) => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}-{}{}{}", c, quotes, s, quotes), QuoteKind::None)
}
Self::Ident(s, q) => Value::Ident(
format!("{}-{}{}{}", c, q.normalize(), s, q.normalize()),
QuoteKind::None,
),
Self::Null => Value::Ident(format!("{}-", c), QuoteKind::None),
Self::Dimension(..) | Self::Color(..) => {
return Err(format!("Undefined operation \"{} - {}\".", c, other).into())
@ -168,18 +132,10 @@ impl Sub for Value {
_ => Value::Ident(format!("{}-{}", c, other), QuoteKind::None),
},
Self::BinaryOp(..) | Self::Paren(..) => (self.eval()? - other)?,
Self::Ident(s1, quotes1) => match other {
Self::Ident(s2, quotes2) => {
let quotes1 = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
let quotes2 = match quotes2 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Self::Ident(s1, q1) => match other {
Self::Ident(s2, q2) => {
Value::Ident(
format!("{}{}{}-{}{}{}", quotes1, s1, quotes1, quotes2, s2, quotes2),
format!("{}{}{}-{}{}{}", q1.normalize(), s1, q1.normalize(), q2.normalize(), s2, q2.normalize()),
QuoteKind::None,
)
}
@ -188,29 +144,17 @@ impl Sub for Value {
| Self::False
| Self::Dimension(..)
| Self::Color(..) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}{}-{}", quotes, s1, quotes, other),
format!("{}{}{}-{}", q1.normalize(), s1, q1.normalize(), other),
QuoteKind::None,
)
}
Self::Null => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}{}-", quotes, s1, quotes), QuoteKind::None)
Value::Ident(format!("{}{}{}-", q1.normalize(), s1, q1.normalize()), QuoteKind::None)
}
Self::List(..) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}{}-{}", quotes, s1, quotes, other),
format!("{}{}{}-{}", q1.normalize(), s1, q1.normalize(), other),
QuoteKind::None,
)
}
@ -218,12 +162,8 @@ impl Sub for Value {
},
Self::List(..) => match other {
Self::Ident(s, q) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}-{}{}{}", self, quotes, s, quotes),
format!("{}-{}{}{}", self, q.normalize(), s, q.normalize()),
QuoteKind::None,
)
}
@ -231,13 +171,9 @@ impl Sub for Value {
_ => Value::Ident(format!("{}-{}", self, other), QuoteKind::None),
},
_ => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Self::Ident(s, q) => {
Value::Ident(
format!("{}-{}{}{}", self, quotes, s, quotes),
format!("{}-{}{}{}", self, q.normalize(), s, q.normalize()),
QuoteKind::None,
)
}
@ -299,12 +235,8 @@ impl Div for Value {
}
}
Self::Ident(s, q) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}/{}{}{}", num, unit, quotes, s, quotes),
format!("{}{}/{}{}{}", num, unit, q.normalize(), s, q.normalize()),
QuoteKind::None,
)
}
@ -314,12 +246,8 @@ impl Div for Value {
_ => todo!(),
},
Self::Color(c) => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}/{}{}{}", c, quotes, s, quotes), QuoteKind::None)
Self::Ident(s, q) => {
Value::Ident(format!("{}/{}{}{}", c, q.normalize(), s, q.normalize()), QuoteKind::None)
}
Self::Null => Value::Ident(format!("{}/", c), QuoteKind::None),
Self::Dimension(..) | Self::Color(..) => {
@ -328,18 +256,10 @@ impl Div for Value {
_ => Value::Ident(format!("{}/{}", c, other), QuoteKind::None),
},
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
Self::Ident(s1, quotes1) => match other {
Self::Ident(s2, quotes2) => {
let quotes1 = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
let quotes2 = match quotes2 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Self::Ident(s1, q1) => match other {
Self::Ident(s2, q2) => {
Value::Ident(
format!("{}{}{}/{}{}{}", quotes1, s1, quotes1, quotes2, s2, quotes2),
format!("{}{}{}/{}{}{}", q1.normalize(), s1, q1.normalize(), q2.normalize(), s2, q2.normalize()),
QuoteKind::None,
)
}
@ -348,32 +268,20 @@ impl Div for Value {
| Self::False
| Self::Dimension(..)
| Self::Color(..) => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(
format!("{}{}{}/{}", quotes, s1, quotes, other),
format!("{}{}{}/{}", q1.normalize(), s1, q1.normalize(), other),
QuoteKind::None,
)
}
Self::Null => {
let quotes = match quotes1 {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Value::Ident(format!("{}{}{}/", quotes, s1, quotes), QuoteKind::None)
Value::Ident(format!("{}{}{}/", q1.normalize(), s1, q1.normalize()), QuoteKind::None)
}
_ => todo!(),
},
_ => match other {
Self::Ident(s, quotes) => {
let quotes = match quotes {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
};
Self::Ident(s, q) => {
Value::Ident(
format!("{}/{}{}{}", self, quotes, s, quotes),
format!("{}/{}{}{}", self, q.normalize(), s, q.normalize()),
QuoteKind::None,
)
}