1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::fmt::{Display, Formatter, Result};

#[derive(Debug, Clone)]
pub enum Argument {
    FunctionDefinition {
        name: String,
        default: Option<String>,
        help: Option<String>,
        arg_type: Option<String>
    },
    Variable ( String ),
    Unnamed ( String )
}

#[cfg(test)]
mod tests {
    mod definition {
        use Argument;

        #[test]
        fn it_prints_an_argument_with_no_default() {
            let arg = Argument::new("test", None, None, None);
            assert_eq!("test", format!("{}", arg));
        }

        #[test]
        fn it_prints_an_argument_with_a_default() {
            let arg = Argument::new("test", Some(r#""value""#), None, None);
            assert_eq!(r#"test="value""#, format!("{}", arg));
        }
    }

    mod bare_argument {
        use Argument;
        #[test]
        fn it_prints_an_argument() {
            let arg = Argument::unnamed("world");
            assert_eq!("\"world\"", format!("{}", arg));
        }
    }

    mod function_argument {
        use Argument;
        #[test]
        fn it_prints_an_argument() {
            let arg = Argument::variable("name");
            assert_eq!("name", format!("{}", arg));
        }
    }

}

impl Argument {
    pub fn new<T: Display>(name: T,
                           default: Option<T>,
                           arg_type: Option<T>,
                           help: Option<T>)
                           -> Argument {
        Argument::FunctionDefinition {
            name: name.to_string(),
            default: default.map(|s| s.to_string()),
            arg_type: arg_type.map(|s| s.to_string()),
            help: help.map(|s| s.to_string())
        }
    }

    // pub fn bare<T: Display>(value: T) -> Argument {
    //     Argument::unnamed(value)
    // }

    pub fn unnamed<T: Display>(value: T) -> Argument {
        Argument::Unnamed(value.to_string())
    }

    // pub fn input<T: Display>(name: T) -> Argument {
    //     Argument::variable(name)
    // }

    pub fn variable<T: Display>(name: T) -> Argument {
        Argument::Variable (name.to_string())
    }

    pub fn has_help(&self) -> bool {
        match self {
            &Argument::FunctionDefinition{ref help, .. } => help.is_some(),
            _ => false,
        }
    }

    pub fn help_string(&self) -> Option<String> {
        match self {
            &Argument::FunctionDefinition{ref help,  ref name, ..} => {
                let mut formatted_help = format!(":param {}:", name);

                if let &Some(ref help_str) = help {
                    formatted_help.push_str(&format!(" {}", help_str)[..]);
                    Some(formatted_help)
                } else {
                    None
                }
            },
            _ => None,
        }
    }

    pub fn type_string(&self) -> Option<String> {
        match self {
            &Argument::FunctionDefinition{ref arg_type, ref name, ..} => {
                let mut help = format!(":type {}:", name);
                if let &Some(ref t) = arg_type {
                    help.push_str(&format!(" {}", t)[..]);
                    Some(help)
                } else {
                    None
                }
            },
            _ => None,
        }
    }
}

impl Display for Argument {
    fn fmt(&self, f: &mut Formatter) -> Result {
        let mut output = String::new();
        match self {
            &Argument::FunctionDefinition{ref name, ref default, ..} => {
                output.push_str(&name[..]);
                if let &Some(ref default) = default {
                    output.push_str(&format!("={}", default));
                }
            },
            &Argument::Unnamed(ref value) => {
                output.push_str(&format!("\"{}\"", value)[..]);
            },
            &Argument::Variable(ref name) => {
                output.push_str(&format!("{}", name)[..]);
            },
        }
        write!(f, "{}", output)
    }
}

#[derive(Debug, Clone)]
pub struct KeywordArgument {
    pub keyword: String,
    pub argument: String,
}

impl KeywordArgument {
    pub fn new<T: Display>(name: T, arg: T) -> KeywordArgument {
        KeywordArgument {
            keyword: name.to_string(),
            argument: arg.to_string(),
        }
    }
}

impl Display for KeywordArgument {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "{}={}", self.keyword, self.argument)
    }
}