citum_schema_style/template/
reference.rs1#[cfg(feature = "schema")]
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11
12use crate::embedded;
13use crate::template::Template;
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
20#[cfg_attr(feature = "schema", derive(JsonSchema))]
21#[serde(rename_all = "kebab-case")]
22pub enum TemplatePreset {
23 Apa,
25 ChicagoAuthorDate,
27 Vancouver,
29 Ieee,
31 Harvard,
33 NumericCitation,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40#[cfg_attr(feature = "schema", derive(JsonSchema))]
41#[serde(untagged)]
42pub enum TemplateReference {
43 Preset(TemplatePreset),
45 Uri(String),
47}
48
49impl From<TemplatePreset> for TemplateReference {
50 fn from(preset: TemplatePreset) -> Self {
51 TemplateReference::Preset(preset)
52 }
53}
54
55impl TemplateReference {
56 #[must_use]
58 pub fn citation_template(&self) -> Option<Template> {
59 match self {
60 Self::Preset(preset) => Some(preset.citation_template()),
61 Self::Uri(_) => None,
62 }
63 }
64
65 #[must_use]
67 pub fn bibliography_template(&self) -> Option<Template> {
68 match self {
69 Self::Preset(preset) => Some(preset.bibliography_template()),
70 Self::Uri(_) => None,
71 }
72 }
73}
74
75impl TemplatePreset {
76 #[must_use]
78 pub fn citation_template(&self) -> Template {
79 match self {
80 TemplatePreset::Apa => embedded::apa_citation(),
81 TemplatePreset::ChicagoAuthorDate => embedded::chicago_author_date_citation(),
82 TemplatePreset::Vancouver => embedded::vancouver_citation(),
83 TemplatePreset::Ieee => embedded::ieee_citation(),
84 TemplatePreset::Harvard => embedded::harvard_citation(),
85 TemplatePreset::NumericCitation => embedded::numeric_citation(),
86 }
87 }
88
89 #[must_use]
91 pub fn bibliography_template(&self) -> Template {
92 match self {
93 TemplatePreset::Apa => embedded::apa_bibliography(),
94 TemplatePreset::ChicagoAuthorDate => embedded::chicago_author_date_bibliography(),
95 TemplatePreset::Vancouver => embedded::vancouver_bibliography(),
96 TemplatePreset::Ieee => embedded::ieee_bibliography(),
97 TemplatePreset::Harvard => embedded::harvard_bibliography(),
98 TemplatePreset::NumericCitation => embedded::vancouver_bibliography(),
100 }
101 }
102}
103
104#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)]
106#[cfg_attr(feature = "schema", derive(JsonSchema))]
107#[serde(rename_all = "kebab-case")]
108pub struct LocalizedTemplateSpec {
109 #[serde(skip_serializing_if = "Option::is_none")]
111 pub locale: Option<Vec<String>>,
112 #[serde(skip_serializing_if = "Option::is_none")]
114 pub default: Option<bool>,
115 pub template: Template,
117 #[serde(
121 flatten,
122 default,
123 skip_serializing_if = "std::collections::BTreeMap::is_empty"
124 )]
125 #[cfg_attr(feature = "schema", schemars(skip))]
126 pub unknown_fields: std::collections::BTreeMap<String, serde_yaml::Value>,
127}
128
129pub(crate) fn locale_matches(targets: &[String], language: &str) -> bool {
130 let primary = language.split('-').next().unwrap_or(language);
131 targets.iter().any(|candidate| {
132 candidate == language || candidate.split('-').next().unwrap_or(candidate) == primary
133 })
134}