From cc30a537284871d668911353bd121e38d0353eb0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 16 May 2023 21:59:46 +0200 Subject: refactor: reorganize structs --- src/description/part.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/description/part.rs (limited to 'src/description/part.rs') diff --git a/src/description/part.rs b/src/description/part.rs new file mode 100644 index 0000000..2f1771f --- /dev/null +++ b/src/description/part.rs @@ -0,0 +1,104 @@ +use crate::gloss_list::{Error as GlossListError, GlossList}; +use crate::itemized_list::{Error as ItemizedListError, ItemizedList}; +use crate::paragraph::{Error as ParagraphError, Paragraph}; +use crate::table::{Error as TableError, Informal, Table}; +use crate::variable_list::{Error as VariableListError, VariableList}; +use crate::xml::element::{FromElements, Tagged}; + +/// Description part. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Part +{ + /// Paragraph. + Paragraph(Paragraph), + + /// Variable list. + VariableList(VariableList), + + /// Program listing. + ProgramListing(String), + + /// Informal table. + InformalTable(Informal), + + /// Itemized list. + ItemizedList(ItemizedList), + + /// Table. + Table(Table), + + /// Gloss list. + GlossList(GlossList), +} + +impl Part +{ + pub(crate) fn from_tagged_element(tagged_element: &Tagged) -> Result + { + match tagged_element.name() { + "para" => Paragraph::from_elements(tagged_element.child_elements()) + .map(Part::Paragraph) + .map_err(Error::InvalidParagraph), + "variablelist" => { + VariableList::from_elements(tagged_element.child_elements()) + .map(Part::VariableList) + .map_err(Error::InvalidVariableList) + } + "programlisting" => Ok(Part::ProgramListing( + tagged_element + .child_elements() + .get_first_text_element() + .cloned() + .unwrap_or_default(), + )), + "informaltable" => Informal::from_elements(tagged_element.child_elements()) + .map(Part::InformalTable) + .map_err(Error::InvalidInformalTable), + "itemizedlist" => { + ItemizedList::from_elements(tagged_element.child_elements()) + .map(Part::ItemizedList) + .map_err(Error::InvalidItemizedList) + } + "table" => Table::from_elements(tagged_element.child_elements()) + .map(Part::Table) + .map_err(Error::InvalidTable), + "glosslist" => GlossList::from_elements(tagged_element.child_elements()) + .map(Part::GlossList) + .map_err(Error::InvalidGlossList), + name => Err(Error::Unknown(name.to_string())), + } + } +} + +/// [`Part`] error. +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + /// Unknown description part. + #[error("Unknown description part '{0}'")] + Unknown(String), + + /// Invalid paragraph. + #[error("Invalid paragraph")] + InvalidParagraph(#[source] ParagraphError), + + /// Invalid variable list. + #[error("Invalid variable list")] + InvalidVariableList(#[source] VariableListError), + + /// Invalid informal table. + #[error("Invalid informal table")] + InvalidInformalTable(#[source] TableError), + + /// Invalid itemized list. + #[error("Invalid itemized list")] + InvalidItemizedList(#[source] ItemizedListError), + + /// Invalid table. + #[error("Invalid table")] + InvalidTable(#[source] TableError), + + /// Invalid gloss list. + #[error("Invalid gloss list")] + InvalidGlossList(#[source] GlossListError), +} -- cgit v1.2.3-18-g5258