diff --git a/Cargo.lock b/Cargo.lock index d994b8d..0245981 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -88,7 +88,7 @@ dependencies = [ [[package]] name = "crisp" -version = "0.1.0" +version = "0.1.3" dependencies = [ "lazy_static", "miette", diff --git a/Cargo.toml b/Cargo.toml index 5b002e9..0c47f3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "crisp" -version = "0.1.0" +version = "0.1.3" edition = "2021" [lib] -crate-type = ["staticlib"] +name = "crisp" path = "src/lib.rs" [dependencies] diff --git a/src/lib.rs b/src/lib.rs index 0a8870f..ba57106 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ pub mod reader; pub mod types; use types::*; -pub fn main() -> Result<()> { +fn main() -> Result<()> { let mut rl = Editor::<(), rustyline::history::DefaultHistory>::new().unwrap(); if rl.load_history(".mal-history").is_err() { eprintln!("No previous history."); diff --git a/src/types.rs b/src/types.rs index b553a70..96e0700 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,4 @@ -use color_eyre::{eyre::eyre, Result}; +use miette::{miette, Result}; use std::{num::ParseIntError, rc::Rc}; #[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)] @@ -46,7 +46,14 @@ impl Value { pub fn apply(&self, args: LispArgs) -> Result { match self { Self::Function(f, _) => f(args), - _ => Err(eyre!("No function here, shouldn't call apply")), + _ => Err(miette!("No function here, shouldn't call apply")), + } + } + + pub fn is_list(&self) -> bool { + match self { + Self::List(_) => true, + _ => false, } } } @@ -100,6 +107,64 @@ impl From<&i64> for Value { } } +impl From for String { + fn from(value: Value) -> Self { + match value { + Value::String(str) => str, + Value::Symbol(Symbol(str)) => str, + Value::Keyword(Keyword(str)) => str, + _ => String::default(), + } + } +} + +impl From<&Value> for String { + fn from(value: &Value) -> Self { + match value { + Value::String(str) => str.clone(), + Value::Symbol(Symbol(str)) => str.clone(), + Value::Keyword(Keyword(str)) => str.clone(), + _ => String::default(), + } + } +} + +impl From for i32 { + fn from(value: Value) -> Self { + match value { + Value::Number(num) => num as i32, + _ => 0, + } + } +} + +impl From for i64 { + fn from(value: Value) -> Self { + match value { + Value::Number(num) => num, + _ => 0, + } + } +} + +impl From<&Value> for i32 { + fn from(value: &Value) -> Self { + match value { + Value::Number(num) => *num as i32, + _ => 0, + } + } +} + +impl From<&Value> for i64 { + fn from(value: &Value) -> Self { + match value { + Value::Number(num) => *num, + _ => 0, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct Symbol(pub String); @@ -129,3 +194,16 @@ impl std::fmt::Display for Keyword { std::fmt::Display::fmt(&self.0, f) } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_conversions() { + let value = Value::Symbol(Symbol("c1".to_owned())); + let string = String::from("c1"); + let convert = String::from(value); + assert_eq!(string, convert); + } +}