Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various refactorings to make things more idiomatic #20

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 42 additions & 76 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,37 @@ use super::*;
impl Expr {
/// If this is a [`Normal`] expression, return that. Otherwise return None.
pub fn try_as_normal(&self) -> Option<&Normal> {
match self.kind() {
ExprKind::Normal(ref normal) => Some(normal),
ExprKind::Symbol(_)
| ExprKind::String(_)
| ExprKind::Integer(_)
| ExprKind::Real(_) => None,
}
let ExprKind::Normal(ref normal) = self.kind() else {
return None;
};
Some(normal)
}

/// If this is a [`True`](http://reference.wolfram.com/language/ref/True.html)
/// or [`False`](http://reference.wolfram.com/language/ref/False.html) symbol,
/// return that. Otherwise return None.
pub fn try_as_bool(&self) -> Option<bool> {
let s = self.try_as_symbol()?;
if s.as_str() == "System`True" {
return Some(true);
}
if s.as_str() == "System`False" {
return Some(false);
match self.try_as_symbol()?.as_str() {
"System`True" => Some(true),
"System`False" => Some(false),
_ => None,
}
None
}

/// If this is a [`ExprKind::String`] expression, return that. Otherwise return None.
pub fn try_as_str(&self) -> Option<&str> {
match self.kind() {
ExprKind::String(ref string) => Some(string.as_str()),
_ => None,
}
let ExprKind::String(ref string) = self.kind() else {
return None;
};
Some(string.as_str())
}

/// If this is a [`Symbol`] expression, return that. Otherwise return None.
pub fn try_as_symbol(&self) -> Option<&Symbol> {
match self.kind() {
ExprKind::Symbol(ref symbol) => Some(symbol),
ExprKind::Normal(_)
| ExprKind::String(_)
| ExprKind::Integer(_)
| ExprKind::Real(_) => None,
}
let ExprKind::Symbol(ref symbol) = self.kind() else {
return None;
};
Some(symbol)
}

/// If this is a [`Number`] expression, return that. Otherwise return None.
Expand Down Expand Up @@ -83,40 +74,37 @@ impl Expr {
//=======================================

impl From<Symbol> for Expr {
fn from(sym: Symbol) -> Expr {
Expr::symbol(sym)
fn from(sym: Symbol) -> Self {
Self::symbol(sym)
}
}

impl From<&Symbol> for Expr {
fn from(sym: &Symbol) -> Expr {
Expr::symbol(sym)
fn from(sym: &Symbol) -> Self {
Self::symbol(sym)
}
}

impl From<Normal> for Expr {
fn from(normal: Normal) -> Expr {
Expr {
inner: Arc::new(ExprKind::Normal(normal)),
fn from(normal: Normal) -> Self {
Self {
inner: ExprKind::Normal(normal).into(),
}
}
}

impl From<bool> for Expr {
fn from(value: bool) -> Expr {
match value {
true => Expr::symbol(Symbol::new("System`True")),
false => Expr::symbol(Symbol::new("System`False")),
}
fn from(value: bool) -> Self {
Self::symbol(Symbol::new(if value { "System`True" } else { "System`False" }))
}
}

macro_rules! string_like {
($($t:ty),*) => {
$(
impl From<$t> for Expr {
fn from(s: $t) -> Expr {
Expr::string(s)
fn from(s: $t) -> Self {
Self::string(s)
}
}
)*
Expand All @@ -129,45 +117,23 @@ string_like!(&str, &String, String);
// Integer conversions
//--------------------

impl From<u8> for Expr {
fn from(int: u8) -> Expr {
Expr::from(i64::from(int))
}
}

impl From<i8> for Expr {
fn from(int: i8) -> Expr {
Expr::from(i64::from(int))
}
}

impl From<u16> for Expr {
fn from(int: u16) -> Expr {
Expr::from(i64::from(int))
}
}

impl From<i16> for Expr {
fn from(int: i16) -> Expr {
Expr::from(i64::from(int))
}
}

impl From<u32> for Expr {
fn from(int: u32) -> Expr {
Expr::from(i64::from(int))
macro_rules! u64_convertible {
($($t:ty),*) => {
$(
impl From<$t> for Expr {
fn from(s: $t) -> Self {
Self::from(i64::from(s))
}
}
)*
}
}

impl From<i32> for Expr {
fn from(int: i32) -> Expr {
Expr::from(i64::from(int))
}
}
u64_convertible!(u8, i8, u16, i16, u32, i32);

impl From<i64> for Expr {
fn from(int: i64) -> Expr {
Expr::number(Number::Integer(int))
fn from(int: i64) -> Self {
Self::number(Number::Integer(int))
}
}

Expand All @@ -184,10 +150,10 @@ impl From<i64> for Expr {
// }

impl From<Number> for ExprKind {
fn from(number: Number) -> ExprKind {
fn from(number: Number) -> Self {
match number {
Number::Integer(int) => ExprKind::Integer(int),
Number::Real(real) => ExprKind::Real(real),
Number::Integer(int) => Self::Integer(int),
Number::Real(real) => Self::Real(real),
}
}
}
Loading