//! 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 { 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, }