From 2b3d8fcce7a966e54103aba8a9487a54d6763c41 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 9 Feb 2020 10:49:37 -0500 Subject: [PATCH] Implement addition between colors and idents --- src/builtin/color.rs | 1 - src/value/ops.rs | 16 +++++++++++++++- tests/color.rs | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/builtin/color.rs b/src/builtin/color.rs index 1922c35..39d335f 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color.rs @@ -1,5 +1,4 @@ use std::collections::BTreeMap; -use std::convert::TryInto; use num_bigint::BigInt; diff --git a/src/value/ops.rs b/src/value/ops.rs index 9b0fd06..28795ae 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -24,7 +24,14 @@ impl Add for Value { _ => todo!(), }, // Self::List(..) => todo!(), - // Self::Color(..) => todo!(), + Self::Color(c) => match other { + Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => { + Value::Ident(format!("{}{}", c, s), QuoteKind::Double) + } + Self::Null => Value::Ident(c.to_string(), QuoteKind::None), + Self::Color(..) => todo!("figure out if it's possible to add colors"), + _ => Value::Ident(format!("{}{}", c, other), QuoteKind::None), + } // Self::BinaryOp(..) => todo!(), // Self::Paren(..) => todo!(), Self::Ident(s1, quotes1) => match other { @@ -52,6 +59,13 @@ impl Add for Value { }; 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) + } _ => todo!(), }, _ => todo!(), diff --git a/tests/color.rs b/tests/color.rs index 214eb86..bbfd8a8 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -116,3 +116,23 @@ test!( "a {\n color: hsla($hue: 193, $saturation: 67%, $luminance: 99, $alpha: .6);\n}\n", "a {\n color: rgba(251, 253, 254, 0.6);\n}\n" ); +test!( + color_plus_ident, + "a {\n color: red + foo;\n}\n", + "a {\n color: redfoo;\n}\n" +); +test!( + ident_plus_color, + "a {\n color: foo + red;\n}\n", + "a {\n color: foored;\n}\n" +); +test!( + color_minus_ident, + "a {\n color: red - foo;\n}\n", + "a {\n color: red-foo;\n}\n" +); +test!( + ident_minus_color, + "a {\n color: foo - red;\n}\n", + "a {\n color: foo-red;\n}\n" +);