@@ -17,7 +17,7 @@ use crate::rust::lib_gen::{Module, ModuleDef, ModuleName};
1717use crate :: rust:: model_gen:: RefCache ;
1818use crate :: rust:: printer:: * ;
1919use crate :: rust:: types:: {
20- ref_or_box_schema_type, ref_or_schema_type, DataType , RustPrinter , RustResult ,
20+ ref_or_box_schema_type, ref_or_schema_type, DataType , ModelType , RustPrinter , RustResult ,
2121} ;
2222use crate :: { Error , Result } ;
2323use convert_case:: { Case , Casing } ;
@@ -672,18 +672,69 @@ fn render_errors(method_name: &str, error_kind: &ErrorKind, errors: &MethodError
672672 . reduce ( |acc, e| acc + e)
673673 . unwrap_or_else ( unit) ;
674674
675+ let display_cases = errors
676+ . codes
677+ . iter ( )
678+ . map ( |( code, model) | {
679+ line (
680+ unit ( )
681+ + name. clone ( )
682+ + "::Error"
683+ + code. to_string ( )
684+ + "(body) => write!(f, \" {}\" , "
685+ + render_error_body_to_string ( model)
686+ + ")," ,
687+ )
688+ } )
689+ . reduce ( |acc, e| acc + e)
690+ . unwrap_or_else ( unit) ;
691+
675692 #[ rustfmt:: skip]
676693 let res = unit ( ) +
677694 line ( unit ( ) + "#[derive(Debug)]" ) +
678- line ( unit ( ) + "pub enum " + name + " {" ) +
695+ line ( unit ( ) + "pub enum " + name. clone ( ) + " {" ) +
679696 indented (
680697 code_cases
681698 ) +
682- line ( unit ( ) + "}" ) ;
699+ line ( unit ( ) + "}" ) +
700+ line ( unit ( ) + "impl std::fmt::Display for " + name. clone ( ) + " {" ) +
701+ indented (
702+ line ( unit ( ) + "fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {" ) +
703+ indented (
704+ if !errors. codes . is_empty ( ) {
705+ line ( unit ( ) + "match self {" ) +
706+ indented ( display_cases) +
707+ line ( unit ( ) + "}" )
708+ } else { line ( unit ( ) + indented ( unit ( ) + "write!(f, \" " + name + "\" )" ) ) }
709+ ) +
710+ line ( "}" )
711+ ) +
712+ line ( "}" ) ;
683713
684714 Ok ( res)
685715}
686716
717+ fn render_error_body_to_string ( typ : & DataType ) -> RustPrinter {
718+ match typ {
719+ DataType :: Model ( ModelType { name } ) if name == "ErrorBody" => unit ( ) + r#"&body.error"# ,
720+ DataType :: Model ( ModelType { name } ) if name == "ErrorsBody" => {
721+ unit ( ) + r#"body.errors.clone().join(", ")"#
722+ }
723+ DataType :: Model ( ModelType { name } ) if name == "WorkerServiceErrorsBody" => {
724+ unit ( )
725+ + "match &body { WorkerServiceErrorsBody::Messages("
726+ + rust_name ( "crate::model" , "ErrorsBody" )
727+ + " { errors }) => { errors.join(\" , \" ) }, WorkerServiceErrorsBody::Validation("
728+ + rust_name ( "crate::model" , "ErrorsBody" )
729+ + " { errors }) => { errors.join(\" , \" ) }}"
730+ }
731+ DataType :: Model ( ModelType { name } ) if name == "GolemErrorBody" => {
732+ unit ( ) + r#"format!("{}", body.golem_error)"#
733+ }
734+ _ => unit ( ) + r#"format!("{body:?}")"# ,
735+ }
736+ }
737+
687738fn async_annotation ( ) -> RustPrinter {
688739 unit ( ) + "#[" + rust_name ( "async_trait" , "async_trait" ) + "]"
689740}
0 commit comments