fn fmt()

in rhai/src/types/error.rs [129:265]


    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ErrorSystem(s, err) if s.is_empty() => write!(f, "{err}")?,
            Self::ErrorSystem(s, err) => write!(f, "{s}: {err}")?,

            Self::ErrorParsing(p, ..) => write!(f, "Syntax error: {p}")?,

            #[cfg(not(feature = "no_function"))]
            Self::ErrorInFunctionCall(s, src, err, ..) if crate::parser::is_anonymous_fn(s) => {
                write!(f, "{err}\nin closure call")?;
                if !src.is_empty() {
                    write!(f, " @ '{src}'")?;
                }
            }
            Self::ErrorInFunctionCall(s, src, err, ..) => {
                write!(f, "{err}\nin call to function '{s}'")?;
                if !src.is_empty() {
                    write!(f, " @ '{src}'")?;
                }
            }

            Self::ErrorInModule(s, err, ..) if s.is_empty() => write!(f, "{err}\nin module")?,
            Self::ErrorInModule(s, err, ..) => write!(f, "{err}\nin module '{s}'")?,

            Self::ErrorVariableExists(s, ..) => write!(f, "Variable already defined: {s}")?,
            Self::ErrorForbiddenVariable(s, ..) => write!(f, "Forbidden variable name: {s}")?,
            Self::ErrorVariableNotFound(s, ..) => write!(f, "Variable not found: {s}")?,
            Self::ErrorPropertyNotFound(s, ..) => write!(f, "Property not found: {s}")?,
            Self::ErrorIndexNotFound(s, ..) => write!(f, "Invalid index: {s}")?,
            Self::ErrorFunctionNotFound(s, ..) => write!(f, "Function not found: {s}")?,
            Self::ErrorModuleNotFound(s, ..) => write!(f, "Module not found: {s}")?,
            Self::ErrorDataRace(s, ..) => write!(f, "Data race detected on variable '{s}'")?,

            Self::ErrorDotExpr(s, ..) if s.is_empty() => f.write_str("Malformed dot expression")?,
            Self::ErrorDotExpr(s, ..) => f.write_str(s)?,

            Self::ErrorIndexingType(s, ..) => write!(f, "Indexer unavailable: {s}")?,
            Self::ErrorUnboundThis(..) => f.write_str("'this' not bound")?,
            Self::ErrorFor(..) => f.write_str("For loop expects iterable type")?,
            Self::ErrorTooManyOperations(..) => f.write_str("Too many operations")?,
            Self::ErrorTooManyModules(..) => f.write_str("Too many modules imported")?,
            Self::ErrorStackOverflow(..) => f.write_str("Stack overflow")?,
            Self::ErrorTerminated(..) => f.write_str("Script terminated")?,

            Self::ErrorRuntime(d, ..) if d.is_unit() => f.write_str("Runtime error")?,
            Self::ErrorRuntime(d, ..)
                if d.read_lock::<ImmutableString>()
                    .map_or(false, |v| v.is_empty()) =>
            {
                write!(f, "Runtime error")?
            }
            Self::ErrorRuntime(d, ..) => write!(f, "Runtime error: {d}")?,

            #[cfg(not(feature = "no_object"))]
            Self::ErrorNonPureMethodCallOnConstant(s, ..)
                if s.starts_with(crate::engine::FN_GET) =>
            {
                let prop = &s[crate::engine::FN_GET.len()..];
                write!(f, "Non-pure property {prop} cannot be accessed on constant")?
            }
            #[cfg(not(feature = "no_object"))]
            Self::ErrorNonPureMethodCallOnConstant(s, ..)
                if s.starts_with(crate::engine::FN_SET) =>
            {
                let prop = &s[crate::engine::FN_SET.len()..];
                write!(f, "Cannot modify property '{prop}' of constant")?
            }
            #[cfg(not(feature = "no_index"))]
            Self::ErrorNonPureMethodCallOnConstant(s, ..) if s == crate::engine::FN_IDX_GET => {
                write!(f, "Non-pure indexer cannot be accessed on constant")?
            }
            #[cfg(not(feature = "no_index"))]
            Self::ErrorNonPureMethodCallOnConstant(s, ..) if s == crate::engine::FN_IDX_SET => {
                write!(f, "Cannot assign to indexer of constant")?
            }
            Self::ErrorNonPureMethodCallOnConstant(s, ..) => {
                write!(f, "Non-pure method '{s}' cannot be called on constant")?
            }

            Self::ErrorAssignmentToConstant(s, ..) => write!(f, "Cannot modify constant {s}")?,
            Self::ErrorMismatchOutputType(e, a, ..) => match (a.as_str(), e.as_str()) {
                ("", e) => write!(f, "Output type incorrect, expecting {e}"),
                (a, "") => write!(f, "Output type incorrect: {a}"),
                (a, e) => write!(f, "Output type incorrect: {a} (expecting {e})"),
            }?,
            Self::ErrorMismatchDataType(e, a, ..) => match (a.as_str(), e.as_str()) {
                ("", e) => write!(f, "Data type incorrect, expecting {e}"),
                (a, "") => write!(f, "Data type incorrect: {a}"),
                (a, e) => write!(f, "Data type incorrect: {a} (expecting {e})"),
            }?,

            Self::ErrorArithmetic(s, ..) if s.is_empty() => f.write_str("Arithmetic error")?,
            Self::ErrorArithmetic(s, ..) => f.write_str(s)?,

            Self::LoopBreak(true, ..) => f.write_str("'break' must be within a loop")?,
            Self::LoopBreak(false, ..) => f.write_str("'continue' must be within a loop")?,

            Self::Return(..) => f.write_str("NOT AN ERROR - function returns value")?,

            Self::ErrorArrayBounds(max, index, ..) => match max {
                0 => write!(f, "Array index {index} out of bounds: array is empty"),
                1 => write!(
                    f,
                    "Array index {index} out of bounds: only 1 element in array",
                ),
                _ => write!(
                    f,
                    "Array index {index} out of bounds: only {max} elements in array",
                ),
            }?,
            Self::ErrorStringBounds(max, index, ..) => match max {
                0 => write!(f, "String index {index} out of bounds: string is empty"),
                1 => write!(
                    f,
                    "String index {index} out of bounds: only 1 character in string",
                ),
                _ => write!(
                    f,
                    "String index {index} out of bounds: only {max} characters in string",
                ),
            }?,
            Self::ErrorBitFieldBounds(max, index, ..) => write!(
                f,
                "Bit-field index {index} out of bounds: only {max} bits in bit-field",
            )?,
            Self::ErrorDataTooLarge(typ, ..) => write!(f, "{typ} too large")?,

            Self::ErrorCustomSyntax(s, tokens, ..) => write!(f, "{s}: {}", tokens.join(" "))?,
        }

        // Do not write any position if None
        if !self.position().is_none() {
            write!(f, " ({})", self.position())?;
        }

        Ok(())
    }