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 163 164 165 166 167
use std::fmt::{Display, Formatter, Result}; use std::iter; use {Argument, Block, Comment, Statement}; #[derive(Debug, Clone)] pub struct Function { pub docstring: Option<Comment>, pub name: String, pub args: Vec<Argument>, block: Block, } #[cfg(test)] mod tests { mod function { use {Function, FunctionCall}; use {Argument, Block, Statement}; #[test] fn it_outputs_a_function() { let mut function = Function::new("hello", vec![Argument::new("name", Some("\"World\""), None, None)], &Block::new()); let arguments = vec![Statement::Argument(Argument::bare("Hello")), Statement::Argument(Argument::input("name"))]; let function_call = FunctionCall::new("print", arguments); function.add_statement(Statement::FunctionCall(function_call)); let expected = r#"def hello(name="World"): print("Hello", name) "#; let actual = format!("{}", function); println!("expected:"); println!("{}", expected); println!("actual:"); println!("{}", actual); assert_eq!(expected, actual); } #[test] fn it_outputs_a_function_with_a_comment() { let mut function = Function::new("hello", vec![Argument::new("name", Some("\"World\""), Some("str"), Some("The name to say Hello to"))], &Block::new()); let arguments = vec![Statement::Argument(Argument::bare("Hello")), Statement::Argument(Argument::input("name"))]; let function_call = FunctionCall::new("print", arguments); function.add_statement(Statement::FunctionCall(function_call)); function.add_docstring("hello is a function that prints \"Hello\" with the given \ name argument."); let expected = r#"def hello(name="World"): """ hello is a function that prints "Hello" with the given name argument. :param name: str The name to say Hello to """ print("Hello", name) "#; let actual = format!("{}", function); println!("expected:"); println!("{}", expected); println!("actual:"); println!("{}", actual); assert_eq!(expected, actual); } } mod function_call { use FunctionCall; use {Argument, Statement}; #[test] fn it_prints_a_function_call() { let arguments = vec![Statement::Argument(Argument::bare("Hello")), Statement::Argument(Argument::input("name"))]; let function_call = FunctionCall::new("print", arguments); let expected = "print(\"Hello\", name)"; assert_eq!(expected, format!("{}", function_call)); } } } impl Function { pub fn new<T: Display>(name: T, args: Vec<Argument>, parent: &Block) -> Function { Function { docstring: None, name: name.to_string(), args: args, block: Block::new_with_parent(parent), } } pub fn add_statement(&mut self, function_call: Statement) { self.block.add_statement(function_call); } pub fn add_docstring<T: Display>(&mut self, doc: T) { self.docstring = Some(Comment::docstring(doc)) } } impl Display for Function { fn fmt(&self, f: &mut Formatter) -> Result { let mut s = String::new(); let args_string = self.args.iter().map(|s| s.to_string()).collect::<Vec<String>>().join(", "); s.push_str(&format!("def {}({}):\n", self.name, args_string)[..]); let indent: String = self.block.indentation(); let mut docstring = String::new(); if let Some(ref doc) = self.docstring { docstring.push_str(&format!("{}{}", indent, doc)[..]); } let mut arg_help: Vec<String> = vec![]; for arg in &self.args { if arg.has_help() { arg_help.push(format!("{}{}", indent, arg.help_string())); } } if arg_help.len() > 0 { docstring.push_str("\n\n"); docstring.push_str(&arg_help.join("\n")[..]); } if docstring.len() > 0 { s.push_str(&format!(r#"{}""" {} {}""" "#, indent, docstring, indent)[..]); } s.push_str(&format!("{}", self.block)[..]); write!(f, "{}", s) } } #[derive(Debug, Clone)] pub struct FunctionCall { pub name: String, pub arguments: Vec<Statement>, } impl FunctionCall { pub fn new<T: Display>(name: T, arguments: Vec<Statement>) -> FunctionCall { FunctionCall { name: name.to_string(), arguments: arguments, } } } impl Display for FunctionCall { fn fmt(&self, f: &mut Formatter) -> Result { let args_string = self.arguments.iter().map(|s| s.to_string()).collect::<Vec<String>>().join(", "); write!(f, "{}({})", self.name, args_string) } }