tokio-thrift-codegen 0.1.0

code generation library of tokio-thrift
Documentation
#[allow(dead_code, non_snake_case, non_camel_case_types)]
pub trait {{service.ident}}Service: Send {{#if service.extends ~}}+ {{service.extends}} {{/if}}{
    {{~#each service.methods as |method|}}
    #[allow(dead_code, unused_imports, non_snake_case, non_camel_case_types)]
    {{> method method = method}};
    {{~/each}}
}


#[allow(dead_code, non_snake_case, non_camel_case_types)]
#[derive(Debug, Clone)]
pub enum {{service.ident}}ServiceMethodArgs {
    {{~#each service.methods as |method|}}
    #[allow(dead_code, non_snake_case)]
    A{{method.ident}}({{../service.ident}}{{method.ident}}Args),
    {{~/each}}
}


impl ::tokio_thrift::protocol::Serialize for {{service.ident}}ServiceMethodArgs {
    fn serialize<S>(&self, s: &mut S) -> Result<(), ::tokio_thrift::protocol::Error>
        where S: ::tokio_thrift::protocol::Serializer + ::tokio_thrift::protocol::ThriftSerializer
    {
        use self::{{service.ident}}ServiceMethodArgs::*;
        match self {
            {{~#each service.methods as |method|}}
            &A{{method.ident}}(ref b) => {
                s.write_message_begin("{{method.ident}}", ::tokio_thrift::protocol::ThriftMessageType::Call)?;
                b.serialize(s)?;
                s.write_message_end()?;
            },
            {{~/each}}
        };
        Ok(())
    }
}

impl ::tokio_thrift::protocol::Deserialize for {{service.ident}}ServiceMethodArgs {
    fn deserialize<D>(de: &mut D) -> Result<Self, ::tokio_thrift::protocol::Error>
        where D: ::tokio_thrift::protocol::Deserializer + ::tokio_thrift::protocol::ThriftDeserializer,
    {
        let msg = de.read_message_begin()?;
        //assert!(msg.type) == $msg_type
        let ret = match msg.name.as_ref() {
            {{~#each service.methods as |method|}}
            "{{method.ident}}" => {{../service.ident}}ServiceMethodArgs::A{{method.ident}}({{../service.ident}}{{method.ident}}Args::deserialize(de)?),
            {{~/each}}
            _ => return Err(::tokio_thrift::protocol::Error::from(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "failed to parse thrift data"))),
        };
        let _ = de.read_message_end()?;
        Ok(ret)
    }
}


#[allow(dead_code, non_snake_case, non_camel_case_types)]
#[derive(Debug, Clone)]
pub enum {{service.ident}}ServiceMethodReturn {
    {{~#each service.methods as |method|}}
    // FIXME: generate exception too
    #[allow(dead_code, non_snake_case)]
    R{{method.ident}}(Result<{{to_rust method.ty}}, ()>),
    {{~/each}}

}


impl ::tokio_thrift::protocol::Serialize for {{service.ident}}ServiceMethodReturn {
    fn serialize<S>(&self, s: &mut S) -> Result<(), ::tokio_thrift::protocol::Error>
        where S: ::tokio_thrift::protocol::Serializer + ::tokio_thrift::protocol::ThriftSerializer
    {
        use self::{{service.ident}}ServiceMethodReturn::*;
        match self {
            {{~#each service.methods as |method|}}
            &R{{method.ident}}(ref b) => {
                match b {
                    &Ok(ref b) => {
                        s.write_message_begin("{{method.ident}}", ::tokio_thrift::protocol::ThriftMessageType::Reply)?;
                        b.serialize(s)?;
                        s.write_message_end()?;
                    },
                    &Err(_) => panic!("exception is not supported yet"),
                }
            },
            {{~/each}}
        };
        Ok(())
    }
}

impl ::tokio_thrift::protocol::Deserialize for {{service.ident}}ServiceMethodReturn {
    fn deserialize<D>(de: &mut D) -> Result<Self, ::tokio_thrift::protocol::Error>
        where D: ::tokio_thrift::protocol::Deserializer + ::tokio_thrift::protocol::ThriftDeserializer,
    {
        let msg = de.read_message_begin()?;
        // if msg.type == return
        let ret = match msg.name.as_ref() {
            {{~#each service.methods as |method|}}
            "{{method.ident}}" => {{../service.ident}}ServiceMethodReturn::R{{method.ident}}(Ok({{to_rust method.ty}}::deserialize(de)?)),
            {{~/each}}
            _ => return Err(::tokio_thrift::protocol::Error::from(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "failed to parse thrift data"))),
        };
        // else msg.type == exception
        // FIXME:

        let _ = de.read_message_end()?;
        Ok(ret)
    }
}


{{#each service.methods as |method| ~}}
#[allow(dead_code, non_snake_case, non_camel_case_types)]
#[derive(Debug, Clone)]
pub struct {{../service.ident}}{{method.ident}}Args {
    {{~#each method.args as |arg|}}
    {{#if field.optional ~}}
    #[allow(dead_code, non_snake_case)]
    pub {{arg.ident}}: Option<{{to_rust arg.ty}}>,
    {{~^~}}
    #[allow(dead_code, non_snake_case)]
    pub {{arg.ident}}: {{to_rust arg.ty}},
    {{~/if~}}
    {{~/each}}
}
{{/each ~}}

{{#each service.methods as |method| ~}}
impl ::tokio_thrift::protocol::Serialize for {{../service.ident}}{{method.ident}}Args {
    fn serialize<S>(&self, s: &mut S) -> Result<(), ::tokio_thrift::protocol::Error>
        where S: ::tokio_thrift::protocol::Serializer + ::tokio_thrift::protocol::ThriftSerializer
    {
        s.write_struct_begin("{{../service.ident}}_{{method.ident}}_Args")?;
        {{#each method.args as |arg|~}}
        {{#if field.optional~}}
        if self.{{arg.ident}}.is_some() {
            s.write_field_begin("{{arg.ident}}", {{to_protocol arg.ty}}, {{arg.seq}})?;
            self.{{arg.ident}}.serialize(s)?;
            s.write_field_end()?;
        }
        {{~^~}}
        s.write_field_begin("{{arg.ident}}", {{to_protocol arg.ty}}, {{arg.seq}})?;
        self.{{arg.ident}}.serialize(s)?;
        s.write_field_end()?;
        {{/if}}
        {{~/each~}}
        s.write_field_stop()?;
        s.write_struct_end()?;
        Ok(())
    }
}
{{/each}}

{{#each service.methods as |method|}}
impl ::tokio_thrift::protocol::Deserialize for {{../service.ident}}{{method.ident}}Args {
    fn deserialize<D>(de: &mut D) -> Result<Self, ::tokio_thrift::protocol::Error>
        where D: ::tokio_thrift::protocol::Deserializer + ::tokio_thrift::protocol::ThriftDeserializer,
    {
        de.read_struct_begin()?;
        {{#each args as |arg|~}}
        let mut {{arg.ident}} = None;
        {{~/each}}
        loop {
            let scheme_field = de.read_field_begin()?;
            if scheme_field.ty == ::tokio_thrift::protocol::ThriftType::Stop {
                break;
            };
            match scheme_field.seq {
                {{#each args as |arg|~}}
                {{arg.seq}} => {
                    if scheme_field.ty == {{to_protocol arg.ty}} {
                        {{arg.ident}} = Some({{expr arg.ty}}?);
                    } else {
                        // skip
                    }
                },
                {{~/each}}
                _ => (),// skip
            }
            de.read_field_end()?;
        };
        de.read_struct_end()?;
        let args = {{../service.ident}}{{method.ident}}Args {
            {{#each args as |arg|~}}
            {{#if field.optional ~}}
            {{arg.ident}}: {{arg.ident}},
            {{~^~}}
            {{arg.ident}}: {{arg.ident}}.unwrap(),
            {{/if}}
            {{~/each}}
        };
        Ok(args)
    }
}
{{/each}}