swash/
setting.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use super::{tag_from_bytes, tag_from_str_lossy, Tag};
use core::fmt;

/// Setting combining a tag and a value for features and variations.
#[derive(Copy, Clone, Default, Debug)]
pub struct Setting<T: Copy> {
    /// The tag that identifies the setting.
    pub tag: Tag,
    /// The value for the setting.
    pub value: T,
}

impl<T: Copy + PartialEq> PartialEq for Setting<T> {
    fn eq(&self, other: &Self) -> bool {
        self.tag == other.tag && self.value == other.value
    }
}

impl<T: Copy + PartialEq> Eq for Setting<T> {}

impl<T: Copy + fmt::Display> fmt::Display for Setting<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let bytes = self.tag.to_be_bytes();
        let tag_name = core::str::from_utf8(&bytes).unwrap_or("");
        write!(f, "\"{}\" {}", tag_name, self.value)
    }
}

impl Setting<u16> {
    /// Parses a feature setting according to the CSS grammar.
    pub fn parse(s: &str) -> Option<Self> {
        Self::parse_list(s).next()
    }

    /// Parses a comma separated list of feature settings according to the CSS
    /// grammar.
    pub fn parse_list(s: &str) -> impl Iterator<Item = Self> + '_ + Clone {
        ParseList::new(s)
            .map(|(_, tag, value_str)| {
                let (ok, value) = match value_str {
                    "on" | "" => (true, 1),
                    "off" => (true, 0),
                    _ => match value_str.parse::<u16>() {
                        Ok(value) => (true, value),
                        _ => (false, 0),
                    },
                };
                (ok, tag, value)
            })
            .take_while(|(ok, _, _)| *ok)
            .map(|(_, tag, value)| Self { tag, value })
    }
}

impl Setting<f32> {
    /// Parses a variation setting according to the CSS grammar.    
    pub fn parse(s: &str) -> Option<Self> {
        Self::parse_list(s).next()
    }

    /// Parses a comma separated list of variation settings according to the
    /// CSS grammar.    
    pub fn parse_list(s: &str) -> impl Iterator<Item = Self> + '_ + Clone {
        ParseList::new(s)
            .map(|(_, tag, value_str)| {
                let (ok, value) = match value_str.parse::<f32>() {
                    Ok(value) => (true, value),
                    _ => (false, 0.),
                };
                (ok, tag, value)
            })
            .take_while(|(ok, _, _)| *ok)
            .map(|(_, tag, value)| Self { tag, value })
    }
}

impl<T: Copy> From<(Tag, T)> for Setting<T> {
    fn from(v: (Tag, T)) -> Self {
        Self {
            tag: v.0,
            value: v.1,
        }
    }
}

impl<T: Copy> From<&(Tag, T)> for Setting<T> {
    fn from(v: &(Tag, T)) -> Self {
        Self {
            tag: v.0,
            value: v.1,
        }
    }
}

impl<T: Copy> From<&([u8; 4], T)> for Setting<T> {
    fn from(v: &([u8; 4], T)) -> Self {
        Self {
            tag: tag_from_bytes(&v.0),
            value: v.1,
        }
    }
}

impl<T: Copy> From<&(&[u8; 4], T)> for Setting<T> {
    fn from(v: &(&[u8; 4], T)) -> Self {
        Self {
            tag: tag_from_bytes(v.0),
            value: v.1,
        }
    }
}

impl<T: Copy> From<(&str, T)> for Setting<T> {
    fn from(v: (&str, T)) -> Self {
        Self {
            tag: tag_from_str_lossy(v.0),
            value: v.1,
        }
    }
}

impl<T: Copy> From<&(&str, T)> for Setting<T> {
    fn from(v: &(&str, T)) -> Self {
        Self {
            tag: tag_from_str_lossy(v.0),
            value: v.1,
        }
    }
}

#[derive(Clone)]
struct ParseList<'a> {
    source: &'a [u8],
    len: usize,
    pos: usize,
}

impl<'a> ParseList<'a> {
    fn new(source: &'a str) -> Self {
        Self {
            source: source.as_bytes(),
            len: source.len(),
            pos: 0,
        }
    }
}

impl<'a> Iterator for ParseList<'a> {
    type Item = (usize, Tag, &'a str);

    fn next(&mut self) -> Option<Self::Item> {
        let mut pos = self.pos;
        while pos < self.len && {
            let ch = self.source[pos];
            ch.is_ascii_whitespace() || ch == b','
        } {
            pos += 1;
        }
        self.pos = pos;
        if pos >= self.len {
            return None;
        }
        let first = self.source[pos];
        let mut start = pos;
        let quote = match first {
            b'"' | b'\'' => {
                pos += 1;
                start += 1;
                first
            }
            _ => return None,
        };
        let mut tag_str = None;
        while pos < self.len {
            if self.source[pos] == quote {
                tag_str = core::str::from_utf8(self.source.get(start..pos)?).ok();
                pos += 1;
                break;
            }
            pos += 1;
        }
        self.pos = pos;
        let tag_str = tag_str?;
        if tag_str.len() != 4 || !tag_str.is_ascii() {
            return None;
        }
        let tag = tag_from_str_lossy(tag_str);
        while pos < self.len {
            if !self.source[pos].is_ascii_whitespace() {
                break;
            }
            pos += 1;
        }
        self.pos = pos;
        start = pos;
        let mut end = start;
        while pos < self.len {
            if self.source[pos] == b',' {
                pos += 1;
                break;
            }
            pos += 1;
            end += 1;
        }
        let value = core::str::from_utf8(self.source.get(start..end)?)
            .ok()?
            .trim();
        self.pos = pos;
        Some((pos, tag, value))
    }
}