Skip to main content

svgplot/
style.rs

1use crate::escape::escape_xml;
2use crate::{SvgElement, SvgId};
3use std::io::Write;
4
5#[derive(Default)]
6pub struct SvgStyle {
7    pub content: String,
8}
9
10impl From<SvgStyle> for SvgElement {
11    fn from(value: SvgStyle) -> Self {
12        Self::Style(value)
13    }
14}
15
16impl SvgStyle {
17    pub const fn new(content: String) -> Self {
18        Self { content }
19    }
20    pub(crate) fn write<W: Write>(&self, id: Option<SvgId>, buffer: &mut W) {
21        #![allow(clippy::unwrap_used)]
22        buffer.write_all(b"<style").unwrap();
23        if let Some(id) = id {
24            id.write(buffer);
25        }
26        buffer.write_all(b">").unwrap();
27        buffer
28            .write_all(escape_xml(&self.content).as_bytes())
29            .unwrap();
30        buffer.write_all(b"</style>").unwrap();
31    }
32}