{{! Copyright (c) Meta Platforms, Inc. and its affiliates. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. }} {{#service:interactions}}{{>lib/service}} {{/service:interactions}} /// Service definitions for `{{service:rust_name}}`. pub mod {{service:snake}} {{>lib/block}}{{! }}{{#service:docs?}} #![doc = {{service:docs}}] {{/service:docs?}}{{! }}{{#service:rustFunctions}}{{^function:starts_interaction?}}{{#function:stream?}} #[derive(Clone, Debug)] pub enum {{function:upcamel}}StreamExn { #[doc(hidden)] Success({{#function:stream_elem_type}}{{>lib/type}}{{/function:stream_elem_type}}),{{! }}{{#function:stream_exceptions}} {{field:rust_name}}({{#field:type}}{{>lib/type}}{{/field:type}}),{{! }}{{/function:stream_exceptions}} ApplicationException(::fbthrift::ApplicationException), } impl ::fbthrift::ExceptionInfo for {{function:upcamel}}StreamExn { fn exn_name(&self) -> &'static str { match self { Self::Success(_) => panic!("ExceptionInfo::exn_name called on Success"), Self::ApplicationException(aexn) => aexn.exn_name(), {{#function:stream_exceptions}} Self::{{field:rust_name}}(exn) => exn.exn_name(), {{/function:stream_exceptions}} } } fn exn_value(&self) -> String { match self { Self::Success(_) => panic!("ExceptionInfo::exn_value called on Success"), Self::ApplicationException(aexn) => aexn.exn_value(), {{#function:stream_exceptions}} Self::{{field:rust_name}}(exn) => exn.exn_value(), {{/function:stream_exceptions}} } } fn exn_is_declared(&self) -> bool { match self { Self::Success(_) => panic!("ExceptionInfo::exn_is_declared called on Success"), Self::ApplicationException(aexn) => aexn.exn_is_declared(), {{#function:stream_exceptions}} Self::{{field:rust_name}}(exn) => exn.exn_is_declared(), {{/function:stream_exceptions}} } } } impl ::fbthrift::ResultInfo for {{function:upcamel}}StreamExn { fn result_type(&self) -> ::fbthrift::ResultType { match self { Self::Success(_) => ::fbthrift::ResultType::Return, Self::ApplicationException(_aexn) => ::fbthrift::ResultType::Exception, {{#function:stream_exceptions}} Self::{{field:rust_name}}(_exn) => fbthrift::ResultType::Error, {{/function:stream_exceptions}} } } }{{! }}{{#function:uniqueStreamExceptions}} impl ::std::convert::From<{{#field:type}}{{>lib/type}}{{/field:type}}> for {{function:upcamel}}StreamExn { fn from(exn: {{#field:type}}{{>lib/type}}{{/field:type}}) -> Self { Self::{{field:rust_name}}(exn) } }{{! }}{{/function:uniqueStreamExceptions}} impl ::std::convert::From<::fbthrift::ApplicationException> for {{function:upcamel}}StreamExn { fn from(exn: ::fbthrift::ApplicationException) -> Self { Self::ApplicationException(exn) } } {{#function:check_service_for_enable_anyhow_to_application_exn}}{{#service:enable_anyhow_to_application_exn}} impl ::std::convert::From<::anyhow::Error> for {{function:upcamel}}StreamExn { fn from(exn: ::anyhow::Error) -> Self { Self::ApplicationException( ::fbthrift::ApplicationException { message: format!("{exn:#}"), type_: ::fbthrift::ApplicationExceptionErrorCode::Unknown, } ) } } {{/service:enable_anyhow_to_application_exn}}{{/function:check_service_for_enable_anyhow_to_application_exn}} {{^function:check_service_for_enable_anyhow_to_application_exn}}{{#function:enable_anyhow_to_application_exn}} impl ::std::convert::From<::anyhow::Error> for {{function:upcamel}}StreamExn { fn from(exn: ::anyhow::Error) -> Self { Self::ApplicationException( ::fbthrift::ApplicationException { message: format!("{exn:#}"), type_: ::fbthrift::ApplicationExceptionErrorCode::Unknown, } ) } } {{/function:enable_anyhow_to_application_exn}}{{/function:check_service_for_enable_anyhow_to_application_exn}} impl ::fbthrift::GetTType for {{function:upcamel}}StreamExn { const TTYPE: ::fbthrift::TType = ::fbthrift::TType::Struct; } impl
::fbthrift::Serialize
for {{function:upcamel}}StreamExn where P: ::fbthrift::ProtocolWriter, { fn write(&self, p: &mut P) { if let Self::ApplicationException(aexn) = self { return aexn.write(p); } p.write_struct_begin("{{function:upcamel}}"); match self { Self::Success(inner) => { p.write_field_begin( "Success", {{#function:stream_elem_type}}{{>lib/ttype}}{{/function:stream_elem_type}}, 0i16, ); inner.write(p); p.write_field_end(); }{{! }}{{#function:stream_exceptions}} Self::{{field:rust_name}}(inner) => { p.write_field_begin( "{{field:name}}", {{#field:type}}{{>lib/ttype}}{{/field:type}}, {{field:key}}, ); inner.write(p); p.write_field_end(); }{{! }}{{/function:stream_exceptions}} Self::ApplicationException(_) => unreachable!(), } p.write_field_stop(); p.write_struct_end(); } } impl
::fbthrift::Deserialize
for {{function:upcamel}}StreamExn
where
P: ::fbthrift::ProtocolReader,
{
fn read(p: &mut P) -> ::anyhow::Result ::fbthrift::Serialize for {{function:upcamel}}ResponseExn
where
P: ::fbthrift::ProtocolWriter,
{
fn write(&self, p: &mut P) {
if let Self::ApplicationException(aexn) = self {
return aexn.write(p);
}
p.write_struct_begin("{{function:upcamel}}");
match self {
Self::Success(_inner) => {
p.write_field_begin(
"Success",
{{#function:stream_has_first_response?}}
{{#function:return_type}}{{#function:stream_first_response_type}}{{>lib/ttype}}{{/function:stream_first_response_type}}{{/function:return_type}},
{{/function:stream_has_first_response?}}
{{^function:stream_has_first_response?}}
::fbthrift::TType::Void,
{{/function:stream_has_first_response?}}
0i16,
);
{{#function:stream_has_first_response?}}
_inner.write(p);
{{/function:stream_has_first_response?}}
p.write_field_end();
}{{!
}}{{#function:exceptions}}
Self::{{field:rust_name}}(inner) => {
p.write_field_begin(
"{{field:name}}",
{{#field:type}}{{>lib/ttype}}{{/field:type}},
{{field:key}},
);
inner.write(p);
p.write_field_end();
}{{!
}}{{/function:exceptions}}
Self::ApplicationException(_) => unreachable!(),
}
p.write_field_stop();
p.write_struct_end();
}
}
impl ::fbthrift::Deserialize for {{function:upcamel}}ResponseExn
where
P: ::fbthrift::ProtocolReader,
{
fn read(p: &mut P) -> ::anyhow::Result ::fbthrift::Serialize for {{function:upcamel}}Exn
where
P: ::fbthrift::ProtocolWriter,
{
fn write(&self, p: &mut P) {
if let Self::ApplicationException(aexn) = self {
return aexn.write(p);
}
p.write_struct_begin("{{function:upcamel}}");
match self {
{{!We handle serialization of response and stream items with a separate logic.
However, the service exceptions can have the same serialization logic}}
Self::Success(inner) => {
p.write_field_begin(
"Success",
{{#function:return_type}}{{>lib/ttype}}{{/function:return_type}},
0i16,
);
{{#function:return_type}}{{#type:has_adapter?}}::fbthrift::Serialize::write(&{{>lib/adapter/qualified}}::to_thrift_field:: ::fbthrift::Deserialize for {{function:upcamel}}Exn
where
P: ::fbthrift::ProtocolReader,
{
fn read(p: &mut P) -> ::anyhow::Result