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}
42
43/// A generated payload with no filesystem destination.
44#[derive(Clone)]
45pub struct CollectedOutput {
46    /// Identifies the format.
47    pub kind: OutputKind,
48    /// Reference-counted output data.
49    pub contents: OutputContents,
50}
51
52/// Collect already-produced contents without assigning destinations or copying payloads.
53#[cfg(any(feature = "fs", feature = "in_memory"))]
54pub(crate) fn collect_outputs(
55    fonts: &FontOutputs,
56    css: Option<Arc<String>>,
57    html: Option<Arc<String>>,
58) -> Vec<CollectedOutput> {
59    let mut outputs = Vec::new();
60
61    if let Some(svg_font) = &fonts.svg_font {
62        outputs.push(CollectedOutput {
63            kind: OutputKind::Font(FontType::Svg),
64            contents: OutputContents::Text(Arc::clone(svg_font)),
65        });
66    }
67    if let Some(ttf_font) = &fonts.ttf_font {
68        outputs.push(CollectedOutput {
69            kind: OutputKind::Font(FontType::Ttf),
70            contents: OutputContents::Bytes(Arc::clone(ttf_font)),
71        });
72    }
73    if let Some(woff_font) = &fonts.woff_font {
74        outputs.push(CollectedOutput {
75            kind: OutputKind::Font(FontType::Woff),
76            contents: OutputContents::Bytes(Arc::clone(woff_font)),
77        });
78    }
79    if let Some(woff2_font) = &fonts.woff2_font {
80        outputs.push(CollectedOutput {
81            kind: OutputKind::Font(FontType::Woff2),
82            contents: OutputContents::Bytes(Arc::clone(woff2_font)),
83        });
84    }
85    if let Some(eot_font) = &fonts.eot_font {
86        outputs.push(CollectedOutput {
87            kind: OutputKind::Font(FontType::Eot),
88            contents: OutputContents::Bytes(Arc::clone(eot_font)),
89        });
90    }
91
92    if let Some(css) = css {
93        outputs.push(CollectedOutput {
94            kind: OutputKind::Css,
95            contents: OutputContents::Text(css),
96        });
97    }
98    if let Some(html) = html {
99        outputs.push(CollectedOutput {
100            kind: OutputKind::Html,
101            contents: OutputContents::Text(html),
102        });
103    }
104    outputs
105}