|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +#[derive(PartialEq,Eq)] |
| 4 | +pub enum Action{ |
| 5 | + RunScript(String), |
| 6 | + Evaluate(String), |
| 7 | + Nothing, |
| 8 | +} |
| 9 | + |
| 10 | +impl fmt::Debug for Action { |
| 11 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 12 | + match &*self { |
| 13 | + Action::RunScript(filepath) => write!(f, "RunScript: {}", filepath), |
| 14 | + Action::Evaluate(expression) => write!(f, "Evaluate: {}", expression), |
| 15 | + Action::Nothing => write!(f, "Nothing"), |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +pub fn parse_args(arguments: Vec<String>) -> Action{ |
| 21 | + |
| 22 | + if arguments.len() >= 2 { |
| 23 | + if arguments[1] == "-i" || arguments[1] == "--init" { |
| 24 | + return Action::RunScript(arguments[2].clone()); |
| 25 | + }else if arguments[1] == "-e" || arguments[1] == "--eval" { |
| 26 | + return Action::Evaluate(arguments[2].clone()); |
| 27 | + }else { Action::RunScript(arguments[1].clone()) } // for path as argument |
| 28 | + }else { |
| 29 | + return Action::Nothing; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[cfg(test)] |
| 34 | +mod tests { |
| 35 | + mod parse_args_test { |
| 36 | + use crate::user_action; |
| 37 | + |
| 38 | + #[test] |
| 39 | + fn parses_args_given_path() { |
| 40 | + let arguments = vec!["target/debug/rust_clojure".to_string(), "examples/hello_world.clj".to_string()]; |
| 41 | + assert_eq!(user_action::Action::RunScript("examples/hello_world.clj".to_string()), |
| 42 | + user_action::parse_args(arguments)); |
| 43 | + } |
| 44 | + |
| 45 | + #[test] |
| 46 | + fn parses_args_given_i() { |
| 47 | + let arguments = vec!["target/debug/rust_clojure".to_string(), "-i".to_string(), "test.clj".to_string()]; |
| 48 | + |
| 49 | + assert_eq!(user_action::Action::RunScript("test.clj".to_string()), |
| 50 | + user_action::parse_args(arguments)); |
| 51 | + } |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn parses_args_given_init() { |
| 55 | + let arguments = vec!["target/debug/rust_clojure".to_string(), "--init".to_string(), "testing.clj".to_string()]; |
| 56 | + |
| 57 | + assert_eq!(user_action::Action::RunScript("testing.clj".to_string()), |
| 58 | + user_action::parse_args(arguments)); |
| 59 | + } |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn parses_args_given_e() { |
| 63 | + let arguments = vec!["target/debug/rust_clojure".to_string(), "-e".to_string(), "(+ 1 2 3)".to_string()]; |
| 64 | + |
| 65 | + assert_eq!(user_action::Action::Evaluate("(+ 1 2 3)".to_string()), |
| 66 | + user_action::parse_args(arguments)); |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn parses_args_given_eval() { |
| 71 | + let arguments = vec!["target/debug/rust_clojure".to_string(), "--eval".to_string(), "(println \"eh\")".to_string()]; |
| 72 | + |
| 73 | + assert_eq!(user_action::Action::Evaluate("(println \"eh\")".to_string()), |
| 74 | + user_action::parse_args(arguments)); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn parses_args_given_nil() { |
| 79 | + assert_eq!(user_action::Action::Nothing, user_action::parse_args(vec!["target/debug/rust_clojure".to_string()])); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
0 commit comments