diff options
Diffstat (limited to 'src/emphasis.rs')
-rw-r--r-- | src/emphasis.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/emphasis.rs b/src/emphasis.rs new file mode 100644 index 0000000..12502ad --- /dev/null +++ b/src/emphasis.rs @@ -0,0 +1,66 @@ +//! Emphasis. +use crate::xml::element::Tagged; + +/// Emphasis. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Emphasis +{ + /// The emphasised text. + pub text: String, + + /// Emphasis role. + pub role: Role, +} + +impl Emphasis +{ + pub(crate) fn from_tagged_element(tagged_element: &Tagged) -> Result<Self, Error> + { + return Ok(Emphasis { + text: tagged_element + .child_elements() + .get_first_text_element() + .cloned() + .unwrap_or_default(), + role: tagged_element + .attributes() + .iter() + .find(|attr| attr.key == "role") + .map(|attr| { + let value = String::from_utf8(attr.value.clone()) + .map_err(|_| Error::EmphasisRoleNotUTF8)?; + + if value == "bold" { + return Ok(Role::Bold); + } + + Err(Error::UnknownEmphasisRole(value)) + }) + .unwrap_or(Ok(Role::None))?, + }); + } +} + +/// [`Emphasis`] error. +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + /// Emphasis role is not valid UTF-8. + #[error("Emphasis role is not valid UTF-8")] + EmphasisRoleNotUTF8, + + /// Unknown emphasis role. + #[error("Unknown emphasis role '{0}'")] + UnknownEmphasisRole(String), +} + +/// Emphasis role. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Role +{ + /// Bold. + Bold, + + /// None. + None, +} |