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
use std::io::Write;

use crate::common_attributes::{implement_common_attributes, CommonAttributes};
use crate::{Coordinate, SvgColor, SvgElement, SvgId, SvgStrokeLinecap, SvgTransform};

pub struct SvgUse {
    id: SvgId,
    x: Option<Coordinate>,
    y: Option<Coordinate>,
    common_attributes: CommonAttributes,
}

implement_common_attributes!(SvgUse);

impl From<SvgUse> for SvgElement {
    fn from(value: SvgUse) -> Self {
        Self::Use(value)
    }
}

impl SvgUse {
    pub const fn new(id: SvgId) -> Self {
        Self {
            id,
            x: None,
            y: None,
            common_attributes: CommonAttributes::new(),
        }
    }

    pub const fn x(mut self, new_x: Coordinate) -> Self {
        self.x = Some(new_x);
        self
    }

    pub const fn y(mut self, new_y: Coordinate) -> Self {
        self.y = Some(new_y);
        self
    }

    pub(crate) fn write<W: Write>(&self, id: Option<SvgId>, writer: &mut W) {
        #![allow(clippy::unwrap_used)]
        writer.write_all(b"<use").unwrap();
        if let Some(id) = id {
            id.write(writer);
        }
        writer
            .write_all(format!(" href=\"#{}\"", self.id).as_bytes())
            .unwrap();
        if let Some(x) = self.x {
            writer.write_all(format!(" x=\"{x}\"").as_bytes()).unwrap();
        }
        if let Some(y) = self.y {
            writer.write_all(format!(" y=\"{y}\"").as_bytes()).unwrap();
        }

        self.common_attributes.write(writer);

        writer.write_all(b"/>\n").unwrap();
    }
}