// Returns the updated index for requests and responses.
Returns the updated index for requests and responses.
Returns the updated index for requests and responses .
pub fn generate_messages_for_interface<'a, W: Write>(
        &self,
        name: &str,
        messages: &[TypedMessage],
        req_index: usize,
        resp_index: usize,
        sink: &'a mut W,
    ) -> Result<(usize, usize)> {
        writeln!(sink, "const {}Messages = {{", name)?;
        // For each request, provide an encoder.
        let mut req_index = req_index;
        let mut resp_index = resp_index;
        for message in messages.iter() {
            match message {
                TypedMessage::Request(req) => {
                    writeln!(sink, "{}Request: {{", req.name)?;
                    writeln!(sink, "encode: (data) => {{")?;
                    writeln!(sink, "let encoder = new Encoder();")?;
                    writeln!(sink, "let result = encoder.enum_tag({});", req_index)?;
                    req_index += 1;
                    // generate the encoder for the request payload.
                    for ptype in &req.types {
                        self.write_encoder_for_item("data", &ptype, sink)?;
                    }
                    writeln!(sink, "return result.value();")?;
                    writeln!(sink, "}}")?;
                    writeln!(sink, "}},")?;
                }
                TypedMessage::Response(resp) => {
                    // For each response, provide a decoder.
                    writeln!(sink, "{}Response: {{", resp.name)?;
                    writeln!(sink, "decode: (buffer , service_id, session) => {{")?;
                    writeln!(sink, "let decoder = new Decoder(buffer);")?;
                    writeln!(sink, "let variant = decoder.enum_tag();")?;
                    if let Some(item) = &resp.success {
                        // Methods and getter responses.
                        writeln!(sink, "if (variant == {}) {{", resp_index)?;
                        writeln!(sink, "// Success")?;
                        writeln!(sink, "let result = null;")?;
                        self.write_decoder_for_item(&item, sink, "result", 0)?;
                        writeln!(sink, "return {{ success: result }}")?;
                        writeln!(sink, "}}")?;
                        resp_index += 1;
                    } else {
                        panic!("Unexpected empty success response for {}", resp.name);
                    }
                    if let Some(item) = &resp.error {
                        // Methods error responses.
                        writeln!(sink, "else if (variant == {}) {{", resp_index)?;
                        writeln!(sink, "// Error")?;
                        writeln!(sink, "let result = null;")?;
                        self.write_decoder_for_item(&item, sink, "result", 0)?;
                        writeln!(sink, "return {{ error: result }}")?;
                        writeln!(sink, "}}")?;
                        resp_index += 1;
                    }
                    writeln!(sink, "else {{")?;
                    writeln!(
                        sink,
                        "console.error(`{}Response::decode: Unexpected variant ${{variant}}`);",
                        resp.name
                    )?;
                    writeln!(sink, "return null;")?;
                    writeln!(sink, "}}")?;
                    writeln!(sink, "}}")?;
                    writeln!(sink, "}},")?;
                }
            }
        }
        writeln!(sink, "}}\n")?;
        Ok((req_index, resp_index))
    }
pub fn generate_messages_for_interface < ' a , W : Write > ( & self , name : & str , messages : & [ TypedMessage ] , req_index : usize , resp_index : usize , sink : & ' a mut W , ) -> Result < ( usize , usize ) > { writeln ! ( sink " " name ) ? ; // For each request, provide an encoder. let mut req_index = req_index ; let mut resp_index = resp_index ; for message in messages . iter ( ) { match message { TypedMessage :: Request ( req ) => { writeln ! ( sink " " req name ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " req_index ) ? ; req_index += 1 ; // generate the encoder for the request payload. for ptype in & req . types { self . write_encoder_for_item ( " " , & ptype , sink ) ? ; } writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; } TypedMessage :: Response ( resp ) => { // For each response, provide a decoder. writeln ! ( sink " " resp name ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; if let Some ( item ) = & resp . success { // Methods and getter responses. writeln ! ( sink " " resp_index ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; self . write_decoder_for_item ( & item , sink , " " , 0 ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; resp_index += 1 ; } else { panic ! ( " " resp name ) ; } if let Some ( item ) = & resp . error { // Methods error responses. writeln ! ( sink " " resp_index ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; self . write_decoder_for_item ( & item , sink , " " , 0 ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; resp_index += 1 ; } writeln ! ( sink " " ) ? ; writeln ! ( sink " " resp name ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; } } } writeln ! ( sink " \n " ) ? ; Ok ( ( req_index , resp_index ) ) }
Returns the updated index for requests and responses.
Returns the updated index for requests and responses .
&self
name
&str
messages
&[TypedMessage]
req_index
usize
resp_index
usize
sink
&'a mut W