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

Code before:
{
    fn at(self, span: Span) -> SourceResult<T> {
        self.map_err(|message| {
            let mut diagnostic = SourceDiagnostic::error(span, message);
            if diagnostic.message.contains("(access denied)") {
                diagnostic.hint("cannot read file outside of project root");
                diagnostic
                    .hint("you can adjust the project root with the --root argument");
            }
            eco_vec![diagnostic]
        })
    }
}

/// A result type with a string error message and hints.
pub type HintedStrResult<T> = Result<T, HintedString>;

/// A string message with hints.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct HintedString {
    /// The string's components as a single vector of strings.
    /// The first element of the vector contains the message.
    /// The remaining elements are the hints.
    /// This is done to reduce the size of a HintedString.
    /// The vector is guaranteed to not be empty.
    components: EcoVec<EcoString>,
}

impl HintedString {
    /// Creates a new hinted string with the given message and hints.
    pub fn new(message: EcoString, hints: impl IntoIterator<Item = EcoString>) -> Self {
        Self {
            components: std::iter::once(message).chain(hints).collect(),
        }
    }

    /// A diagnostic message describing the problem.
    pub fn message(&self) -> &EcoString {
        self.components.first().unwrap()
    }

    /// Additional hints to the user, indicating how this error could be avoided
    /// or worked around.
    pub fn hints(&self) -> &[EcoString] {
        self.components.get(1..).unwrap_or(&[])
    }

    /// Add another hint to this hinted string.
    pub fn add_hint(&mut self, hint: EcoString) {
        self.components.push(hint);
    }

Review comment (suggestion):
Since it's just a single field, we can use `pub struct HintedString(EcoVec<EcoString>);`

Code after:
}

impl<T, S> At<T> for Result<T, S>
where
    S: Into<EcoString>,
{
    fn at(self, span: Span) -> SourceResult<T> {
        self.map_err(|message| {
            let mut diagnostic = SourceDiagnostic::error(span, message);
            if diagnostic.message.contains("(access denied)") {
                diagnostic.hint("cannot read file outside of project root");
                diagnostic
                    .hint("you can adjust the project root with the --root argument");
            }
            eco_vec![diagnostic]
        })
    }
}

/// A result type with a string error message and hints.
pub type HintedStrResult<T> = Result<T, HintedString>;

/// A string message with hints.
///
/// This is internally represented by a vector of strings.
/// The first element of the vector contains the message.
/// The remaining elements are the hints.
/// This is done to reduce the size of a HintedString.
/// The vector is guaranteed to not be empty.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct HintedString(EcoVec<EcoString>);

impl HintedString {
    /// Creates a new hinted string with the given message.
    pub fn new(message: EcoString) -> Self {
        Self(eco_vec![message])
    }

    /// A diagnostic message describing the problem.
    pub fn message(&self) -> &EcoString {
        self.0.first().unwrap()
    }

    /// Additional hints to the user, indicating how this error could be avoided
    /// or worked around.
    pub fn hints(&self) -> &[EcoString] {
        self.0.get(1..).unwrap_or(&[])
    }

    /// Adds a single hint to the hinted string.
    pub fn hint(&mut self, hint: impl Into<EcoString>) {