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