use super::{Located, Reader};
use crate::diagnostic::Location;
use serde_json::Value;
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct FontDeclaration {
pub(crate) name: Located<String>,
pub(crate) fallback: Option<Located<Value>>,
pub(crate) files: Option<Vec<FontFileEntry>>,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct FontFileEntry {
pub(crate) at: Location,
pub(crate) from: Option<Located<Value>>,
pub(crate) weight: Option<Located<Value>>,
pub(crate) style: Option<Located<Value>>,
}
impl Reader<'_> {
pub(super) fn fonts(&self, md: &Value) -> Vec<FontDeclaration> {
md.get("fonts")
.and_then(Value::as_object)
.into_iter()
.flatten()
.map(|(name, declared)| {
let pointer = format!("/md/fonts/{name}");
FontDeclaration {
name: Located {
value: name.clone(),
at: self.key_at(&pointer),
},
fallback: self.located(declared, &pointer, "fallback"),
files: declared
.get("files")
.and_then(Value::as_array)
.map(|files| {
files
.iter()
.enumerate()
.map(|(i, entry)| {
let pointer = format!("{pointer}/files/{i}");
FontFileEntry {
at: self.value_at(&pointer),
from: self.located(entry, &pointer, "from"),
weight: self.located(entry, &pointer, "weight"),
style: self.located(entry, &pointer, "style"),
}
})
.collect()
}),
}
})
.collect()
}
fn located(&self, entry: &Value, pointer: &str, key: &str) -> Option<Located<Value>> {
Some(Located {
value: entry.get(key)?.clone(),
at: self.content_at(&format!("{pointer}/{key}")),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parse::{ParseOptions, parse};
use serde_json::json;
#[test]
fn reads_each_font_with_its_files_and_their_places() {
let fonts = parse(
"---\nmd:\n fonts:\n harbour-serif:\n fallback: heading\n files:\n - { from: ./h.woff2 }\n - { from: \"./h-bold.woff2\", weight: bold, style: italic }\n plain: {}\n---\n",
&ParseOptions::default(),
)
.settings
.fonts;
assert_eq!(fonts.len(), 2);
let harbour = &fonts[0];
assert_eq!(
(harbour.name.value.as_str(), harbour.name.at),
("harbour-serif", Location::new(4, 5))
);
assert_eq!(
harbour.fallback,
Some(Located {
value: json!("heading"),
at: Location::new(5, 17)
})
);
let files = harbour.files.as_ref().expect("files");
assert_eq!(files[0].at, Location::new(7, 11));
assert_eq!(
files[1].from,
Some(Located {
value: json!("./h-bold.woff2"),
at: Location::new(8, 20)
})
);
assert_eq!(
files[1].weight.as_ref().map(|w| w.value.clone()),
Some(json!("bold"))
);
assert_eq!(files[0].style, None);
assert_eq!(
(fonts[1].fallback.as_ref(), fonts[1].files.as_ref()),
(None, None)
);
}
}