Skip to main content

Error

Enum Error 

Source
#[non_exhaustive]
pub enum Error {
Show 13 variants Parse { input: String, position: usize, message: String, }, InvalidNumber { input: String, }, UnknownUnit { symbol: String, suggestions: Vec<String>, }, AmbiguousSymbol { symbol: String, candidates: Vec<String>, }, AmbiguousExpression { input: String, message: String, }, DimensionMismatch { from: Dim, to: Dim, }, KindMismatch { from: Kind, to: Kind, operation: &'static str, suggestions: Vec<String>, }, Overflow { operation: &'static str, }, DivisionByZero, LengthMismatch { input: usize, output: usize, }, Refused { reason: String, suggestions: Vec<String>, }, InvalidDefinition { symbol: String, message: String, }, DuplicateSymbol { symbol: String, },
}
Expand description

Everything that can go wrong in dimvexa. Library code never panics: every fallible operation returns this type.

Each variant has a stable, documented machine-readable code (see Error::code and Error::ALL_CODES). The enum is #[non_exhaustive]: new variants may be added in minor releases, codes are never renamed within a schema version.

§Examples

use dimvexa_core::{Error, Ratio};

let err = Ratio::new(1, 0).unwrap_err();
assert_eq!(err.code(), "division_by_zero");
assert!(matches!(err, Error::DivisionByZero));

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Parse

Syntax error in a unit expression or number. Code parse_error.

Fields

§input: String

The complete input that failed to parse.

§position: usize

Zero-based character position of the problem.

§message: String

Human-readable explanation.

§

InvalidNumber

A number could not be read. Code invalid_number.

Fields

§input: String

The text that is not a valid number.

§

UnknownUnit

The symbol is not in the registry. Code unknown_unit.

Fields

§symbol: String

The unknown symbol.

§suggestions: Vec<String>

Close matches, for example the same text with different capitalisation.

§

AmbiguousSymbol

The bare symbol has several meanings and no profile resolves it. Code ambiguous_symbol.

Fields

§symbol: String

The ambiguous symbol as written.

§candidates: Vec<String>

Explicit unit symbols the caller may use instead.

§

AmbiguousExpression

The expression is syntactically valid but its meaning depends on operator precedence conventions. Code ambiguous_expression.

Fields

§input: String

The offending expression.

§message: String

Explanation including how to write it unambiguously.

§

DimensionMismatch

The two units measure different physical dimensions. Code dimension_mismatch.

Fields

§from: Dim

Dimension of the source.

§to: Dim

Dimension of the target.

§

KindMismatch

The two units have the same dimension but incompatible kinds, for example an absolute temperature and a temperature difference. Code kind_mismatch.

Fields

§from: Kind

Kind of the left or source operand.

§to: Kind

Kind of the right or target operand.

§operation: &'static str

What was attempted.

§suggestions: Vec<String>

Units to use instead, for example delta_K for a temperature difference.

§

Overflow

A checked integer operation overflowed. Code overflow.

Fields

§operation: &'static str

The operation that overflowed.

§

DivisionByZero

Division by zero in exact arithmetic. Code division_by_zero.

§

LengthMismatch

Input and output slices have different lengths. Code length_mismatch.

Fields

§input: usize

Length of the input slice.

§output: usize

Length of the output slice.

§

Refused

The operation is deliberately not supported because it would require guessing. Code refused.

Fields

§reason: String

Why the operation is refused and what to do instead.

§suggestions: Vec<String>

Units or expressions to use instead (may be empty).

§

InvalidDefinition

A unit definition is inconsistent. Code invalid_definition.

Fields

§symbol: String

Symbol of the offending definition.

§message: String

What is wrong.

§

DuplicateSymbol

A symbol or alias is registered twice. Code duplicate_symbol.

Fields

§symbol: String

The symbol that is already taken.

Implementations§

Source§

impl Error

Source

pub const ALL_CODES: &'static [&'static str]

Every stable machine-readable error code, in declaration order.

Source

pub const fn code(&self) -> &'static str

The stable machine-readable code of this error.

§Examples
use dimvexa_core::Error;
let e = Error::Overflow { operation: "mul" };
assert_eq!(e.code(), "overflow");
assert!(Error::ALL_CODES.contains(&e.code()));
Source

pub fn suggestions(&self) -> &[String]

Everything the error recommends instead: close matches for an unknown unit, the explicit symbols for an ambiguous one, and the difference unit (delta_K, …) for a refused or mismatched temperature operation. Empty if the error has no recommendation.

§Examples
use dimvexa_core::Error;
let e = Error::UnknownUnit { symbol: "PSI".into(), suggestions: vec!["psi".into()] };
assert_eq!(e.suggestions(), ["psi"]);
assert!(Error::DivisionByZero.suggestions().is_empty());
Source

pub fn candidates(&self) -> &[String]

Candidate symbols attached to the error, if any (ambiguous or unknown symbols).

Trait Implementations§

Source§

impl Clone for Error

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Error

Source§

impl Error for Error

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for Error

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Error

Auto Trait Implementations§

§

impl Forget for Error

§

impl Freeze for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnsafeUnpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.