Change KeywordValue to enum

This commit is contained in:
Nazım Can Altınova 2016-10-23 02:38:36 +03:00
parent 769129f5c2
commit 6dc2c36310
2 changed files with 48 additions and 49 deletions

View file

@ -750,14 +750,14 @@ ${helpers.single_keyword("text-align-last",
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum T { pub enum T {
Keyword(Keyword), Keyword(KeywordValue),
None, None,
String(String), String(String),
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Keyword { pub struct KeywordValue {
pub fill: bool, pub fill: bool,
pub shape: super::ShapeKeyword, pub shape: super::ShapeKeyword,
} }
@ -766,7 +766,7 @@ ${helpers.single_keyword("text-align-last",
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum SpecifiedValue { pub enum SpecifiedValue {
Keyword(Keyword), Keyword(KeywordValue),
None, None,
String(String), String(String),
} }
@ -792,24 +792,23 @@ ${helpers.single_keyword("text-align-last",
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Keyword { pub enum KeywordValue {
pub fill: Option<bool>, Fill(bool),
pub shape: Option<ShapeKeyword>, Shape(ShapeKeyword),
FillAndShape(bool, ShapeKeyword),
} }
impl ToCss for Keyword { impl ToCss for KeywordValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let mut has_fill = false; if let Some(fill) = self.fill() {
if let Some(fill) = self.fill {
has_fill = true;
if fill { if fill {
try!(dest.write_str("filled")); try!(dest.write_str("filled"));
} else { } else {
try!(dest.write_str("open")); try!(dest.write_str("open"));
} }
} }
if let Some(shape) = self.shape { if let Some(shape) = self.shape() {
if has_fill { if self.fill().is_some() {
try!(dest.write_str(" ")); try!(dest.write_str(" "));
} }
try!(shape.to_css(dest)); try!(shape.to_css(dest));
@ -817,7 +816,7 @@ ${helpers.single_keyword("text-align-last",
Ok(()) Ok(())
} }
} }
impl ToCss for computed_value::Keyword { impl ToCss for computed_value::KeywordValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if self.fill { if self.fill {
try!(dest.write_str("filled")); try!(dest.write_str("filled"));
@ -829,6 +828,23 @@ ${helpers.single_keyword("text-align-last",
} }
} }
impl KeywordValue {
fn fill(&self) -> Option<bool> {
match *self {
KeywordValue::Fill(fill) |
KeywordValue::FillAndShape(fill,_) => Some(fill),
_ => None,
}
}
fn shape(&self) -> Option<ShapeKeyword> {
match *self {
KeywordValue::Shape(shape) |
KeywordValue::FillAndShape(_, shape) => Some(shape),
_ => None,
}
}
}
define_css_keyword_enum!(ShapeKeyword: define_css_keyword_enum!(ShapeKeyword:
"dot" => Dot, "dot" => Dot,
"circle" => Circle, "circle" => Circle,
@ -866,16 +882,14 @@ ${helpers.single_keyword("text-align-last",
} else { } else {
ShapeKeyword::Sesame ShapeKeyword::Sesame
}; };
computed_value::T::Keyword(computed_value::Keyword { computed_value::T::Keyword(computed_value::KeywordValue {
fill: keyword.fill.unwrap_or(true), fill: keyword.fill().unwrap_or(true),
shape: keyword.shape.unwrap_or(default_shape) shape: keyword.shape().unwrap_or(default_shape),
}) })
}, },
SpecifiedValue::None => computed_value::T::None, SpecifiedValue::None => computed_value::T::None,
SpecifiedValue::String(ref s) => { SpecifiedValue::String(ref s) => {
let string = if s.len() > 1 { let string = s.chars().next().as_ref().map(ToString::to_string).unwrap_or_default();
s.chars().nth(0).unwrap().to_string()
} else { s.clone() };
computed_value::T::String(string) computed_value::T::String(string)
} }
} }
@ -883,10 +897,8 @@ ${helpers.single_keyword("text-align-last",
#[inline] #[inline]
fn from_computed_value(computed: &computed_value::T) -> Self { fn from_computed_value(computed: &computed_value::T) -> Self {
match *computed { match *computed {
computed_value::T::Keyword(ref keyword) => SpecifiedValue::Keyword(Keyword { computed_value::T::Keyword(ref keyword) =>
fill: Some(keyword.fill), SpecifiedValue::Keyword(KeywordValue::FillAndShape(keyword.fill,keyword.shape)),
shape: Some(keyword.shape)
}),
computed_value::T::None => SpecifiedValue::None, computed_value::T::None => SpecifiedValue::None,
computed_value::T::String(ref string) => SpecifiedValue::String(string.clone()) computed_value::T::String(ref string) => SpecifiedValue::String(string.clone())
} }
@ -910,19 +922,18 @@ ${helpers.single_keyword("text-align-last",
} else if input.try(|input| input.expect_ident_matching("open")).is_ok() { } else if input.try(|input| input.expect_ident_matching("open")).is_ok() {
Some(false) Some(false)
} else { None }; } else { None };
if let Err(_) = shape { if shape.is_err() {
shape = input.try(ShapeKeyword::parse); shape = input.try(ShapeKeyword::parse);
} }
// At least one of shape or fill must be handled // At least one of shape or fill must be handled
if let (None, Err(_)) = (fill, shape) { let keyword_value = match (fill, shape) {
Err(()) (Some(fill), Ok(shape)) => KeywordValue::FillAndShape(fill,shape),
} else { (Some(fill), Err(_)) => KeywordValue::Fill(fill),
Ok(SpecifiedValue::Keyword(Keyword { (None, Ok(shape)) => KeywordValue::Shape(shape),
fill: fill, _ => return Err(()),
shape: shape.ok() };
})) Ok(SpecifiedValue::Keyword(keyword_value))
}
} }
</%helpers:longhand> </%helpers:longhand>

View file

@ -11,37 +11,25 @@ use url::Url;
#[test] #[test]
fn text_emphasis_style_longhand_should_parse_properly() { fn text_emphasis_style_longhand_should_parse_properly() {
use style::properties::longhands::text_emphasis_style; use style::properties::longhands::text_emphasis_style;
use style::properties::longhands::text_emphasis_style::{ShapeKeyword, SpecifiedValue, Keyword}; use style::properties::longhands::text_emphasis_style::{ShapeKeyword, SpecifiedValue, KeywordValue};
let none = parse_longhand!(text_emphasis_style, "none"); let none = parse_longhand!(text_emphasis_style, "none");
assert_eq!(none, SpecifiedValue::None); assert_eq!(none, SpecifiedValue::None);
let fill = parse_longhand!(text_emphasis_style, "open"); let fill = parse_longhand!(text_emphasis_style, "open");
let fill_struct = SpecifiedValue::Keyword(Keyword { let fill_struct = SpecifiedValue::Keyword(KeywordValue::Fill(false));
fill: Some(false),
shape: None
});
assert_eq!(fill, fill_struct); assert_eq!(fill, fill_struct);
let shape = parse_longhand!(text_emphasis_style, "triangle"); let shape = parse_longhand!(text_emphasis_style, "triangle");
let shape_struct = SpecifiedValue::Keyword(Keyword { let shape_struct = SpecifiedValue::Keyword(KeywordValue::Shape(ShapeKeyword::Triangle));
fill: None,
shape: Some(ShapeKeyword::Triangle)
});
assert_eq!(shape, shape_struct); assert_eq!(shape, shape_struct);
let fill_shape = parse_longhand!(text_emphasis_style, "filled dot"); let fill_shape = parse_longhand!(text_emphasis_style, "filled dot");
let fill_shape_struct = SpecifiedValue::Keyword(Keyword { let fill_shape_struct = SpecifiedValue::Keyword(KeywordValue::FillAndShape(true, ShapeKeyword::Dot));
fill: Some(true),
shape: Some(ShapeKeyword::Dot)
});
assert_eq!(fill_shape, fill_shape_struct); assert_eq!(fill_shape, fill_shape_struct);
let shape_fill = parse_longhand!(text_emphasis_style, "dot filled"); let shape_fill = parse_longhand!(text_emphasis_style, "dot filled");
let shape_fill_struct = SpecifiedValue::Keyword(Keyword { let shape_fill_struct = SpecifiedValue::Keyword(KeywordValue::FillAndShape(true, ShapeKeyword::Dot));
fill: Some(true),
shape: Some(ShapeKeyword::Dot)
});
assert_eq!(shape_fill, shape_fill_struct); assert_eq!(shape_fill, shape_fill_struct);
let a_string = parse_longhand!(text_emphasis_style, "\"a\""); let a_string = parse_longhand!(text_emphasis_style, "\"a\"");