use std::fmt;
use f_office_xml::NodeId;
use f_office_xml::enumeration::{Values, own};
use crate::ns::W;
use crate::numbering::{Format, format_number, read_format};
use crate::numbers;
use crate::reader::PartReader;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum NoteRestart {
#[default]
Continuous,
EachSection,
EachPage,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NotePosition {
PageBottom,
BeneathText,
SectionEnd,
DocumentEnd,
}
const RESTART: &Values<NoteRestart> = &Values::new(&[
own("continuous", NoteRestart::Continuous),
own("eachSect", NoteRestart::EachSection),
own("eachPage", NoteRestart::EachPage),
]);
const FOOTNOTE_POSITION: &Values<NotePosition> = &Values::new(&[
own("pageBottom", NotePosition::PageBottom),
own("beneathText", NotePosition::BeneathText),
own("sectEnd", NotePosition::SectionEnd),
own("docEnd", NotePosition::DocumentEnd),
]);
const ENDNOTE_POSITION: &Values<NotePosition> = &Values::new(&[
own("sectEnd", NotePosition::SectionEnd),
own("docEnd", NotePosition::DocumentEnd),
]);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NoteSetup {
pub format: Format,
pub start: i64,
pub restart: NoteRestart,
pub position: NotePosition,
}
impl NoteSetup {
pub const FOOTNOTES: Self = Self {
format: Format::Decimal,
start: 1,
restart: NoteRestart::Continuous,
position: NotePosition::PageBottom,
};
pub const ENDNOTES: Self = Self {
format: Format::LowerRoman,
start: 1,
restart: NoteRestart::Continuous,
position: NotePosition::DocumentEnd,
};
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct NoteProps {
pub format: Option<Format>,
pub start: Option<i64>,
pub restart: Option<NoteRestart>,
pub position: Option<NotePosition>,
}
impl NoteProps {
fn read(
reader: &mut PartReader,
node: NodeId,
positions: &'static Values<NotePosition>,
) -> Self {
let doc = reader.doc;
Self {
format: doc
.child(node, W, "numFmt")
.and_then(|n| read_format(reader, n, "val")),
start: doc
.child(node, W, "numStart")
.and_then(|n| reader.number(n, "val", numbers::ANY)),
restart: doc
.child(node, W, "numRestart")
.and_then(|n| reader.enumeration(n, "val", RESTART)),
position: doc
.child(node, W, "pos")
.and_then(|n| reader.enumeration(n, "val", positions)),
}
}
#[must_use]
pub fn over(self, base: NoteSetup) -> NoteSetup {
NoteSetup {
format: self.format.unwrap_or(base.format),
start: self.start.unwrap_or(base.start),
restart: self.restart.unwrap_or(base.restart),
position: self.position.unwrap_or(base.position),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ByNoteKind<T> {
pub footnotes: T,
pub endnotes: T,
}
impl<T> ByNoteKind<T> {
pub const fn get(&self, kind: crate::NoteKind) -> &T {
match kind {
crate::NoteKind::Footnote => &self.footnotes,
crate::NoteKind::Endnote => &self.endnotes,
}
}
}
impl ByNoteKind<NoteSetup> {
pub const DEFAULT: Self = Self {
footnotes: NoteSetup::FOOTNOTES,
endnotes: NoteSetup::ENDNOTES,
};
}
impl Default for ByNoteKind<NoteSetup> {
fn default() -> Self {
Self::DEFAULT
}
}
impl ByNoteKind<NoteProps> {
pub(crate) fn read(reader: &mut PartReader, parent: NodeId) -> Self {
let doc = reader.doc;
let mut read = |name: &str, positions| {
doc.child(parent, W, name)
.map_or_default(|node| NoteProps::read(reader, node, positions))
};
Self {
footnotes: read("footnotePr", FOOTNOTE_POSITION),
endnotes: read("endnotePr", ENDNOTE_POSITION),
}
}
#[must_use]
pub fn over(self, base: ByNoteKind<NoteSetup>) -> ByNoteKind<NoteSetup> {
ByNoteKind {
footnotes: self.footnotes.over(base.footnotes),
endnotes: self.endnotes.over(base.endnotes),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NoteLabel {
pub value: i64,
pub format: Format,
}
impl NoteLabel {
pub const FIRST: Self = Self {
value: 1,
format: Format::Decimal,
};
}
impl fmt::Display for NoteLabel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format_number(self.value, self.format))
}
}
#[cfg(test)]
mod tests {
use f_office_xml::{Document, Limits};
use super::{ByNoteKind, NoteLabel, NotePosition, NoteRestart, NoteSetup};
use crate::ns::W;
use crate::numbering::Format;
use crate::reader::PartReader;
fn read(xml: &str) -> ByNoteKind<super::NoteProps> {
let xml = format!(r#"<w:sectPr xmlns:w="{W}">{xml}</w:sectPr>"#);
let doc = Document::parse(xml.as_bytes(), &Limits::default()).unwrap();
ByNoteKind::read(&mut PartReader::new(&doc), doc.root().unwrap())
}
#[test]
fn value_tables_hold_the_names_of_their_types() {
crate::enumerations::assert_names(super::RESTART, "continuous eachSect eachPage");
crate::enumerations::assert_names(
super::FOOTNOTE_POSITION,
"pageBottom beneathText sectEnd docEnd",
);
crate::enumerations::assert_names(super::ENDNOTE_POSITION, "sectEnd docEnd");
}
#[test]
fn each_element_of_a_section_stands_over_the_settings() {
let settings = read(
r#"<w:footnotePr><w:numFmt w:val="lowerRoman"/><w:numStart w:val="3"/></w:footnotePr><w:endnotePr><w:numFmt w:val="decimal"/></w:endnotePr>"#,
)
.over(ByNoteKind::DEFAULT);
let section = read(
r#"<w:footnotePr><w:numRestart w:val="eachPage"/><w:numStart w:val="2"/><w:pos w:val="beneathText"/></w:footnotePr><w:endnotePr><w:numRestart w:val="eachSect"/><w:pos w:val="sectEnd"/></w:endnotePr>"#,
)
.over(settings);
assert_eq!(
section.footnotes,
NoteSetup {
format: Format::LowerRoman,
start: 2,
restart: NoteRestart::EachPage,
position: NotePosition::BeneathText,
}
);
assert_eq!(
section.endnotes,
NoteSetup {
format: Format::Decimal,
start: 1,
restart: NoteRestart::EachSection,
position: NotePosition::SectionEnd,
}
);
assert_eq!(read("").over(settings), settings);
assert_eq!(read("").over(ByNoteKind::DEFAULT), ByNoteKind::default());
}
#[test]
fn a_label_shows_its_number_in_its_format() {
assert_eq!(NoteLabel::FIRST.to_string(), "1");
let label = NoteLabel {
value: 4,
format: Format::LowerRoman,
};
assert_eq!(label.to_string(), "iv");
}
}