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

Code before:

/// Format a user-facing error message for an XML-like file format.
pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> LoadError {
    let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize);
    let message = match error {
        roxmltree::Error::UnexpectedCloseTag(expected, actual, _) => {
            eco_format!(
                "failed to parse {format} (found closing tag '{actual}' instead of '{expected}')"
            )
        }
        roxmltree::Error::UnknownEntityReference(entity, _) => {
            eco_format!("failed to parse {format} (unknown entity '{entity}')")
        }
        roxmltree::Error::DuplicatedAttribute(attr, _) => {
            eco_format!("failed to parse {format} (duplicate attribute '{attr}')")
        }
        roxmltree::Error::NoRootNode => {
            eco_format!("failed to parse {format} (missing root node)")
        }
        err => eco_format!("failed to parse {format} ({err})"),
    };

    LoadError { pos: pos.into(), message }
}

/// Assert a condition, on failuer generate an internal compiler error with the
/// provided message.
#[track_caller]
pub fn assert_internal(cond: bool, msg: &str) -> HintedStrResult<()> {
    if !cond { Err(internal_error(msg)) } else { Ok(()) }
}

pub trait ExpectInternal<T> {
    /// Analog to [`Option::expect`] but instead of panicking generate an
    /// internal compiler error.
    fn expect_internal(self, msg: &str) -> HintedStrResult<T>;
}

impl<T> ExpectInternal<T> for Option<T> {
    #[track_caller]
    fn expect_internal(self, msg: &str) -> HintedStrResult<T> {
        self.ok_or_else(|| internal_error(msg))
    }
}

#[track_caller]
fn internal_error(msg: &str) -> HintedString {
    let loc = std::panic::Location::caller();
    let mut error = error!(
        "internal error: {msg}";
        hint: "please report this as a bug";

Review comment (bug):
```suggestion
/// Assert a condition, on failure generate an internal compiler error with the
```


Code after:

/// Format a user-facing error message for an XML-like file format.
pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> LoadError {
    let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize);
    let message = match error {
        roxmltree::Error::UnexpectedCloseTag(expected, actual, _) => {
            eco_format!(
                "failed to parse {format} (found closing tag '{actual}' instead of '{expected}')"
            )
        }
        roxmltree::Error::UnknownEntityReference(entity, _) => {
            eco_format!("failed to parse {format} (unknown entity '{entity}')")
        }
        roxmltree::Error::DuplicatedAttribute(attr, _) => {
            eco_format!("failed to parse {format} (duplicate attribute '{attr}')")
        }
        roxmltree::Error::NoRootNode => {
            eco_format!("failed to parse {format} (missing root node)")
        }
        err => eco_format!("failed to parse {format} ({err})"),
    };

    LoadError { pos: pos.into(), message }
}

/// Asserts a condition, generating an internal compiler error with the provided
/// message on failure.
#[track_caller]
pub fn assert_internal(cond: bool, msg: &str) -> HintedStrResult<()> {
    if !cond { Err(internal_error(msg)) } else { Ok(()) }
}

/// Generates an internal compiler error with the provided message.
#[track_caller]
pub fn panic_internal(msg: &str) -> HintedStrResult<()> {
    Err(internal_error(msg))
}

/// Adds a method analogous to [`Option::expect`] that raises an internal
/// compiler error instead of panicking.
pub trait ExpectInternal<T> {
    /// Extracts the value, producing an internal error if `self` is `None`.
    fn expect_internal(self, msg: &str) -> HintedStrResult<T>;
}

impl<T> ExpectInternal<T> for Option<T> {
    #[track_caller]
    fn expect_internal(self, msg: &str) -> HintedStrResult<T> {
        match self {
            Some(val) => Ok(val),
            None => Err(internal_error(msg)),