oxvg_collections 0.0.5

Collections of data and utilities about SVG
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Content types as specified in [SVG 1.1](https://www.w3.org/TR/2011/REC-SVG11-20110816/types.html) and [SVG 2](https://svgwg.org/svg2-draft/propidx.html)
use std::ops::Deref;

use lightningcss::{
    declaration::DeclarationBlock,
    properties::svg::SVGPaint,
    stylesheet::ParserOptions,
    values::{
        alpha::AlphaValue,
        color::CssColor,
        length::LengthValue,
        number::{CSSInteger, CSSNumber},
    },
};

pub use lightningcss::{
    properties::text::Spacing,
    values::{percentage::Percentage, time::Time},
};

#[cfg(feature = "parse")]
use oxvg_parse::{error::Error, Parse, Parser};
#[cfg(feature = "serialize")]
use oxvg_serialize::{error::PrinterError, Printer, ToValue};

use crate::atom::Atom;

pub use super::transform::SVGTransformList;

/// A CSS angle
pub type Angle = lightningcss::values::angle::Angle;
/// A sequence of any characters
pub type Anything<'i> = Atom<'i>;
/// An id string
pub type Id<'i> = NonWhitespace<'i>;
/// A class string
pub type Class<'i> = NonWhitespace<'i>;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// A sequence of non-whitespace characters
pub struct NonWhitespace<'i>(pub Anything<'i>);
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for NonWhitespace<'input> {
    fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
        input.skip_whitespace();
        let slice = input.take_matches(|char| !char.is_whitespace());
        if slice.is_empty() {
            Err(Error::ExpectedMatch {
                expected: "non-whitespace character",
                received: "nothing",
            })?
        } else {
            Ok(Self(slice.into()))
        }
    }
}
#[cfg(feature = "serialize")]
impl ToValue for NonWhitespace<'_> {
    fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
    where
        W: std::fmt::Write,
    {
        dest.write_str(&self.0)
    }
}
impl Deref for NonWhitespace<'_> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<'a> From<&'a str> for NonWhitespace<'a> {
    fn from(value: &'a str) -> Self {
        Self(value.into())
    }
}
#[test]
fn non_whitespace() {
    assert_eq!(
        NonWhitespace::parse_string(" foo "),
        Ok(NonWhitespace("foo".into()))
    );

    assert_eq!(
        NonWhitespace::parse_string(" foo bar "),
        Err(Error::ExpectedDone)
    );
    assert_eq!(
        NonWhitespace::parse_string(""),
        Err(Error::ExpectedMatch {
            expected: "non-whitespace character",
            received: "nothing"
        })
    );
    assert_eq!(
        NonWhitespace::parse_string(" \n\t"),
        Err(Error::ExpectedMatch {
            expected: "non-whitespace character",
            received: "nothing"
        })
    );
}

#[derive(Debug, Clone, PartialEq)]
/// A boolean attribute is true when it's empty or matches the attribute's canonical name
///
/// [HTML](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attribute)
pub struct Boolean<'input>(Option<Atom<'input>>);
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for Boolean<'input> {
    fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
        input.skip_whitespace();
        Ok(Self(
            input
                .try_parse(|input| -> Result<Atom<'input>, ()> {
                    let ident = input.expect_ident().map_err(|_| ())?;
                    Ok(Atom::Cow(ident.into()))
                })
                .ok(),
        ))
    }
}
#[cfg(feature = "serialize")]
impl ToValue for Boolean<'_> {
    fn write_value<W>(&self, _dest: &mut Printer<W>) -> Result<(), PrinterError>
    where
        W: std::fmt::Write,
    {
        // Empty string is valid boolean value
        // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes
        Ok(())
    }
}
#[test]
fn boolean() {
    assert_eq!(Boolean::parse_string(""), Ok(Boolean(None)));
    assert_eq!(
        Boolean::parse_string(" autofocus "),
        Ok(Boolean(Some("autofocus".into())))
    );
}

/// A CSS colour
pub type Color = CssColor;

#[derive(Clone, Debug, PartialEq)]
/// A number, absolute, or relative length
pub enum Length {
    /// A number length
    Number(Number),
    /// An absolute length
    Length(LengthValue),
    /// A relative length
    Percentage(Percentage),
}
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for Length {
    fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
        input.skip_whitespace();
        input
            .try_parse(Percentage::parse)
            .map(Self::Percentage)
            .or_else(|_| input.try_parse(LengthValue::parse).map(Self::Length))
            .or_else(|_| Number::parse(input).map(Self::Number))
    }
}
#[cfg(feature = "serialize")]
impl ToValue for Length {
    fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
    where
        W: std::fmt::Write,
    {
        match self {
            Self::Number(number) => number.write_value(dest),
            Self::Length(LengthValue::Px(length)) => length.write_value(dest),
            Self::Length(length) => length.write_value(dest),
            Self::Percentage(percentage) => percentage.write_value(dest),
        }
    }
}
#[test]
fn length() {
    assert_eq!(
        Length::parse_string("20.2350"),
        Ok(Length::Length(LengthValue::Px(20.235)))
    );
    assert_eq!(
        Length::parse_string("20.2268px"),
        Ok(Length::Length(LengthValue::Px(20.2268)))
    );
    assert_eq!(
        Length::parse_string("0.22356em"),
        Ok(Length::Length(LengthValue::Em(0.22356)))
    );
    assert_eq!(
        Length::parse_string("80.0005%"),
        Ok(Length::Percentage(Percentage(0.800_005)))
    );

    assert_eq!(Length::parse_string("20 20"), Err(Error::ExpectedDone));
}

#[derive(Clone, Debug, PartialEq)]
/// A frequency in hertz
pub enum Frequency {
    /// Hertz; cycles per second
    Hz(Number),
    /// Kilohertz; cycles per nano-second
    KHz(Number),
}
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for Frequency {
    fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
        let number = Number::parse(input)?;
        let str = input.expect_ident()?;
        Ok(match str {
            "Hz" => Self::Hz(number),
            "KHz" => Self::KHz(number),
            received => {
                return Err(Error::ExpectedIdent {
                    expected: "one of `Hz` `KHz`",
                    received,
                })
            }
        })
    }
}
#[cfg(feature = "serialize")]
impl ToValue for Frequency {
    fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
    where
        W: std::fmt::Write,
    {
        match self {
            Self::Hz(number) => {
                number.write_value(dest)?;
                dest.write_str("Hz")
            }
            Self::KHz(number) => {
                number.write_value(dest)?;
                dest.write_str("KHz")
            }
        }
    }
}
#[test]
fn frequency() {
    assert_eq!(Frequency::parse_string(" 10.5Hz "), Ok(Frequency::Hz(10.5)));
    assert_eq!(Frequency::parse_string(" -1KHz "), Ok(Frequency::KHz(-1.0)));

    assert_eq!(
        Frequency::parse_string("1 Khz"),
        Err(Error::ExpectedIdent {
            expected: "valid ident starting character",
            received: " "
        })
    );
    assert_eq!(
        Frequency::parse_string("1Khz"),
        Err(Error::ExpectedIdent {
            expected: "one of `Hz` `KHz`",
            received: "Khz"
        })
    );
}

/// Functional notation for an IRI
///
/// [w3 | SVG 1.1](https://www.w3.org/TR/2011/REC-SVG11-20110816/types.html#DataTypeFuncIRI)
pub type FuncIRI<'i> = Anything<'i>;
/// An integer
///
/// [w3 | SVG 1.1](https://www.w3.org/TR/2011/REC-SVG11-20110816/types.html#DataTypeInteger)
pub type Integer = CSSInteger;
/// An internationalized resource identifier
///
/// [w3 | SVG 1.1](https://www.w3.org/TR/2011/REC-SVG11-20110816/types.html#DataTypeIRI)
pub type IRI<'i> = Anything<'i>;

/// A non-whitespace, non-parenthesis, non-comma value
pub type Name<'i> = Anything<'i>;
/// A real number
pub type Number = CSSNumber;

#[derive(Clone, Debug, PartialEq)]
/// A pair of numbers, where the second is optional, separated by a comma or whitespace
pub struct NumberOptionalNumber(pub Number, pub Option<Number>);
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for NumberOptionalNumber {
    fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
        let a = Number::parse(input)?;
        let cursor_before_space = input.cursor();
        input.skip_whitespace();
        let has_comma = input.try_parse(|input| input.expect_char(',')).is_ok();
        input.skip_whitespace();
        let b = if has_comma {
            // Comma makes second number compulsory
            Some(Number::parse(input)?)
        } else {
            let cursor_after_space = input.cursor();
            if let Ok(b) = input.try_parse(Number::parse) {
                if cursor_before_space == cursor_after_space {
                    return Err(Error::ExpectedMatch {
                        expected: "space or comma between numbers",
                        received: "nothing",
                    });
                }
                Some(b)
            } else {
                None
            }
        };
        Ok(Self(a, b))
    }
}
#[cfg(feature = "serialize")]
impl ToValue for NumberOptionalNumber {
    fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
    where
        W: std::fmt::Write,
    {
        self.0.write_value(dest)?;
        if let Some(b) = self.1 {
            dest.write_char(' ')?;
            b.write_value(dest)?;
        }
        Ok(())
    }
}
#[test]
fn number_optional_number() {
    assert_eq!(
        NumberOptionalNumber::parse_string("10"),
        Ok(NumberOptionalNumber(10.0, None))
    );
    assert_eq!(
        NumberOptionalNumber::parse_string("10 -1"),
        Ok(NumberOptionalNumber(10.0, Some(-1.0)))
    );
    assert_eq!(
        NumberOptionalNumber::parse_string("10,-1"),
        Ok(NumberOptionalNumber(10.0, Some(-1.0)))
    );
    assert_eq!(
        NumberOptionalNumber::parse_string("10 , -1"),
        Ok(NumberOptionalNumber(10.0, Some(-1.0)))
    );
    assert_eq!(
        NumberOptionalNumber::parse_string("10, -1"),
        Ok(NumberOptionalNumber(10.0, Some(-1.0)))
    );

    assert_eq!(
        NumberOptionalNumber::parse_string("10.1.1"),
        Err(Error::ExpectedMatch {
            expected: "space or comma between numbers",
            received: "nothing"
        })
    );
    assert_eq!(
        NumberOptionalNumber::parse_string("10.1-1"),
        Err(Error::ExpectedMatch {
            expected: "space or comma between numbers",
            received: "nothing"
        })
    );
    assert_eq!(
        NumberOptionalNumber::parse_string("10.1 -1 -1"),
        Err(Error::ExpectedDone)
    );
}

/// An alpha value
pub type Opacity = AlphaValue;
/// A paint value
pub type Paint<'i> = SVGPaint<'i>;

#[derive(Clone, Debug, PartialEq)]
/// A CSS style declaration block
pub struct Style<'i>(pub DeclarationBlock<'i>);
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for Style<'input> {
    fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
        DeclarationBlock::parse_string(input.take_slice(), ParserOptions::default())
            .map(Self)
            .map_err(Error::Lightningcss)
    }
}
#[cfg(feature = "serialize")]
impl ToValue for Style<'_> {
    fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
    where
        W: std::fmt::Write,
    {
        self.0.write_value(dest)
    }
}
impl<'input> Deref for Style<'input> {
    type Target = DeclarationBlock<'input>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
#[test]
fn style() {
    assert_eq!(
        Style::parse_string("display: none;"),
        Ok(Style(DeclarationBlock {
            important_declarations: vec![],
            declarations: vec![lightningcss::properties::Property::Display(
                lightningcss::properties::display::Display::Keyword(
                    lightningcss::properties::display::DisplayKeyword::None
                )
            )]
        }))
    );
}

#[derive(Clone, Debug, PartialEq)]
/// A raw list of CSS tokens
pub struct TokenList<'input>(pub lightningcss::properties::custom::TokenList<'input>);
#[cfg(feature = "serialize")]
impl ToValue for TokenList<'_> {
    fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
    where
        W: std::fmt::Write,
    {
        use lightningcss::properties::{
            custom::{CustomProperty, CustomPropertyName},
            Property,
        };
        Property::Custom(CustomProperty {
            name: CustomPropertyName::Unknown("".into()),
            value: self.0.clone(),
        })
        .to_css(dest, false)
    }
}

/// A URL string
pub type Url<'i> = Anything<'i>;