mssun commented on issue #201: Use a template for proto_gen to make it more readable/configurable URL: https://github.com/apache/incubator-teaclave/issues/201#issuecomment-570886432 Fixed by 3aad0d8. This is the current template for the proto generation: ``` use anyhow; use teaclave_rpc; use core::convert::TryFrom; #[derive(Clone, serde_derive::Serialize, serde_derive::Deserialize, Debug)] #[serde(tag = "type", rename_all = "snake_case")] pub enum {{ service.proto_name }}Request { {% for m in service.methods %} {{ m.proto_name }}({{ m.input_type }}), {% endfor %} } #[derive(Clone, serde_derive::Serialize, serde_derive::Deserialize, Debug)] #[serde(tag = "type", rename_all = "snake_case")] pub enum {{ service.proto_name }}Response { {% for m in service.methods %} {{ m.proto_name }}({{ m.output_type }}), {% endfor %} } pub trait {{ service.proto_name }} { {% for m in service.methods %} fn {{ m.name }}(request: crate::{{ m.input_type }}) -> anyhow::Result<crate::{{ m.output_type }}>; {% endfor %} fn dispatch(&self, request: {{ service.proto_name }}Request) -> anyhow::Result<{{ service.proto_name }}Response> { match request { {% for m in service.methods %} {{ service.proto_name }}Request::{{ m.proto_name }}(request) => { let request = crate::{{ m.input_type }}::try_from(request)?; let response = {{ m.output_type }}::from(Self::{{ m.name }}(request)?); Ok(response).map({{ service.proto_name }}Response::{{ m.proto_name }}) }, {% endfor %} } } } pub struct {{ service.proto_name }}Client { channel: teaclave_rpc::channel::SgxTrustedTlsChannel<{{ service.proto_name }}Request, {{ service.proto_name }}Response>, } impl {{ service.proto_name }}Client { pub fn new(channel: teaclave_rpc::channel::SgxTrustedTlsChannel<{{ service.proto_name }}Request, {{ service.proto_name }}Response>) -> anyhow::Result<Self> { Ok(Self { channel }) } {% for m in service.methods %} pub fn {{ m.name }}(&mut self, request: {{ m.input_type }}) -> anyhow::Result<{{ m.output_type }}> { let request = {{ service.proto_name }}Request::UserLogin(request); let response = self.channel.invoke(request)?; match response { {{ service.proto_name }}Response::{{ m.proto_name }}(response) => Ok(response), {% if service.methods.len() > 1 -%} _ => Err(anyhow!("rpc error")), {% endif %} } } {% endfor %} } ```
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
