style/properties_and_values/syntax/
data_type.rs1use super::{Component, ComponentName, Multiplier};
8use std::fmt::{self, Debug, Write};
9use style_traits::{CssWriter, ToCss};
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, MallocSizeOf, ToShmem)]
13pub struct DependentDataTypes(u8);
14bitflags! {
15 impl DependentDataTypes: u8 {
16 const LENGTH = 1 << 0;
18 const COLOR= 1 << 1;
20 }
21}
22
23#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)]
25pub enum DataType {
26 Length,
28 Number,
30 Percentage,
32 LengthPercentage,
35 Color,
37 Image,
39 Url,
41 Integer,
43 Angle,
45 Time,
47 Resolution,
49 TransformFunction,
51 CustomIdent,
53 TransformList,
56 String,
60}
61
62impl DataType {
63 pub fn unpremultiply(&self) -> Option<Component> {
67 match *self {
68 DataType::TransformList => Some(Component {
69 name: ComponentName::DataType(DataType::TransformFunction),
70 multiplier: Some(Multiplier::Space),
71 }),
72 _ => None,
73 }
74 }
75
76 pub fn from_str(ty: &str) -> Option<Self> {
78 Some(match ty.as_bytes() {
79 b"length" => DataType::Length,
80 b"number" => DataType::Number,
81 b"percentage" => DataType::Percentage,
82 b"length-percentage" => DataType::LengthPercentage,
83 b"color" => DataType::Color,
84 b"image" => DataType::Image,
85 b"url" => DataType::Url,
86 b"integer" => DataType::Integer,
87 b"angle" => DataType::Angle,
88 b"time" => DataType::Time,
89 b"resolution" => DataType::Resolution,
90 b"transform-function" => DataType::TransformFunction,
91 b"custom-ident" => DataType::CustomIdent,
92 b"transform-list" => DataType::TransformList,
93 b"string" => DataType::String,
94 _ => return None,
95 })
96 }
97
98 pub fn dependent_types(&self) -> DependentDataTypes {
100 match self {
101 DataType::Length
102 | DataType::LengthPercentage
103 | DataType::TransformFunction
104 | DataType::TransformList => DependentDataTypes::LENGTH,
105 DataType::Color => DependentDataTypes::COLOR,
106 DataType::Number
107 | DataType::Percentage
108 | DataType::Image
109 | DataType::Url
110 | DataType::Integer
111 | DataType::Angle
112 | DataType::Time
113 | DataType::Resolution
114 | DataType::CustomIdent
115 | DataType::String => DependentDataTypes::empty(),
116 }
117 }
118}
119
120impl ToCss for DataType {
121 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
122 where
123 W: Write,
124 {
125 dest.write_char('<')?;
126 dest.write_str(match *self {
127 DataType::Length => "length",
128 DataType::Number => "number",
129 DataType::Percentage => "percentage",
130 DataType::LengthPercentage => "length-percentage",
131 DataType::Color => "color",
132 DataType::Image => "image",
133 DataType::Url => "url",
134 DataType::Integer => "integer",
135 DataType::Angle => "angle",
136 DataType::Time => "time",
137 DataType::Resolution => "resolution",
138 DataType::TransformFunction => "transform-function",
139 DataType::CustomIdent => "custom-ident",
140 DataType::TransformList => "transform-list",
141 DataType::String => "string",
142 })?;
143 dest.write_char('>')
144 }
145}