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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use crate::slice::types::IceType;
use crate::slice::writer;
use crate::slice::escape::escape;
use inflector::cases::snakecase;
use writer::Writer;


#[derive(Clone, Debug)]
pub struct Function {
    pub name: String,
    pub return_type: IceType,
    pub arguments: Vec<(String, IceType, bool)>,
    pub throws: Option<IceType>,
    idempotent: bool,
    return_proxy: bool
}

impl Function {
    pub fn empty() -> Function {
        Function {
            name: String::new(),
            return_type: IceType::VoidType,
            arguments: Vec::new(),
            throws: None,
            idempotent: false,
            return_proxy: false
        }
    }

    pub fn new(name: &str, return_type: IceType) -> Function {
        Function {
            name: String::from(name),
            return_type: return_type,
            arguments: Vec::new(),
            throws: None,
            idempotent: false,
            return_proxy: false
        }
    }

    pub fn set_idempotent(&mut self) {
        self.idempotent = true;
    }

    pub fn set_return_proxy(&mut self) {
        self.return_proxy = true;
    }

    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!(
                    "{}: {}{}{}",
                    escape(&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(),
                if self.return_proxy { "Prx" } else { "" }                
            )),
            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!(
                    "{}: {}{}{}",
                    escape(&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(),
                if self.return_proxy { "Prx" } else { "" }                
            )),
            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, ice_type, _) in input_args.into_iter() {
            match ice_type {
                IceType::Optional(var_type, tag) => {
                    writer.write(&format!("if let Some(value) = {} {{\n", escape(&snakecase::to_snake_case(key))), 2)?;
                    writer.write(&format!("bytes.extend(OptionalFlag::new({}, {}::optional_type()).to_bytes()?);\n", tag, var_type.rust_type()), 3)?;
                    writer.write("bytes.extend(value.to_bytes()?);\n", 3)?;
                    writer.write("}\n", 2)?;
                }
                _ => {
                    writer.write(&format!("bytes.extend({}.to_bytes()?);\n", escape(&snakecase::to_snake_case(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(\"{}\"), {}", error_type, self.name, if self.idempotent { 1 } else { 0 }), 2)?;
        } else {
            writer.write(&format!("self.dispatch::<{}>(&String::from(\"{}\"), {}", error_type, self.name, if self.idempotent { 1 } else { 0 }), 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",
                        snakecase::to_snake_case(key),
                        argtype.rust_from()
                    ),
                    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)?;
                    }
                    IceType::CustomType(_) => {
                        if self.return_proxy {
                            writer.write("let proxy_data = ProxyData::from_bytes(&reply.body.data[read_bytes as usize..reply.body.data.len()], &mut read_bytes)?;\n", 2)?;
                            writer.write(&format!("let proxy_string = format!(\"{{}}:{{}} -h {{}} -p {{}}\", proxy_data.id, if proxy_data.secure {{ \"ssl\" }} else {{ \"tcp\" }}, self.proxy.host, self.proxy.port);\n"), 2)?;
                            writer.write("let comm = ice_rs::communicator::Communicator::new();\n", 2)?;
                            writer.write("let proxy = comm.string_to_proxy(&proxy_string)?;\n", 2)?;
                            writer.write("HelloPrx::checked_cast(proxy)", 2)?;
                        } else {
                            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.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()
    }
}