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