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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Option type arithmetic operations module
//!
//! Generates arithmetic operations for `Option<T>` types, supporting `Lhs op Option<Rhs>` pattern.
//!
//! # Orphan Rule Limitations
//!
//! Due to Rust's orphan rule, we cannot implement external traits for the external type `Option<T>`,
//! therefore the following patterns are **not implementable**:
//!
//! - `impl Add<Rhs> for Option<Lhs>` - `Option<Lhs> op Rhs`
//! - `impl Add<Option<Rhs>> for Option<Lhs>` - `Option<Lhs> op Option<Rhs>`
//! - `impl Neg for Option<T>` - `Option` negation operation
//!
//! # Feasible Pattern
//!
//! ✅ **Only feasible pattern**: `impl Add<Option<Rhs>> for Lhs` - `Lhs op Option<Rhs>`
//!
//! # Alternatives for Non-Implementable Patterns
//!
//! ## Negation Operation
//!
//! Use the `.map()` method:
//!
//! ```text
//! use strict_num_extended::*;
//!
//! let a: Option<PositiveF64> = Some(PositiveF64::new_const(5.0));
//! let neg: Option<NegativeF64> = a.map(|x| -x);
//! assert!(neg.is_some());
//! assert_eq!(neg.unwrap().get(), -5.0);
//!
//! // Handle None
//! let none: Option<PositiveF64> = None;
//! let neg_none: Option<NegativeF64> = none.map(|x| -x);
//! assert!(neg_none.is_none());
//! ```
//!
//! ## `Option<Lhs>` op Rhs
//!
//! Use `.map()` or `.and_then()`:
//!
//! ```text
//! let a: Option<PositiveF64> = Some(PositiveF64::new_const(5.0));
//! let b: NegativeF64 = NegativeF64::new_const(-3.0);
//!
//! // Use map
//! let result: Option<FinF64> = a.map(|a_val| a_val + b);
//! assert!(result.is_some());
//!
//! // Handle None
//! let none: Option<PositiveF64> = None;
//! let result_none: Option<FinF64> = none.map(|a_val| a_val + b);
//! assert!(result_none.is_none());
//! ```
//!
//! ## `Option<Lhs>` op `Option<Rhs>`
//!
//! Use pattern matching or combinators:
//!
//! ```text
//! let a: Option<PositiveF64> = Some(PositiveF64::new_const(5.0));
//! let b: Option<NegativeF64> = Some(NegativeF64::new_const(-3.0));
//!
//! // Use pattern matching
//! let result = match (a, b) {
//! (Some(a_val), Some(b_val)) => Some(a_val + b_val),
//! _ => None,
//! };
//! assert!(result.is_some());
//!
//! // Or use combinators from libraries like itertools
//! ```
use TokenStream as TokenStream2;
use quote;
use crate;
use crategenerate_arithmetic_for_all_types;
/// Generates `Lhs op Option<Rhs>` pattern arithmetic operation implementations
/// Generates Option type arithmetic operation implementations
///
/// # Supported Pattern
///
/// - `Lhs op Option<Rhs>` -> `Option<Output>` or `Result<Option<Output>, FloatError>`
///
/// # Return Type Rules
///
/// - **Safe operations** (e.g., `PositiveF64 + NegativeF64 -> FinF64`):
/// Returns `Option<Output>`
///
/// - **Fallible operations** (e.g., multiplication may cause overflow):
/// Returns `Result<Output, FloatError>`
/// - Returns `Ok(Output)` when rhs is `Some(value)` and operation succeeds
/// - Returns `Err(FloatError::NoneOperand)` when rhs is `None`
///
/// - **Division operations**:
/// Returns `Result<Output, FloatError>`
/// - Returns `Ok(Output)` when rhs is `Some(value)` and division succeeds
/// - Returns `Err(FloatError::NoneOperand)` when rhs is `None`
/// - Returns `Err(FloatError::DivisionByZero)` when rhs is `Some(0.0)`
///
/// # Examples
///
/// ## Safe operations return Option
///
/// ```text
/// const A: PositiveF64 = PositiveF64::new_const(5.0);
/// let b: Option<NegativeF64> = Some(NegativeF64::new_const(-3.0));
/// let result: Option<FinF64> = A + b;
/// assert_eq!(result.unwrap().get(), 2.0);
/// ```
///
/// ## Handle None
///
/// ```text
/// const A: PositiveF64 = PositiveF64::new_const(5.0);
/// let b: Option<NegativeF64> = None;
/// let result: Option<FinF64> = A + b;
/// assert!(result.is_none());
/// ```
///
/// ## Fallible operations return Result
///
/// ```text
/// const A: PositiveF64 = PositiveF64::new_const(5.0);
/// let b: Option<PositiveF64> = Some(PositiveF64::new_const(3.0));
/// let result: Result<PositiveF64, FloatError> = A * b;
/// assert!(result.is_ok());
/// assert_eq!(result.unwrap().get(), 15.0);
/// ```
///
/// ## None operand returns error
///
/// ```text
/// const A: PositiveF64 = PositiveF64::new_const(5.0);
/// let b: Option<PositiveF64> = None;
/// let result: Result<PositiveF64, FloatError> = A * b;
/// assert!(result.is_err());
/// assert_eq!(result.unwrap_err(), FloatError::NoneOperand);
/// ```