use std::hash::{Hash, Hasher};
use serde::Deserialize;
use crate::core::settings::named;
use crate::stdlib::features::numbering::{
digest_style, CounterStyleDecl, Delimiters, JoinDecl, PluralWords, PrefixWords,
};
pub const STYLE_NAME: &str = "flm.citations.style.citation";
pub const CAPITAL_VARIANT: &str = "capital";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Deserialize)]
#[derive(schemars::JsonSchema)]
#[serde(rename_all = "kebab-case")]
pub enum CitationPlacement {
#[default]
Listed,
InPlace,
}
impl CitationPlacement {
fn digest_byte(self) -> u8 {
match self {
CitationPlacement::Listed => 0,
CitationPlacement::InPlace => 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Deserialize, serde::Serialize)]
#[derive(schemars::JsonSchema)]
#[serde(rename_all = "kebab-case")]
pub enum MarkForm {
#[default]
Bracketed,
Superscript,
}
impl MarkForm {
fn digest_byte(self) -> u8 {
match self {
MarkForm::Bracketed => 0,
MarkForm::Superscript => 1,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
#[derive(schemars::JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct CitationsConfig {
pub placement: CitationPlacement,
pub mark: MarkForm,
#[serde(deserialize_with = "read_style")]
pub style: CounterStyleDecl,
pub heading_title: Option<String>,
pub heading_level: u8,
pub note_separator: String,
}
pub fn default_style() -> CounterStyleDecl {
let mut style = CounterStyleDecl::new(STYLE_NAME);
style.delimiters = Delimiters::new("[", "]");
style.join = JoinDecl::compact();
style.prefix_words = PrefixWords::new("ref.~", "refs.~")
.with_variant(CAPITAL_VARIANT, PluralWords::new("Ref.~", "Refs.~"));
style
}
fn read_style<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<CounterStyleDecl, D::Error> {
use serde::de::Error;
let written = match serde_json::Value::deserialize(deserializer)? {
serde_json::Value::Object(written) => written,
other => return Err(D::Error::custom(format!(
"the option `style` of the feature `citations` must be a map, not `{other}`"
))),
};
if written.contains_key("name") {
return Err(D::Error::custom(
"the option `style` of the feature `citations` may not set `name`",
));
}
named::read_fields_over(&default_style(), written).map_err(D::Error::custom)
}
impl Default for CitationsConfig {
fn default() -> CitationsConfig {
CitationsConfig {
placement: CitationPlacement::default(),
mark: MarkForm::default(),
style: default_style(),
heading_title: Some("References".to_string()),
heading_level: 2,
note_separator: "; ".to_string(),
}
}
}
impl CitationsConfig {
pub fn new() -> CitationsConfig {
CitationsConfig::default()
}
pub fn placement(mut self, placement: CitationPlacement) -> CitationsConfig {
self.placement = placement;
self
}
pub fn mark(mut self, mark: MarkForm) -> CitationsConfig {
self.mark = mark;
self
}
pub fn style(mut self, style: CounterStyleDecl) -> CitationsConfig {
self.style = style;
self
}
pub fn heading_title(mut self, title: impl Into<String>) -> CitationsConfig {
self.heading_title = Some(title.into());
self
}
pub fn without_heading(mut self) -> CitationsConfig {
self.heading_title = None;
self
}
pub fn heading_level(mut self, level: u8) -> CitationsConfig {
self.heading_level = level;
self
}
pub fn note_separator(mut self, separator: impl Into<String>) -> CitationsConfig {
self.note_separator = separator.into();
self
}
pub fn digest_bytes(&self) -> Vec<u8> {
let CitationsConfig { placement, mark, style, heading_title, heading_level, note_separator } = self;
let mut out = vec![placement.digest_byte(), mark.digest_byte()];
digest_style(&mut out, style);
let text = |out: &mut Vec<u8>, text: &String| {
out.extend_from_slice(&(text.len() as u64).to_le_bytes());
out.extend_from_slice(text.as_bytes());
};
match heading_title {
Some(title) => {
out.push(1);
text(&mut out, title);
}
None => out.push(0),
}
text(&mut out, note_separator);
out.push(*heading_level);
out
}
}
impl Hash for CitationsConfig {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(&self.digest_bytes());
}
}