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

WIP: clojure.string functionality #41

Merged
merged 12 commits into from
May 13, 2020
Prev Previous commit
Next Next commit
wip work for join
  • Loading branch information
erkkikeranen committed May 11, 2020
commit c70e35a929bdd009882ddf91dcc59f080aef63a8
1 change: 1 addition & 0 deletions src/clojure_string.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub (crate) mod reverse;
pub (crate) mod join;
48 changes: 48 additions & 0 deletions src/clojure_string/join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::ifn::IFn;
use crate::value::{ToValue, Value};
use std::rc::Rc;

use crate::error_message;
use crate::type_tag::TypeTag;

/// clojure.string/reverse ; reverses a string
/// (defn print-string [string] .. prints single string .. )
#[derive(Debug, Clone)]
pub struct JoinFn {}
impl ToValue for JoinFn {
fn to_value(&self) -> Value {
Value::IFn(Rc::new(self.clone()))
}
}
impl IFn for JoinFn {
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
if args.len() == 1 {
match args.get(0).unwrap().to_value() {
Value::Iseq(s) => Value::String(s.chars().rev().collect()),
_a => error_message::type_mismatch(TypeTag::String, &_a.to_value())
}

} else {
return error_message::wrong_arg_count(2, args.len());

}
}
}

#[cfg(test)]
mod tests {
mod reverse_tests {
use crate::value::Value;
use std::rc::Rc;
use crate::clojure_string::reverse::JoinFn;
use crate::ifn::IFn;

#[test]
fn reverse_string() {
let reverse = JoinFn {};
let s = "hello";
let args = vec![Rc::new(Value::String(String::from(s)))];
assert_eq!(Value::String(String::from("olleh")), reverse.invoke(args));
}
}
}