cranelift_reader/
testcommand.rs1use std::fmt::{self, Display, Formatter};
16
17#[derive(Clone, PartialEq, Eq, Debug)]
19pub struct TestCommand<'a> {
20 pub command: &'a str,
22 pub options: Vec<TestOption<'a>>,
24}
25
26#[derive(Clone, PartialEq, Eq, Debug)]
28pub enum TestOption<'a> {
29 Flag(&'a str),
31 Value(&'a str, &'a str),
33}
34
35impl<'a> TestCommand<'a> {
36 pub fn new(s: &'a str) -> Self {
39 let mut parts = s.split_whitespace();
40 let cmd = parts.next().unwrap_or("");
41 Self {
42 command: cmd,
43 options: parts
44 .filter(|s| !s.is_empty())
45 .map(TestOption::new)
46 .collect(),
47 }
48 }
49}
50
51impl<'a> Display for TestCommand<'a> {
52 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
53 write!(f, "{}", self.command)?;
54 for opt in &self.options {
55 write!(f, " {opt}")?;
56 }
57 writeln!(f)
58 }
59}
60
61impl<'a> TestOption<'a> {
62 pub fn new(s: &'a str) -> Self {
65 match s.find('=') {
66 None => TestOption::Flag(s),
67 Some(p) => TestOption::Value(&s[0..p], &s[p + 1..]),
68 }
69 }
70}
71
72impl<'a> Display for TestOption<'a> {
73 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
74 match *self {
75 TestOption::Flag(s) => write!(f, "{s}"),
76 TestOption::Value(s, v) => write!(f, "{s}={v}"),
77 }
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn parse_option() {
87 assert_eq!(TestOption::new(""), TestOption::Flag(""));
88 assert_eq!(TestOption::new("foo"), TestOption::Flag("foo"));
89 assert_eq!(TestOption::new("foo=bar"), TestOption::Value("foo", "bar"));
90 }
91
92 #[test]
93 fn parse_command() {
94 assert_eq!(&TestCommand::new("").to_string(), "\n");
95 assert_eq!(&TestCommand::new("cat").to_string(), "cat\n");
96 assert_eq!(&TestCommand::new("cat ").to_string(), "cat\n");
97 assert_eq!(&TestCommand::new("cat 1 ").to_string(), "cat 1\n");
98 assert_eq!(
99 &TestCommand::new("cat one=4 two t").to_string(),
100 "cat one=4 two t\n"
101 );
102 }
103}