decal 0.6.0

Declarative DSL for describing scenes and rendering them to SVG or PNG
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use super::writer::Writer;
use crate::{
    primitives::{
        Length,
        PositiveF32Pair,
    },
    utils::FloatWriter,
};
use std::{
    fmt::{
        Display,
        Write,
    },
    marker::PhantomData,
};
use strict_num::{
    FiniteF32,
    NormalizedF32,
    PositiveF32,
};

pub(crate) struct Initialized;
pub(crate) struct Opened;

mod private {
    pub trait WriterState {}
}

impl private::WriterState for Initialized {}
impl private::WriterState for Opened {}

/// Stateful XML element writer with compile time enforcement of valid write
/// order.
#[derive(Debug)]
pub(crate) struct ElementWriter<'a, T, S>
where
    T: Write,
{
    out: &'a mut T,
    element_name: &'a str,
    state: PhantomData<S>,
}

impl<T, S> FloatWriter<T> for ElementWriter<'_, T, S>
where
    T: Write,
{
    fn out_mut(&mut self) -> &mut T {
        &mut self.out
    }
}

impl<T, S> Writer<T> for ElementWriter<'_, T, S> where T: Write {}

impl<'a, T, S> ElementWriter<'a, T, S>
where
    T: Write,
    S: private::WriterState,
{
    /// Executes a write operation against the underlying output and returns the
    /// writer.
    ///
    /// # Arguments
    /// - `write_fn`: Closure that writes formatted content into the output.
    ///
    /// # Returns
    /// - `Ok(Self)` if writing succeeds.
    pub(crate) fn write<F>(self, write_fn: F) -> Result<Self, std::fmt::Error>
    where
        F: FnOnce(&mut T) -> std::fmt::Result,
    {
        write_fn(self.out).map(|_| self)
    }
}

impl<'a, T> ElementWriter<'a, T, Initialized>
where
    T: Write,
{
    /// Begins writing a new XML element with the given name.
    ///
    /// # Arguments
    /// - `out`: Output sink receiving the serialized element.
    /// - `element_name`: Name of the XML element.
    ///
    /// # Returns
    /// - [`Self`] in the [`Initialized`] state.
    pub(crate) fn new(
        out: &'a mut T,
        element_name: &'a str,
    ) -> Result<ElementWriter<'a, T, Initialized>, std::fmt::Error> {
        write!(out, "<{element_name}").map(|_| ElementWriter {
            out,
            element_name,
            state: PhantomData,
        })
    }

    /// Writes a single attribute onto the element.
    ///
    /// # Arguments
    /// - `key`: Attribute name.
    /// - `value`: Attribute value convertible via [`IntoAttrValue`].
    ///
    /// # Returns
    /// - [`Self`]
    pub(crate) fn attr<V>(self, key: &str, value: V) -> Result<Self, std::fmt::Error>
    where
        V: IntoAttrValue<T>,
    {
        value.write(key, self.out)?;
        Ok(self)
    }

    /// Writes multiple attributes onto the element.
    ///
    /// # Arguments
    /// - `attrs`: Iterator of key-value attribute pairs.
    ///
    /// # Returns
    /// - [`Self`]
    pub(crate) fn attrs<'b, I, V>(self, attrs: I) -> Result<Self, std::fmt::Error>
    where
        V: IntoAttrValue<T>,
        I: IntoIterator<Item = (&'b str, V)>,
    {
        attrs
            .into_iter()
            .try_for_each(|(key, value)| value.write(key, self.out))
            .map(|_| self)
    }

    /// Conditionally writes an attribute based on a boolean flag.
    ///
    /// # Arguments
    /// - `key`: Attribute name.
    /// - `value`: Attribute value.
    /// - `condition`: Whether the attribute should be written.
    ///
    /// # Returns
    /// - [`Self`]
    pub(crate) fn attr_if<V>(
        mut self,
        key: &str,
        value: V,
        condition: bool,
    ) -> Result<Self, std::fmt::Error>
    where
        V: IntoAttrValue<T>,
    {
        if condition {
            self = self.attr(key, value)?;
        }

        Ok(self)
    }

    /// Writes a custom attribute using a closure.
    ///
    /// # Arguments
    /// - `key`: Attribute name.
    /// - `write_fn`: Closure that writes the attribute value.
    ///
    /// # Returns
    /// - [`Self`]
    pub(crate) fn write_attr<F>(self, key: &str, write_fn: F) -> Result<Self, std::fmt::Error>
    where
        F: FnOnce(&mut T) -> std::fmt::Result,
    {
        write!(self.out, r#" {key}=""#)?;
        write_fn(self.out)?;
        self.out.write_char('"').map(|_| self)
    }

    /// Opens the element, transitioning it to the [`Opened`] state.
    ///
    /// # Returns
    /// - [`Self`] in the [`Opened`] state.
    pub(crate) fn open(self) -> Result<ElementWriter<'a, T, Opened>, std::fmt::Error> {
        self.out.write_char('>').map(|_| ElementWriter {
            out: self.out,
            element_name: self.element_name,
            state: PhantomData,
        })
    }

    /// Writes element content without explicitly opening the element first.
    ///
    /// # Arguments
    /// - `write_fn`: Closure that writes element content.
    ///
    /// # Returns
    /// - [`Self`] in the [`Opened`] state.
    pub(crate) fn content<F>(
        self,
        write_fn: F,
    ) -> Result<ElementWriter<'a, T, Opened>, std::fmt::Error>
    where
        F: FnOnce(&mut T) -> std::fmt::Result,
    {
        let writer = self.open()?;
        writer.content(write_fn)
    }

    /// Closes the element as a self-closing tag.
    pub(crate) fn close(self) -> std::fmt::Result {
        self.out.write_str(" />")
    }

    /// Writes a closing tag for the given element name.
    ///
    /// # Arguments
    /// - `out`: The output writer.
    /// - `element_name`: Name of the element to close.
    pub(crate) fn close_tag(out: &'a mut T, element_name: &str) -> std::fmt::Result {
        write!(out, "</{element_name}>")
    }
}

impl<'a, T> ElementWriter<'a, T, Opened>
where
    T: Write,
{
    /// Writes content inside an opened element.
    ///
    /// # Arguments
    /// - `write_fn`: Closure that writes content.
    ///
    /// # Returns
    /// - [`Self`]
    pub(crate) fn content<F>(self, write_fn: F) -> Result<Self, std::fmt::Error>
    where
        F: FnOnce(&mut T) -> std::fmt::Result,
    {
        self.write(write_fn)
    }

    /// Closes the element with a matching end tag.
    pub(crate) fn close(self) -> std::fmt::Result {
        write!(self.out, "</{}>", self.element_name)
    }
}

/// Trait for types that can be written as XML attribute values.
pub(crate) trait IntoAttrValue<W>
where
    W: Write,
{
    /// Writes the value as an attribute with the given key.
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result;
}

impl<'a, W, D> IntoAttrValue<W> for (D,)
where
    W: Write,
    D: Display,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        write!(out, r#" {key}="{}""#, self.0)
    }
}

impl<'a, W> IntoAttrValue<W> for &'a str
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        write!(out, r#" {key}="{self}""#)
    }
}

impl<'a, W> IntoAttrValue<W> for String
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        IntoAttrValue::write(&self.as_str(), key, out)
    }
}

impl<'a, W> IntoAttrValue<W> for f32
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        write!(out, r#" {key}=""#)?;
        out.write_float(*self)?;
        out.write_char('"')
    }
}

impl<'a, W, const AUTO: bool, const PERCENT: bool> IntoAttrValue<W> for Length<AUTO, PERCENT>
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        IntoAttrValue::write(&(self,), key, out)
    }
}

impl<'a, W> IntoAttrValue<W> for FiniteF32
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        IntoAttrValue::write(&self.get(), key, out)
    }
}

impl<'a, W> IntoAttrValue<W> for NormalizedF32
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        IntoAttrValue::write(&self.get(), key, out)
    }
}

impl<'a, W> IntoAttrValue<W> for PositiveF32
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        IntoAttrValue::write(&self.get(), key, out)
    }
}

impl<'a, W> IntoAttrValue<W> for PositiveF32Pair
where
    W: Write,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        write!(out, r#" {key}="{self}""#)
    }
}

impl<'a, W, V> IntoAttrValue<W> for Option<V>
where
    W: Write,
    V: IntoAttrValue<W>,
{
    fn write(&self, key: &str, out: &mut W) -> std::fmt::Result {
        if let Some(value) = self {
            IntoAttrValue::write(value, key, out)?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        macros::{
            ff32,
            nf32,
            pf32,
        },
        test_utils::{
            assert_xml,
            str_sink,
        },
    };

    #[test]
    fn writes_empty_element() {
        assert_xml(
            str_sink(|out| ElementWriter::new(out, "path")?.close()),
            r#"<path />"#,
        );
    }

    #[test]
    fn writes_single_attr() {
        assert_xml(
            str_sink(|out| ElementWriter::new(out, "rect")?.attr("id", "test")?.close()),
            r#"<rect id="test" />"#,
        );
    }

    #[test]
    fn writes_multiple_attrs() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "rect")?
                    .attrs([("x", 10.0), ("y", 25.0), ("width", 96.0), ("height", 64.0)])?
                    .close()
            }),
            r#"<rect x="10" y="25" width="96" height="64" />"#,
        );
    }

    #[test]
    fn attr_if_writes_conditionally() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "circle")?
                    .attr_if("cx", 50.0, true)?
                    .attr_if("cy", 50.0, false)?
                    .close()
            }),
            r#"<circle cx="50" />"#,
        );
    }

    #[test]
    fn element_with_content() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "text")?
                    .open()?
                    .content(|out| out.write_str("hello"))?
                    .close()
            }),
            r#"<text>hello</text>"#,
        );
    }

    #[test]
    fn content_without_manually_opening() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "text")?
                    .content(|out| out.write_str("hello"))?
                    .close()
            }),
            r#"<text>hello</text>"#,
        );
    }

    #[test]
    fn custom_attr_writer() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "polygon")?
                    .write_attr("points", |out| out.write_str("0,0 10,10"))?
                    .close()
            }),
            r#"<polygon points="0,0 10,10" />"#,
        );
    }

    #[test]
    fn writes_float_attrs() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "group")?
                    .attr("x", nf32!(0.5))?
                    .attr("y", pf32!(1.5))?
                    .attr("z", ff32!(2.5))?
                    .close()
            }),
            r#"<group x="0.5" y="1.5" z="2.5" />"#,
        );
    }

    #[test]
    fn writes_length_attr() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "rect")?
                    .attr("width", Length::<false, false>::units(100.0))?
                    .close()
            }),
            r#"<rect width="100" />"#,
        );
    }

    #[test]
    fn writes_positive_f32_pair() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "feMorphology")?
                    .attr("radius", PositiveF32Pair::from((1.5, 2.5)))?
                    .close()
            }),
            r#"<feMorphology radius="1.5 2.5" />"#,
        );
    }

    #[test]
    fn writes_optional_attr() {
        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "rect")?
                    .attr("x", Some(10.0))?
                    .close()
            }),
            r#"<rect x="10" />"#,
        );

        assert_xml(
            str_sink(|out| {
                ElementWriter::new(out, "rect")?
                    .attr("x", None::<f32>)?
                    .close()
            }),
            "<rect />",
        );
    }

    #[test]
    fn writes_close_tag() {
        assert_eq!(str_sink(|out| ElementWriter::close_tag(out, "g")), "</g>");
    }
}