Skip to main content

PositiveError

Enum PositiveError 

Source
pub enum PositiveError {
    InvalidValue {
        value: String,
        reason: String,
    },
    ArithmeticError {
        operation: String,
        reason: String,
    },
    ConversionError {
        from_type: String,
        to_type: String,
        reason: String,
    },
    OutOfBounds {
        value: Decimal,
        min: Decimal,
        max: Decimal,
    },
    InvalidPrecision {
        precision: u32,
        reason: String,
    },
}
Expand description

Positive, the non-negative decimal newtype, its error and the is_positive::<T>() type check. Defined by the standalone positive crate and re-exported unchanged: a value obtained through this path is a positive::Positive (see the crate-level “Foundational types” table). Represents errors that can occur during positive decimal operations.

This enum provides a structured way to handle various error conditions that may arise when working with positive decimal values, including validation, arithmetic operations, conversions, and precision issues.

See the module documentation for the stable variant set and the rationale behind the field types.

§Examples

use positive::{Positive, PositiveError};

let err = Positive::new(-1.0).unwrap_err();
assert!(matches!(err, PositiveError::OutOfBounds { .. }));
assert!(err.to_string().starts_with("value -1 is out of bounds"));

Variants§

§

InvalidValue

Error when attempting to create a positive decimal from an invalid value.

Occurs when a value cannot be interpreted as a decimal at all — NaN, ±inf, or text that does not parse. Values that are well-formed decimals but fall outside the permitted range produce PositiveError::OutOfBounds instead.

value holds the offending input rendered exactly as it was supplied.

Fields

§value: String

The problematic input, rendered exactly as supplied.

§reason: String

Detailed explanation of why the value is invalid.

§

ArithmeticError

Error when performing decimal arithmetic operations.

Occurs during mathematical operations such as addition, subtraction, multiplication, or division when the operation cannot be completed correctly (e.g., division by zero, overflow, result would be negative).

Fields

§operation: String

The operation that failed (e.g., “subtraction”, “division”).

§reason: String

Detailed explanation of why the operation failed.

§

ConversionError

Error when converting between decimal types.

Occurs when a value is well-formed but cannot be represented in the destination type — for example a Decimal larger than u64::MAX, or an f64 outside the range Decimal can hold.

Fields

§from_type: String

The source type being converted from.

§to_type: String

The destination type being converted to.

§reason: String

Detailed explanation of why the conversion failed.

§

OutOfBounds

Error when a decimal value falls outside the permitted range.

All three fields are exact Decimal values. min is the minimum of the type being built: 0 for Positive, and 1e-28 (the smallest strictly positive Decimal) for StrictlyPositive.

Fields

§value: Decimal

The value that is out of bounds.

§min: Decimal

The minimum acceptable value, inclusive.

§max: Decimal

The maximum acceptable value, inclusive.

§

InvalidPrecision

Error when decimal precision is invalid.

Occurs when an operation specifies a number of decimal places outside the range rust_decimal::Decimal supports (0 through 28 inclusive).

Fields

§precision: u32

The problematic precision value, in decimal places.

§reason: String

Detailed explanation of why the precision is invalid.

Implementations§

Source§

impl PositiveError

Source

pub fn invalid_value(value: &str, reason: &str) -> PositiveError

Creates a new InvalidValue error.

§Arguments
  • value - The problematic input, rendered exactly as supplied
  • reason - Explanation of why the value is invalid
§Returns

A new PositiveError::InvalidValue instance

§Examples
use positive::PositiveError;

let err = PositiveError::invalid_value("NaN", "not a number");
assert_eq!(err.to_string(), "invalid positive value 'NaN': not a number");
Source

pub fn arithmetic_error(operation: &str, reason: &str) -> PositiveError

Creates a new ArithmeticError error.

§Arguments
  • operation - The name of the operation that failed
  • reason - Explanation of why the operation failed
§Returns

A new PositiveError::ArithmeticError instance

§Examples
use positive::PositiveError;

let err = PositiveError::arithmetic_error("division", "division by zero");
assert_eq!(
    err.to_string(),
    "arithmetic error during division: division by zero"
);
Source

pub fn conversion_error( from_type: &str, to_type: &str, reason: &str, ) -> PositiveError

Creates a new ConversionError error.

§Arguments
  • from_type - The source type being converted from
  • to_type - The destination type being converted to
  • reason - Explanation of why the conversion failed
§Returns

A new PositiveError::ConversionError instance

§Examples
use positive::PositiveError;

let err = PositiveError::conversion_error("Positive", "u64", "value exceeds u64::MAX");
assert_eq!(
    err.to_string(),
    "failed to convert from Positive to u64: value exceeds u64::MAX"
);
Source

pub fn out_of_bounds( value: Decimal, min: Decimal, max: Decimal, ) -> PositiveError

Creates a new OutOfBounds error.

§Arguments
  • value - The out-of-bounds value, exact
  • min - The lower bound (inclusive) of the valid range
  • max - The upper bound (inclusive) of the valid range
§Returns

A new PositiveError::OutOfBounds instance

§Examples
use positive::PositiveError;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;

let err = PositiveError::out_of_bounds(dec!(-5), Decimal::ZERO, Decimal::MAX);
assert!(err.to_string().starts_with("value -5 is out of bounds"));
Source

pub fn invalid_precision(precision: u32, reason: &str) -> PositiveError

Creates a new InvalidPrecision error.

§Arguments
  • precision - The problematic precision, in decimal places
  • reason - Explanation of why the precision is invalid
§Returns

A new PositiveError::InvalidPrecision instance

§Examples
use positive::PositiveError;

let err = PositiveError::invalid_precision(29, "decimal supports at most 28 places");
assert_eq!(
    err.to_string(),
    "invalid precision 29: decimal supports at most 28 places"
);

Trait Implementations§

Source§

impl Clone for PositiveError

Source§

fn clone(&self) -> PositiveError

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 PositiveError

Source§

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

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

impl Display for PositiveError

Source§

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

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

impl Eq for PositiveError

Source§

impl Error for PositiveError

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 From<PositiveError> for ExpirationDateError

Source§

fn from(source: PositiveError) -> ExpirationDateError

Converts to this type from the input type.
Source§

impl From<PositiveError> for PositionError

Source§

fn from(source: PositiveError) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for PositiveError

Source§

fn eq(&self, other: &PositiveError) -> 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 PositiveError

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more