From ffff80109bc81ac15271e6871d1456d7d571738f Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 2 Feb 2020 11:17:23 -0500 Subject: [PATCH] Implement ident addition --- src/lib.rs | 2 +- src/value.rs | 33 +++++++++++++- tests/values.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bfa07b0..0afb454 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -454,7 +454,7 @@ impl<'a> StyleSheetParser<'a> { match expr { Expr::Style(s) => stmts.push(Stmt::Style(s)), #[allow(clippy::redundant_closure)] - Expr::Styles(s) => stmts.extend(s.into_iter().map(|s| Stmt::Style(s))), + Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)), Expr::MixinDecl(name, mixin) => { scope.mixins.insert(name, *mixin); } diff --git a/src/value.rs b/src/value.rs index a3aa67b..0d433af 100644 --- a/src/value.rs +++ b/src/value.rs @@ -95,7 +95,33 @@ impl Add for Value { // Self::Color(..) => todo!(), // Self::BinaryOp(..) => todo!(), // Self::Paren(..) => todo!(), - // Self::Ident(..) => todo!(), + Self::Ident(s1, quotes1) => match other { + Self::Ident(s2, quotes2) => { + let quotes = match (quotes1, quotes2) { + (QuoteKind::Double, _) + | (QuoteKind::Single, _) + | (_, QuoteKind::Double) + | (_, QuoteKind::Single) => QuoteKind::Double, + _ => QuoteKind::None, + }; + 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) + } + _ => todo!(), + }, _ => todo!(), } } @@ -115,7 +141,10 @@ impl Display for Value { .join(sep.as_str()) ), Self::Color(c) => write!(f, "{}", c), - Self::BinaryOp(lhs, op, rhs) => write!(f, "{}{}{}", lhs, op, rhs), + Self::BinaryOp(lhs, op, rhs) => match op { + Op::Plus => write!(f, "{}", *lhs.clone() + *rhs.clone()), + _ => write!(f, "{}{}{}", lhs, op, rhs), + }, Self::Paren(val) => write!(f, "{}", val), Self::Ident(val, kind) => write!(f, "{}{}{}", kind.as_str(), val, kind.as_str()), Self::True => write!(f, "true"), diff --git a/tests/values.rs b/tests/values.rs index ae2800b..c60ea4d 100644 --- a/tests/values.rs +++ b/tests/values.rs @@ -49,3 +49,118 @@ test!( "a {\n color: 1 (red blue);\n}\n", "a {\n color: 1 red blue;\n}\n" ); +test!( + adds_idents, + "a {\n color: red + blue;\n}\n", + "a {\n color: redblue;\n}\n" +); +test!( + adds_dbl_quoted_idents, + "a {\n color: \"red\" + \"blue\";\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_sgl_quoted_idents, + "a {\n color: 'red' + 'blue';\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_dbl_and_un_quoted_idents, + "a {\n color: \"red\" + blue;\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_sgl_and_un_quoted_idents, + "a {\n color: 'red' + blue;\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_un_and_dbl_quoted_idents, + "a {\n color: red + \"blue\";\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_un_and_sgl_quoted_idents, + "a {\n color: red + 'blue';\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_sgl_and_dbl_quoted_idents, + "a {\n color: 'red' + \"blue\";\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_dbl_and_sgl_quoted_idents, + "a {\n color: \"red\" + 'blue';\n}\n", + "a {\n color: \"redblue\";\n}\n" +); +test!( + adds_ident_true, + "a {\n color: red + true;\n}\n", + "a {\n color: redtrue;\n}\n" +); +test!( + adds_dbl_quoted_ident_true, + "a {\n color: \"red\" + true;\n}\n", + "a {\n color: \"redtrue\";\n}\n" +); +test!( + adds_ident_false, + "a {\n color: red + false;\n}\n", + "a {\n color: redfalse;\n}\n" +); +test!( + adds_dbl_quoted_ident_false, + "a {\n color: \"red\" + false;\n}\n", + "a {\n color: \"redfalse\";\n}\n" +); +test!( + adds_ident_important, + "a {\n color: red + !important;\n}\n", + "a {\n color: red!important;\n}\n" +); +test!( + adds_ident_null, + "a {\n color: red + null;\n}\n", + "a {\n color: red;\n}\n" +); +test!( + adds_dbl_quoted_ident_null, + "a {\n color: \"red\" + null;\n}\n", + "a {\n color: \"red\";\n}\n" +); +test!( + adds_sgl_quoted_ident_null, + "a {\n color: 'red' + null;\n}\n", + "a {\n color: \"red\";\n}\n" +); +test!( + adds_ident_number, + "a {\n color: red + 1;\n}\n", + "a {\n color: red1;\n}\n" +); +test!( + adds_dbl_quoted_ident_number, + "a {\n color: \"red\" + 1;\n}\n", + "a {\n color: \"red1\";\n}\n" +); +test!( + adds_sgl_quoted_ident_number, + "a {\n color: 'red' + 1;\n}\n", + "a {\n color: \"red1\";\n}\n" +); +test!( + adds_ident_dimension, + "a {\n color: red + 1px;\n}\n", + "a {\n color: red1px;\n}\n" +); +test!( + adds_dbl_quoted_ident_dimension, + "a {\n color: \"red\" + 1px;\n}\n", + "a {\n color: \"red1px\";\n}\n" +); +test!( + adds_sgl_quoted_ident_dimension, + "a {\n color: 'red' + 1px;\n}\n", + "a {\n color: \"red1px\";\n}\n" +);