Skip to main content

webfont_generator/output/
collect.rs

1use std::sync::Arc;
2
3use crate::FontType;
4#[cfg(any(feature = "fs", feature = "in_memory"))]
5use crate::result::FontOutputs;
6
7/// Shared output payload. Cloning it does not copy font bytes or rendered text.
8#[derive(Clone)]
9pub enum OutputContents {
10    /// Binary font payload.
11    Bytes(Arc<Vec<u8>>),
12    /// SVG font or rendered companion text.
13    Text(Arc<String>),
14}
15
16impl OutputContents {
17    /// Borrow the encoded output without copying it.
18    pub fn as_bytes(&self) -> &[u8] {
19        match self {
20            Self::Bytes(bytes) => bytes.as_slice(),
21            Self::Text(text) => text.as_bytes(),
22        }
23    }
24}
25
26impl AsRef<[u8]> for OutputContents {
27    fn as_ref(&self) -> &[u8] {
28        self.as_bytes()
29    }
30}
31
32/// Format of a destination-free generated output.
33#[derive(Clone, Copy, PartialEq, Eq)]
34pub enum OutputKind {
35    /// A generated font format.
36    Font(FontType),
37    /// CSS stylesheet.
38    Css,
39    /// HTML preview.
40    Html,
41    /// TypeScript source with icon names and selector settings.
42    Ts,
43}
44
45/// A generated payload with no filesystem destination.
46#[derive(Clone)]
47pub struct CollectedOutput {
48    /// Identifies the format.
49    pub kind: OutputKind,
50    /// Reference-counted output data.
51    pub contents: OutputContents,
52}
53
54/// Collect already-produced contents without assigning destinations or copying payloads.
55#[cfg(any(feature = "fs", feature = "in_memory"))]
56pub(crate) fn collect_outputs(
57    fonts: &FontOutputs,
58    css: Option<Arc<String>>,
59    html: Option<Arc<String>>,
60    ts: Option<Arc<String>>,
61) -> Vec<CollectedOutput> {
62    let mut outputs = Vec::new();
63
64    if let Some(svg_font) = &fonts.svg_font {
65        outputs.push(CollectedOutput {
66            kind: OutputKind::Font(FontType::Svg),
67            contents: OutputContents::Text(Arc::clone(svg_font)),
68        });
69    }
70    if let Some(ttf_font) = &fonts.ttf_font {
71        outputs.push(CollectedOutput {
72            kind: OutputKind::Font(FontType::Ttf),
73            contents: OutputContents::Bytes(Arc::clone(ttf_font)),
74        });
75    }
76    if let Some(woff_font) = &fonts.woff_font {
77        outputs.push(CollectedOutput {
78            kind: OutputKind::Font(FontType::Woff),
79            contents: OutputContents::Bytes(Arc::clone(woff_font)),
80        });
81    }
82    if let Some(woff2_font) = &fonts.woff2_font {
83        outputs.push(CollectedOutput {
84            kind: OutputKind::Font(FontType::Woff2),
85            contents: OutputContents::Bytes(Arc::clone(woff2_font)),
86        });
87    }
88    if let Some(eot_font) = &fonts.eot_font {
89        outputs.push(CollectedOutput {
90            kind: OutputKind::Font(FontType::Eot),
91            contents: OutputContents::Bytes(Arc::clone(eot_font)),
92        });
93    }
94
95    if let Some(css) = css {
96        outputs.push(CollectedOutput {
97            kind: OutputKind::Css,
98            contents: OutputContents::Text(css),
99        });
100    }
101    if let Some(html) = html {
102        outputs.push(CollectedOutput {
103            kind: OutputKind::Html,
104            contents: OutputContents::Text(html),
105        });
106    }
107    if let Some(ts) = ts {
108        outputs.push(CollectedOutput {
109            kind: OutputKind::Ts,
110            contents: OutputContents::Text(ts),
111        });
112    }
113    outputs
114}