Skip to main content

wolfram_expr/
conversion.rs

1use super::*;
2
3impl Expr {
4    // These accessors are deprecated: match on [`Expr::kind`] ([`ExprKind`])
5    // directly instead. None of them are used inside this crate.
6
7    /// If this is a [`Normal`] expression, return that. Otherwise return None.
8    ///
9    /// # Migration
10    ///
11    /// ```
12    /// # use wolfram_expr::{Expr, ExprKind, expr};
13    /// # let expr = expr!(System::List[1, 2, 3]);
14    /// if let ExprKind::Normal(normal) = expr.kind() {
15    ///     // use `normal`
16    /// }
17    /// ```
18    #[deprecated(note = "match on `Expr::kind()` instead")]
19    pub fn try_as_normal(&self) -> Option<&Normal> {
20        match self.kind() {
21            ExprKind::Normal(ref normal) => Some(normal),
22            _ => None,
23        }
24    }
25
26    /// If this is the `True` or `False` symbol, return that. Otherwise None.
27    ///
28    /// # Migration
29    ///
30    /// ```
31    /// # use wolfram_expr::{Expr, ExprKind, expr};
32    /// # let expr = expr!(System::True);
33    /// let is_true = matches!(expr.kind(), ExprKind::Symbol(s) if s.as_str() == "System`True");
34    /// let is_false = matches!(expr.kind(), ExprKind::Symbol(s) if s.as_str() == "System`False");
35    /// ```
36    #[deprecated(note = "match on `Expr::kind()` instead")]
37    #[allow(deprecated)]
38    pub fn try_as_bool(&self) -> Option<bool> {
39        match self.try_as_symbol()?.as_str() {
40            "System`True" => Some(true),
41            "System`False" => Some(false),
42            _ => None,
43        }
44    }
45
46    /// If this is an [`ExprKind::String`] expression, return that. Otherwise return None.
47    ///
48    /// # Migration
49    ///
50    /// ```
51    /// # use wolfram_expr::{Expr, ExprKind, expr};
52    /// # let expr = expr!("hello");
53    /// if let ExprKind::String(s) = expr.kind() {
54    ///     // use `s` as `&str` via `s.as_str()`
55    /// }
56    /// ```
57    #[deprecated(note = "match on `Expr::kind()` instead")]
58    pub fn try_as_str(&self) -> Option<&str> {
59        match self.kind() {
60            ExprKind::String(ref string) => Some(string.as_str()),
61            _ => None,
62        }
63    }
64
65    /// If this is a [`Symbol`] expression, return that. Otherwise return None.
66    ///
67    /// # Migration
68    ///
69    /// ```
70    /// # use wolfram_expr::{Expr, ExprKind, expr};
71    /// # let expr = expr!(System::Pi);
72    /// if let ExprKind::Symbol(sym) = expr.kind() {
73    ///     // use `sym`
74    /// }
75    /// ```
76    #[deprecated(note = "match on `Expr::kind()` instead")]
77    pub fn try_as_symbol(&self) -> Option<&Symbol> {
78        match self.kind() {
79            ExprKind::Symbol(ref symbol) => Some(symbol),
80            _ => None,
81        }
82    }
83
84    /// If this is a [`Number`] expression, return that. Otherwise return None.
85    ///
86    /// # Migration
87    ///
88    /// ```
89    /// # use wolfram_expr::{Expr, ExprKind, expr};
90    /// # let expr = expr!(42);
91    /// match expr.kind() {
92    ///     ExprKind::Integer(i) => { /* use i: &i64 */ },
93    ///     ExprKind::Real(r)    => { /* use r: &F64 */ },
94    ///     _ => {},
95    /// }
96    /// ```
97    #[deprecated(note = "match on `Expr::kind()` instead")]
98    #[allow(deprecated)]
99    pub fn try_as_number(&self) -> Option<Number> {
100        match self.kind() {
101            ExprKind::Integer(int) => Some(Number::Integer(*int)),
102            ExprKind::Real(real) => Some(Number::Real(*real)),
103            _ => None,
104        }
105    }
106
107    //---------------------------------------------------------------------------
108    // SEMVER: These methods have been replaced; remove them in a future version.
109    //---------------------------------------------------------------------------
110
111    #[deprecated(note = "match on `Expr::kind()` instead")]
112    #[allow(missing_docs, deprecated)]
113    pub fn try_normal(&self) -> Option<&Normal> {
114        self.try_as_normal()
115    }
116
117    #[deprecated(note = "match on `Expr::kind()` instead")]
118    #[allow(missing_docs, deprecated)]
119    pub fn try_symbol(&self) -> Option<&Symbol> {
120        self.try_as_symbol()
121    }
122
123    #[deprecated(note = "match on `Expr::kind()` instead")]
124    #[allow(missing_docs, deprecated)]
125    pub fn try_number(&self) -> Option<Number> {
126        self.try_as_number()
127    }
128}
129
130//=======================================
131// Conversion trait impl's
132//=======================================
133
134impl From<Symbol> for Expr {
135    fn from(sym: Symbol) -> Expr {
136        Expr::symbol(sym)
137    }
138}
139
140impl From<&Symbol> for Expr {
141    fn from(sym: &Symbol) -> Expr {
142        Expr::symbol(sym)
143    }
144}
145
146impl From<Normal> for Expr {
147    fn from(normal: Normal) -> Expr {
148        Expr {
149            inner: Arc::new(ExprKind::Normal(normal)),
150        }
151    }
152}
153
154impl From<bool> for Expr {
155    fn from(value: bool) -> Expr {
156        match value {
157            true => crate::expr!(System::True),
158            false => crate::expr!(System::False),
159        }
160    }
161}
162
163macro_rules! string_like {
164    ($($t:ty),*) => {
165        $(
166            impl From<$t> for Expr {
167                fn from(s: $t) -> Expr {
168                    Expr::string(s)
169                }
170            }
171        )*
172    }
173}
174
175string_like!(&str, &String, String);
176
177//--------------------
178// Integer conversions
179//--------------------
180
181impl From<u8> for Expr {
182    fn from(int: u8) -> Expr {
183        Expr::from(i64::from(int))
184    }
185}
186
187impl From<i8> for Expr {
188    fn from(int: i8) -> Expr {
189        Expr::from(i64::from(int))
190    }
191}
192
193impl From<u16> for Expr {
194    fn from(int: u16) -> Expr {
195        Expr::from(i64::from(int))
196    }
197}
198
199impl From<i16> for Expr {
200    fn from(int: i16) -> Expr {
201        Expr::from(i64::from(int))
202    }
203}
204
205impl From<u32> for Expr {
206    fn from(int: u32) -> Expr {
207        Expr::from(i64::from(int))
208    }
209}
210
211impl From<i32> for Expr {
212    fn from(int: i32) -> Expr {
213        Expr::from(i64::from(int))
214    }
215}
216
217impl From<i64> for Expr {
218    fn from(int: i64) -> Expr {
219        Expr::new(ExprKind::Integer(int))
220    }
221}
222
223impl From<f64> for Expr {
224    fn from(f: f64) -> Expr {
225        Expr::real(f)
226    }
227}
228
229// impl From<Normal> for ExprKind {
230//     fn from(normal: Normal) -> ExprKind {
231//         ExprKind::Normal(Box::new(normal))
232//     }
233// }
234
235// impl From<Symbol> for ExprKind {
236//     fn from(symbol: Symbol) -> ExprKind {
237//         ExprKind::Symbol(symbol)
238//     }
239// }
240
241#[allow(deprecated)]
242impl From<Number> for ExprKind {
243    fn from(number: Number) -> ExprKind {
244        match number {
245            Number::Integer(int) => ExprKind::Integer(int),
246            Number::Real(real) => ExprKind::Real(real),
247        }
248    }
249}
250
251//==============================
252// New WXF-derived From<T> impls
253//==============================
254
255impl From<ByteArray> for Expr {
256    fn from(b: ByteArray) -> Expr {
257        Expr {
258            inner: Arc::new(ExprKind::ByteArray(b)),
259        }
260    }
261}
262
263impl From<Association> for Expr {
264    fn from(a: Association) -> Expr {
265        Expr {
266            inner: Arc::new(ExprKind::Association(a)),
267        }
268    }
269}
270
271impl From<Vec<Expr>> for Expr {
272    fn from(v: Vec<Expr>) -> Expr {
273        Expr::list(v)
274    }
275}
276
277impl From<NumericArray> for Expr {
278    fn from(a: NumericArray) -> Expr {
279        Expr {
280            inner: Arc::new(ExprKind::NumericArray(a)),
281        }
282    }
283}
284
285impl From<PackedArray> for Expr {
286    fn from(a: PackedArray) -> Expr {
287        Expr {
288            inner: Arc::new(ExprKind::PackedArray(a)),
289        }
290    }
291}
292impl From<BigInteger> for Expr {
293    fn from(n: BigInteger) -> Expr {
294        Expr {
295            inner: Arc::new(ExprKind::BigInteger(n)),
296        }
297    }
298}
299impl From<BigReal> for Expr {
300    fn from(r: BigReal) -> Expr {
301        Expr {
302            inner: Arc::new(ExprKind::BigReal(r)),
303        }
304    }
305}