diff options
author | HampusM <hampus@hampusmat.com> | 2023-05-16 21:59:46 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-05-16 21:59:46 +0200 |
commit | cc30a537284871d668911353bd121e38d0353eb0 (patch) | |
tree | 121976a44346f5979447d9d9052372246d4ff66c /src/emphasis.rs | |
parent | b336066d6067a0eb9ff9fc34c5aa062b86e56c62 (diff) |
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, +} |