Repository: typst/typst
File: crates/typst/src/foundations/content.rs (Rust)

Code before:
    }
}

impl PartialEq for StyledElem {
    fn eq(&self, other: &Self) -> bool {
        self.child == other.child
    }
}

impl Repr for StyledElem {
    fn repr(&self) -> EcoString {
        eco_format!("styled(child: {}, ..)", self.child.repr())
    }
}

/// Tries to extract the plain-text representation of the element.
pub trait PlainText {
    /// Write this element's plain text into the given buffer.
    fn plain_text(&self, text: &mut EcoString);
}

/// An error arising when trying to access a field of content.
#[derive(Copy, Clone, Debug)]
pub enum FieldAccessError {
    MissingField,
    MissingFieldNoDefault,
    FieldNotSet,
    Internal,
}

impl FieldAccessError {
    /// Gets the error message given a field name.
    #[cold]
    pub fn get_message(self, field: &str) -> EcoString {
        match self {
            FieldAccessError::MissingField => {
                eco_format!("content does not contain field {}", field.repr())
            }
            FieldAccessError::MissingFieldNoDefault => {
                eco_format!(
                    "content does not contain field {} and \
                 no default value was specified",
                    field.repr()
                )
            }
            FieldAccessError::FieldNotSet => {
                eco_format!("field {} in content is not set at this point", field.repr())
            }
            FieldAccessError::Internal => {
                eco_format!(
                    "internal error when accessing field {} in content – this is a bug",

Review comment (suggestion):
Does the no default really only make sense for `MissingField`? Isn't it essentially a separate thing? We could also have the field not set at this point and no default. Perhaps we should just amend `" and no default value was specified"` in the relevant function and remove the variant.

Code after:
    }
}

impl PartialEq for StyledElem {
    fn eq(&self, other: &Self) -> bool {
        self.child == other.child
    }
}

impl Repr for StyledElem {
    fn repr(&self) -> EcoString {
        eco_format!("styled(child: {}, ..)", self.child.repr())
    }
}

/// Tries to extract the plain-text representation of the element.
pub trait PlainText {
    /// Write this element's plain text into the given buffer.
    fn plain_text(&self, text: &mut EcoString);
}

/// An error arising when trying to access a field of content.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum FieldAccessError {
    Unknown,
    Unset,
    Internal,
}

impl FieldAccessError {
    /// Formats the error message given the content and the field name.
    #[cold]
    pub fn message(self, content: &Content, field: &str) -> EcoString {
        let elem_name = content.elem().name();
        match self {
            FieldAccessError::Unknown => {
                eco_format!("{elem_name} does not have field {}", field.repr())
            }
            FieldAccessError::Unset => {
                eco_format!(
                    "field {} in {elem_name} is not known at this point",
                    field.repr()
                )
            }
            FieldAccessError::Internal => {
                eco_format!(
                    "internal error when accessing field {} in {elem_name} – this is a bug",
                    field.repr()
                )
            }
        }