use std::sync::Arc;
use crate::FontType;
#[cfg(any(feature = "fs", feature = "in_memory"))]
use crate::result::FontOutputs;
#[derive(Clone)]
pub enum OutputContents {
Bytes(Arc<Vec<u8>>),
Text(Arc<String>),
}
impl OutputContents {
pub fn as_bytes(&self) -> &[u8] {
match self {
Self::Bytes(bytes) => bytes.as_slice(),
Self::Text(text) => text.as_bytes(),
}
}
}
impl AsRef<[u8]> for OutputContents {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum OutputKind {
Font(FontType),
Css,
Html,
Ts,
}
#[derive(Clone)]
pub struct CollectedOutput {
pub kind: OutputKind,
pub contents: OutputContents,
}
#[cfg(any(feature = "fs", feature = "in_memory"))]
pub(crate) fn collect_outputs(
fonts: &FontOutputs,
css: Option<Arc<String>>,
html: Option<Arc<String>>,
ts: Option<Arc<String>>,
) -> Vec<CollectedOutput> {
let mut outputs = Vec::new();
if let Some(svg_font) = &fonts.svg_font {
outputs.push(CollectedOutput {
kind: OutputKind::Font(FontType::Svg),
contents: OutputContents::Text(Arc::clone(svg_font)),
});
}
if let Some(ttf_font) = &fonts.ttf_font {
outputs.push(CollectedOutput {
kind: OutputKind::Font(FontType::Ttf),
contents: OutputContents::Bytes(Arc::clone(ttf_font)),
});
}
if let Some(woff_font) = &fonts.woff_font {
outputs.push(CollectedOutput {
kind: OutputKind::Font(FontType::Woff),
contents: OutputContents::Bytes(Arc::clone(woff_font)),
});
}
if let Some(woff2_font) = &fonts.woff2_font {
outputs.push(CollectedOutput {
kind: OutputKind::Font(FontType::Woff2),
contents: OutputContents::Bytes(Arc::clone(woff2_font)),
});
}
if let Some(eot_font) = &fonts.eot_font {
outputs.push(CollectedOutput {
kind: OutputKind::Font(FontType::Eot),
contents: OutputContents::Bytes(Arc::clone(eot_font)),
});
}
if let Some(css) = css {
outputs.push(CollectedOutput {
kind: OutputKind::Css,
contents: OutputContents::Text(css),
});
}
if let Some(html) = html {
outputs.push(CollectedOutput {
kind: OutputKind::Html,
contents: OutputContents::Text(html),
});
}
if let Some(ts) = ts {
outputs.push(CollectedOutput {
kind: OutputKind::Ts,
contents: OutputContents::Text(ts),
});
}
outputs
}