use crate::Value; pub fn print(value: &Value) -> String { match value { Value::List(v) => { let strings: Vec = v.iter().map(print).collect(); let mut string = strings.join(" "); if v.len() < 2 { return string; } let end = string.len() + 1; string.insert_str(0, "("); string.insert_str(end, ")"); // dbg!(&string); string } Value::String(s) => { let mut s = s.clone(); let length = s.len() + 1; s.insert(0, '"'); s.insert(length, '"'); s.to_string() } Value::Number(n) => n.to_string(), Value::Nil => "nil".to_string(), Value::True => "t".to_string(), Value::Symbol(s) => s.0.clone(), Value::Keyword(k) => { let mut string = k.0.clone(); string.insert_str(0, ":"); string } _ => "fn".to_string(), } } #[cfg(test)] mod test { use super::*; use crate::reader::read; use pretty_assertions::assert_eq; use std::fs::read_to_string; #[test] fn test_print() { let string = read_to_string("./test_presentation.lisp").unwrap(); let values = read(&string); let string = r#"((slide :background (image :source "~/pics/frodo.jpg" :fit crop) (text "This is frodo" :font-size 50)) (slide (video :source "~/vids/test/chosensmol.mp4" :fit fill)) (song :author "Jordan Feliz" :ccli 97987 :font "Quicksand" :font-size 80 :title "The River" :background (image :source "./coolbg.jpg") (text "I'm going down to the river") (text "Down to the river") (text "Down to the river to pray ay ay!")) (song :author "Jordan Feliz" :ccli 97987 :font "Quicksand" :font-size 80 :title "The River" :background (video :source "./coolerbg.mkv" :fit cover) :verse-order (v1 c1 v2 c1) (v1 "I'm going down to the river") (c1 "Down to the river") (v2 "Down to the river to pray ay ay!")) (load "./10000-reasons.lisp"))"#.to_string(); let print = print(&values); assert_eq!(print, string) } }