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
use crate::slice::types::IceType; use crate::slice::writer; use inflector::cases::snakecase; use writer::Writer; #[derive(Clone, Debug)] pub struct Function { pub name: String, pub return_type: IceType, arguments: Vec<(String, IceType, bool)>, throws: Option<IceType> } impl Function { pub fn empty() -> Function { Function { name: String::new(), return_type: IceType::VoidType, arguments: Vec::new(), throws: None } } pub fn new(name: &str, return_type: IceType) -> Function { Function { name: String::from(name), return_type: return_type, arguments: Vec::new(), throws: None } } pub fn function_name(&self) -> String { snakecase::to_snake_case(&self.name) } pub fn add_argument(&mut self, name: &str, var_type: IceType, output: bool) { self.arguments.push((String::from(name), var_type, output)); } pub fn set_throw(&mut self, throws: Option<IceType>) { self.throws = throws; } pub fn generate_decl(&self, writer: &mut Writer) -> Result<(), Box<dyn std::error::Error>> { let mut arguments = Vec::new(); arguments.push(String::from("&mut self")); for (key, var_type, out) in &self.arguments { arguments.push( format!( "{}: {}{}{}", snakecase::to_snake_case(key), if var_type.as_ref() | *out { "&" } else { "" }, if *out { "mut "} else { "" }, var_type.rust_type() ) ); } writer.generate_fn( false, None, &self.function_name(), arguments, Some(&format!("Result<{}, Box<dyn std::error::Error>>", self.return_type.rust_type())), false, 1 ) } pub fn generate_impl(&self, writer: &mut Writer) -> Result<(), Box<dyn std::error::Error>> { let mut arguments = Vec::new(); arguments.push(String::from("&mut self")); for (key, var_type, out) in &self.arguments { arguments.push( format!( "{}: {}{}{}", snakecase::to_snake_case(key), if var_type.as_ref() | *out { "&" } else { "" }, if *out { "mut "} else { "" }, var_type.rust_type() ) ); } writer.generate_fn( false, None, &self.function_name(), arguments, Some(&format!("Result<{}, Box<dyn std::error::Error>>", self.return_type.rust_type())), true, 1 )?; let input_args_count = self.arguments.iter().filter(|(_, _, out)| !*out).count(); let input_args = self.arguments.iter().filter(|(_, _, out)| !*out); let output_args_count = self.arguments.iter().filter(|(_, _, out)| *out).count(); let output_args = self.arguments.iter().filter(|(_, _, out)| *out); writer.write(&format!("let {} bytes = Vec::new();\n", if input_args_count > 0 { "mut" } else { "" }), 2)?; for (key, _, _) in input_args.into_iter() { writer.write(&format!("bytes.extend({}.to_bytes()?);\n", key), 2)?; } let mut require_reply = output_args_count > 0; match self.return_type { IceType::VoidType => {}, _ => require_reply = true } let error_type = match &self.throws { Some(throws) => { throws.rust_type() }, _ => { String::from("ProtocolError") } }; if require_reply { writer.write(&format!("let reply = self.dispatch::<{}>(&String::from(\"{}\"), 0", error_type, self.name), 2)?; } else { writer.write(&format!("self.dispatch::<{}>(&String::from(\"{}\"), 0", error_type, self.name), 2)?; } writer.write(", &Encapsulation::from(bytes))?;\n\n", 0)?; if require_reply { writer.write("let mut read_bytes: i32 = 0;\n", 2)?; for (key, argtype, _) in output_args.into_iter() { writer.write( &format!( "*{} = {}::from_bytes(&reply.body.data[read_bytes as usize..reply.body.data.len()], &mut read_bytes)?;\n", key, argtype.rust_type() ), 2 )?; } } match self.return_type { IceType::VoidType => { writer.write("Ok(())\n", 2)?; }, _ => { match &self.return_type { IceType::Optional(type_name) => { writer.write(&format!("Option::<{}>::from_bytes(&reply.body.data[read_bytes as usize..reply.body.data.len()], &mut read_bytes)\n", type_name.rust_type()), 2)?; } _ => { writer.write(&format!("{}::from_bytes(&reply.body.data[read_bytes as usize..reply.body.data.len()], &mut read_bytes)\n", self.return_type.rust_type()), 2)?; } } } }; writer.generate_close_block(1)?; writer.blank_line() } }