1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/******************************************************************************
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 25/12/25
******************************************************************************/
//! Macros for creating `Positive` values.
//!
//! This module provides convenient macros for creating `Positive` values
//! with different error handling strategies.
/// Macro for creating a `Positive` value from the given expression.
///
/// Returns `Ok(Positive)` if the value is valid and non-negative,
/// otherwise returns `Err(PositiveError)`.
///
/// # Example
///
/// ```rust
/// use positive::pos;
///
/// let valid = pos!(5.0);
/// assert!(valid.is_ok());
///
/// let invalid = pos!(-5.0);
/// assert!(invalid.is_err());
/// ```
/// Macro for creating a new `Positive` value that panics on invalid input.
///
/// Use this macro when you are certain the value is valid and want to
/// avoid handling the `Result`. For safer alternatives, use `pos!()` which
/// returns `Result<Positive, PositiveError>`.
///
/// # Panics
///
/// This macro will panic if the provided value cannot be converted to a `Positive` value
/// (e.g., negative numbers or values that cannot be represented as `Decimal`).
///
/// # Example
///
/// ```rust
/// use positive::pos_or_panic;
///
/// let value = pos_or_panic!(5.0);
/// assert_eq!(value.to_f64(), 5.0);
/// ```
/// Macro for creating an optional `Positive` value from the given expression.
///
/// Returns `Some(Positive)` if the value is valid and non-negative,
/// otherwise returns `None`. This is useful when you want to ignore errors.
///
/// # Example
///
/// ```rust
/// use positive::spos;
///
/// let valid = spos!(5.0);
/// assert!(valid.is_some());
///
/// let invalid = spos!(-5.0);
/// assert!(invalid.is_none());
/// ```
/// Macro for creating a `StrictlyPositive` value (`> 0`) from an `f64`
/// expression.
///
/// Returns `Ok(StrictlyPositive)` when the value is strictly greater than
/// zero, otherwise `Err(PositiveError)`. The behaviour does not depend on the
/// `non-zero` feature.
///
/// # Example
///
/// ```rust
/// use positive::strict_pos;
///
/// assert!(strict_pos!(5.0).is_ok());
/// assert!(strict_pos!(0.0).is_err());
/// assert!(strict_pos!(-5.0).is_err());
/// ```
/// Macro for creating a `StrictlyPositive` value that panics on invalid
/// input.
///
/// Intended for tests, examples and literals, like [`pos_or_panic!`]. Use
/// [`strict_pos!`] or `StrictlyPositive::new` on production paths.
///
/// # Panics
///
/// Panics when the value is zero, negative, `NaN`, infinite or not
/// representable as a `Decimal`.
///
/// # Example
///
/// ```rust
/// use positive::strict_pos_or_panic;
///
/// let value = strict_pos_or_panic!(5.0);
/// assert_eq!(value.to_f64(), 5.0);
/// ```