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
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
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
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
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).
Implementations§
Source§impl PositiveError
impl PositiveError
Sourcepub fn invalid_value(value: &str, reason: &str) -> PositiveError
pub fn invalid_value(value: &str, reason: &str) -> PositiveError
Creates a new InvalidValue error.
§Arguments
value- The problematic input, rendered exactly as suppliedreason- 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");Sourcepub fn arithmetic_error(operation: &str, reason: &str) -> PositiveError
pub fn arithmetic_error(operation: &str, reason: &str) -> PositiveError
Creates a new ArithmeticError error.
§Arguments
operation- The name of the operation that failedreason- 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"
);Sourcepub fn conversion_error(
from_type: &str,
to_type: &str,
reason: &str,
) -> PositiveError
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 fromto_type- The destination type being converted toreason- 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"
);Sourcepub fn out_of_bounds(
value: Decimal,
min: Decimal,
max: Decimal,
) -> PositiveError
pub fn out_of_bounds( value: Decimal, min: Decimal, max: Decimal, ) -> PositiveError
Creates a new OutOfBounds error.
§Arguments
value- The out-of-bounds value, exactmin- The lower bound (inclusive) of the valid rangemax- 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"));Sourcepub fn invalid_precision(precision: u32, reason: &str) -> PositiveError
pub fn invalid_precision(precision: u32, reason: &str) -> PositiveError
Creates a new InvalidPrecision error.
§Arguments
precision- The problematic precision, in decimal placesreason- 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
impl Clone for PositiveError
Source§fn clone(&self) -> PositiveError
fn clone(&self) -> PositiveError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PositiveError
impl Debug for PositiveError
Source§impl Display for PositiveError
impl Display for PositiveError
impl Eq for PositiveError
Source§impl Error for PositiveError
impl Error for PositiveError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<PositiveError> for ExpirationDateError
impl From<PositiveError> for ExpirationDateError
Source§fn from(source: PositiveError) -> ExpirationDateError
fn from(source: PositiveError) -> ExpirationDateError
Source§impl From<PositiveError> for PositionError
impl From<PositiveError> for PositionError
Source§fn from(source: PositiveError) -> Self
fn from(source: PositiveError) -> Self
Source§impl PartialEq for PositiveError
impl PartialEq for PositiveError
impl StructuralPartialEq for PositiveError
Auto Trait Implementations§
impl Freeze for PositiveError
impl RefUnwindSafe for PositiveError
impl Send for PositiveError
impl Sync for PositiveError
impl Unpin for PositiveError
impl UnsafeUnpin for PositiveError
impl UnwindSafe for PositiveError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.