wolfram_expr/
conversion.rs

1use super::*;
2
3
4impl Expr {
5    /// If this is a [`Normal`] expression, return that. Otherwise return None.
6    pub fn try_as_normal(&self) -> Option<&Normal> {
7        match self.kind() {
8            ExprKind::Normal(ref normal) => Some(normal),
9            ExprKind::Symbol(_)
10            | ExprKind::String(_)
11            | ExprKind::Integer(_)
12            | ExprKind::Real(_) => None,
13        }
14    }
15
16    /// If this is a [`True`](http://reference.wolfram.com/language/ref/True.html)
17    /// or [`False`](http://reference.wolfram.com/language/ref/False.html) symbol,
18    /// return that. Otherwise return None.
19    pub fn try_as_bool(&self) -> Option<bool> {
20        let s = self.try_as_symbol()?;
21        if s.as_str() == "System`True" {
22            return Some(true);
23        }
24        if s.as_str() == "System`False" {
25            return Some(false);
26        }
27        None
28    }
29
30    /// If this is a [`ExprKind::String`] expression, return that. Otherwise return None.
31    pub fn try_as_str(&self) -> Option<&str> {
32        match self.kind() {
33            ExprKind::String(ref string) => Some(string.as_str()),
34            _ => None,
35        }
36    }
37
38    /// If this is a [`Symbol`] expression, return that. Otherwise return None.
39    pub fn try_as_symbol(&self) -> Option<&Symbol> {
40        match self.kind() {
41            ExprKind::Symbol(ref symbol) => Some(symbol),
42            ExprKind::Normal(_)
43            | ExprKind::String(_)
44            | ExprKind::Integer(_)
45            | ExprKind::Real(_) => None,
46        }
47    }
48
49    /// If this is a [`Number`] expression, return that. Otherwise return None.
50    pub fn try_as_number(&self) -> Option<Number> {
51        match self.kind() {
52            ExprKind::Integer(int) => Some(Number::Integer(*int)),
53            ExprKind::Real(real) => Some(Number::Real(*real)),
54            ExprKind::Normal(_) | ExprKind::String(_) | ExprKind::Symbol(_) => None,
55        }
56    }
57
58    //---------------------------------------------------------------------------
59    // SEMVER: These methods have been replaced; remove them in a future version.
60    //---------------------------------------------------------------------------
61
62    #[deprecated(note = "Use Expr::try_as_normal() instead")]
63    #[allow(missing_docs)]
64    pub fn try_normal(&self) -> Option<&Normal> {
65        self.try_as_normal()
66    }
67
68    #[deprecated(note = "Use Expr::try_as_symbol() instead")]
69    #[allow(missing_docs)]
70    pub fn try_symbol(&self) -> Option<&Symbol> {
71        self.try_as_symbol()
72    }
73
74    #[deprecated(note = "Use Expr::try_as_number() instead")]
75    #[allow(missing_docs)]
76    pub fn try_number(&self) -> Option<Number> {
77        self.try_as_number()
78    }
79}
80
81//=======================================
82// Conversion trait impl's
83//=======================================
84
85impl From<Symbol> for Expr {
86    fn from(sym: Symbol) -> Expr {
87        Expr::symbol(sym)
88    }
89}
90
91impl From<&Symbol> for Expr {
92    fn from(sym: &Symbol) -> Expr {
93        Expr::symbol(sym)
94    }
95}
96
97impl From<Normal> for Expr {
98    fn from(normal: Normal) -> Expr {
99        Expr {
100            inner: Arc::new(ExprKind::Normal(normal)),
101        }
102    }
103}
104
105impl From<bool> for Expr {
106    fn from(value: bool) -> Expr {
107        match value {
108            true => Expr::symbol(Symbol::new("System`True")),
109            false => Expr::symbol(Symbol::new("System`False")),
110        }
111    }
112}
113
114macro_rules! string_like {
115    ($($t:ty),*) => {
116        $(
117            impl From<$t> for Expr {
118                fn from(s: $t) -> Expr {
119                    Expr::string(s)
120                }
121            }
122        )*
123    }
124}
125
126string_like!(&str, &String, String);
127
128//--------------------
129// Integer conversions
130//--------------------
131
132impl From<u8> for Expr {
133    fn from(int: u8) -> Expr {
134        Expr::from(i64::from(int))
135    }
136}
137
138impl From<i8> for Expr {
139    fn from(int: i8) -> Expr {
140        Expr::from(i64::from(int))
141    }
142}
143
144impl From<u16> for Expr {
145    fn from(int: u16) -> Expr {
146        Expr::from(i64::from(int))
147    }
148}
149
150impl From<i16> for Expr {
151    fn from(int: i16) -> Expr {
152        Expr::from(i64::from(int))
153    }
154}
155
156impl From<u32> for Expr {
157    fn from(int: u32) -> Expr {
158        Expr::from(i64::from(int))
159    }
160}
161
162impl From<i32> for Expr {
163    fn from(int: i32) -> Expr {
164        Expr::from(i64::from(int))
165    }
166}
167
168impl From<i64> for Expr {
169    fn from(int: i64) -> Expr {
170        Expr::number(Number::Integer(int))
171    }
172}
173
174// impl From<Normal> for ExprKind {
175//     fn from(normal: Normal) -> ExprKind {
176//         ExprKind::Normal(Box::new(normal))
177//     }
178// }
179
180// impl From<Symbol> for ExprKind {
181//     fn from(symbol: Symbol) -> ExprKind {
182//         ExprKind::Symbol(symbol)
183//     }
184// }
185
186impl From<Number> for ExprKind {
187    fn from(number: Number) -> ExprKind {
188        match number {
189            Number::Integer(int) => ExprKind::Integer(int),
190            Number::Real(real) => ExprKind::Real(real),
191        }
192    }
193}