pub fn compile_generated_code(&self) -> String {
        let mut code = String::new();

        write!(
            code,
            r#"
pub enum TryFromProtoError {{
    InvalidUuid,
    MissingField(String),
}}

impl ::std::fmt::Display for TryFromProtoError {{
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {{
        match self {{
            Self::InvalidUuid => write!(f, "invalid uuid"),
            Self::MissingField(field) => write!(f, "missing field '{{}}'", field),
        }}
    }}
}}
        "#
        )
        .unwrap();

        for (model, _) in self.protobuf_messages() {
            let ident = format_ident!("{}", model.name);

            let mut from_rust_fields = Vec::new();
            let mut from_proto_fields = Vec::new();

            for field in &model.fields {
                let field_ident = format_ident!("{}", field.name);
                let field_ident_string = &field.name;

                let ty = strip_ty_option(&field.ty);

                match ty {
                    "chrono::NaiveDateTime"
                    | "NaiveDateTime"
                    | "chrono::DateTime<chrono::FixedOffset>"
                    | "chrono::DateTime<FixedOffset>"
                    | "DateTime<chrono::FixedOffset>"
                    | "DateTime<FixedOffset>" => {
                        from_rust_fields.push(quote!(
                            #field_ident: Some(::prost_types::Timestamp {
                                nanos: val.#field_ident.timestamp_nanos() as i32,
                                seconds: val.#field_ident.timestamp(),
                            })
                        ));
                        from_proto_fields.push(quote!(
                        #field_ident: {
                            let unwrapped_value = val.#field_ident.ok_or_else(|| TryFromProtoError::MissingField(#field_ident_string.to_string()))?;
                            ::chrono::DateTime::from_utc(
                                ::chrono::naive::NaiveDateTime::from_timestamp(
                                    unwrapped_value.seconds,
                                    unwrapped_value.nanos as u32
                                ),
                                ::chrono::FixedOffset::east(0),
                            )
                        }
                    ));
                    }
                    "uuid::Uuid" | "Uuid" => {
                        from_rust_fields.push(quote!(
                            #field_ident: val.#field_ident.to_string()
                        ));
                        from_proto_fields.push(quote!(
                        #field_ident: ::uuid::Uuid::parse_str(&val.#field_ident).map_err(|_| TryFromProtoError::InvalidUuid)?
                    ));
                    }
                    _ => {
                        if is_ty_vec(ty) {
                            from_rust_fields.push(quote!(#field_ident: val.#field_ident.into_iter().map(|v| v.into()).collect()));
                            from_proto_fields.push(quote!(#field_ident: val.#field_ident.into_iter().map(|v| ::std::convert::TryFrom::try_from(v)).collect::<Result<_, _>>()?));
                        } else {
                            from_rust_fields.push(quote!(#field_ident: val.#field_ident.into()));
                            from_proto_fields.push(quote!(#field_ident: val.#field_ident.into()));
                        }
                    }
                }
            }

            let expanded = quote!(
                impl ::std::convert::TryFrom<#ident> for ::schema::#ident {
                    type Error = TryFromProtoError;

                    #[allow(unused_variables)]
                    fn try_from(val: #ident) -> Result<Self, Self::Error> {
                        Ok(Self {
                            #( #from_proto_fields, )*
                        })
                    }
                }

                impl ::std::convert::From<::schema::#ident> for #ident {
                    #[allow(unused_variables)]
                    fn from(val: ::schema::#ident) -> Self {
                        Self {
                            #( #from_rust_fields, )*
                        }
                    }
                }
            );

            write!(code, "{}", expanded.to_string()).unwrap();
        }

        for service in &self.services {
            let ident = format_ident!("{}", service.name);
            let service_path: TokenStream = service.module_path.parse().unwrap();
            let service_server_name = format_ident!("{}_server", service.name.to_snake_case());

            let mut methods = Vec::new();
            for method in &service.methods {
                let name_ident = format_ident!("{}", method.name.to_snake_case());
                let param_ident = format_ident!("{}", method.param.name);
                let returns_ident = format_ident!("{}", method.returns.name);

                let expanded_call_method = if method.returns_result {
                    if method.is_async {
                        quote!(self.#name_ident(param).await?)
                    } else {
                        quote!(self.#name_ident(param)?)
                    }
                } else if method.is_async {
                    quote!(self.#name_ident(param).await)
                } else {
                    quote!(self.#name_ident(param))
                };

                methods.push(quote!(
                    async fn #name_ident(
                        &self,
                        request: ::tonic::Request<#param_ident>,
                    ) -> Result<::tonic::Response<#returns_ident>, ::tonic::Status> {
                        let inner = request.into_inner();
                        let param = ::std::convert::TryInto::try_into(inner).map_err(|err: TryFromProtoError| ::tonic::Status::invalid_argument(err.to_string()))?;
                        let value = #expanded_call_method;
                        Ok(::tonic::Response::new(value.into()))
                    }
                ));
            }

            let expanded = quote!(
                #[::tonic::async_trait]
                impl #service_server_name::#ident for #service_path::#ident {
                    #( #methods )*
                }
            );

            write!(code, "{}", expanded.to_string()).unwrap();
        }

        code.trim().to_string()
    }
/// Compiles generated Rust code from schemas and services.
Compiles generated Rust code from schemas and services.
Compiles generated Rust code from schemas and services .
pub fn compile_generated_code(&self) -> String {
        let mut code = String::new();
        write!(
            code,
            r#"
pub enum TryFromProtoError {{
    InvalidUuid,
    MissingField(String),
}}
impl ::std::fmt::Display for TryFromProtoError {{
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {{
        match self {{
            Self::InvalidUuid => write!(f, "invalid uuid"),
            Self::MissingField(field) => write!(f, "missing field '{{}}'", field),
        }}
    }}
}}
        "#
        )
        .unwrap();
        for (model, _) in self.protobuf_messages() {
            let ident = format_ident!("{}", model.name);
            let mut from_rust_fields = Vec::new();
            let mut from_proto_fields = Vec::new();
            for field in &model.fields {
                let field_ident = format_ident!("{}", field.name);
                let field_ident_string = &field.name;
                let ty = strip_ty_option(&field.ty);
                match ty {
                    "chrono::NaiveDateTime"
                    | "NaiveDateTime"
                    | "chrono::DateTime<chrono::FixedOffset>"
                    | "chrono::DateTime<FixedOffset>"
                    | "DateTime<chrono::FixedOffset>"
                    | "DateTime<FixedOffset>" => {
                        from_rust_fields.push(quote!(
                            #field_ident: Some(::prost_types::Timestamp {
                                nanos: val.#field_ident.timestamp_nanos() as i32,
                                seconds: val.#field_ident.timestamp(),
                            })
                        ));
                        from_proto_fields.push(quote!(
                        #field_ident: {
                            let unwrapped_value = val.#field_ident.ok_or_else(|| TryFromProtoError::MissingField(#field_ident_string.to_string()))?;
                            ::chrono::DateTime::from_utc(
                                ::chrono::naive::NaiveDateTime::from_timestamp(
                                    unwrapped_value.seconds,
                                    unwrapped_value.nanos as u32
                                ),
                                ::chrono::FixedOffset::east(0),
                            )
                        }
                    ));
                    }
                    "uuid::Uuid" | "Uuid" => {
                        from_rust_fields.push(quote!(
                            #field_ident: val.#field_ident.to_string()
                        ));
                        from_proto_fields.push(quote!(
                        #field_ident: ::uuid::Uuid::parse_str(&val.#field_ident).map_err(|_| TryFromProtoError::InvalidUuid)?
                    ));
                    }
                    _ => {
                        if is_ty_vec(ty) {
                            from_rust_fields.push(quote!(#field_ident: val.#field_ident.into_iter().map(|v| v.into()).collect()));
                            from_proto_fields.push(quote!(#field_ident: val.#field_ident.into_iter().map(|v| ::std::convert::TryFrom::try_from(v)).collect::<Result<_, _>>()?));
                        } else {
                            from_rust_fields.push(quote!(#field_ident: val.#field_ident.into()));
                            from_proto_fields.push(quote!(#field_ident: val.#field_ident.into()));
                        }
                    }
                }
            }
            let expanded = quote!(
                impl ::std::convert::TryFrom<#ident> for ::schema::#ident {
                    type Error = TryFromProtoError;
                    #[allow(unused_variables)]
                    fn try_from(val: #ident) -> Result<Self, Self::Error> {
                        Ok(Self {
                            #( #from_proto_fields, )*
                        })
                    }
                }
                impl ::std::convert::From<::schema::#ident> for #ident {
                    #[allow(unused_variables)]
                    fn from(val: ::schema::#ident) -> Self {
                        Self {
                            #( #from_rust_fields, )*
                        }
                    }
                }
            );
            write!(code, "{}", expanded.to_string()).unwrap();
        }
        for service in &self.services {
            let ident = format_ident!("{}", service.name);
            let service_path: TokenStream = service.module_path.parse().unwrap();
            let service_server_name = format_ident!("{}_server", service.name.to_snake_case());
            let mut methods = Vec::new();
            for method in &service.methods {
                let name_ident = format_ident!("{}", method.name.to_snake_case());
                let param_ident = format_ident!("{}", method.param.name);
                let returns_ident = format_ident!("{}", method.returns.name);
                let expanded_call_method = if method.returns_result {
                    if method.is_async {
                        quote!(self.#name_ident(param).await?)
                    } else {
                        quote!(self.#name_ident(param)?)
                    }
                } else if method.is_async {
                    quote!(self.#name_ident(param).await)
                } else {
                    quote!(self.#name_ident(param))
                };
                methods.push(quote!(
                    async fn #name_ident(
                        &self,
                        request: ::tonic::Request<#param_ident>,
                    ) -> Result<::tonic::Response<#returns_ident>, ::tonic::Status> {
                        let inner = request.into_inner();
                        let param = ::std::convert::TryInto::try_into(inner).map_err(|err: TryFromProtoError| ::tonic::Status::invalid_argument(err.to_string()))?;
                        let value = #expanded_call_method;
                        Ok(::tonic::Response::new(value.into()))
                    }
                ));
            }
            let expanded = quote!(
                #[::tonic::async_trait]
                impl #service_server_name::#ident for #service_path::#ident {
                    #( #methods )*
                }
            );
            write!(code, "{}", expanded.to_string()).unwrap();
        }
        code.trim().to_string()
    }
pub fn compile_generated_code ( & self ) -> String { let mut code = String :: new ( ) ; write ! ( code r#"
pub enum TryFromProtoError {{
    InvalidUuid,
    MissingField(String),
}}

impl ::std::fmt::Display for TryFromProtoError {{
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {{
        match self {{
            Self::InvalidUuid => write!(f, "invalid uuid"),
            Self::MissingField(field) => write!(f, "missing field '{{}}'", field),
        }}
    }}
}}
        "# ) . unwrap ( ) ; for ( model , _ ) in self . protobuf_messages ( ) { let ident = format_ident ! ( " " model name ) ; let mut from_rust_fields = Vec :: new ( ) ; let mut from_proto_fields = Vec :: new ( ) ; for field in & model . fields { let field_ident = format_ident ! ( " " field name ) ; let field_ident_string = & field . name ; let ty = strip_ty_option ( & field . ty ) ; match ty { " " | " " | " " | " " | " " | " " => { from_rust_fields . push ( quote ! ( field_ident Some ( prost_types Timestamp { nanos val field_ident timestamp_nanos ( ) as i32 seconds val field_ident timestamp ( ) } ) ) ) ; from_proto_fields . push ( quote ! ( field_ident { let unwrapped_value val field_ident ok_or_else ( TryFromProtoError MissingField ( field_ident_string to_string ( ) ) ) chrono DateTime from_utc ( chrono naive NaiveDateTime from_timestamp ( unwrapped_value seconds unwrapped_value nanos as u32 ) chrono FixedOffset east ( 0 ) ) } ) ) ; } " " | " " => { from_rust_fields . push ( quote ! ( field_ident val field_ident to_string ( ) ) ) ; from_proto_fields . push ( quote ! ( field_ident uuid Uuid parse_str ( val field_ident ) map_err ( TryFromProtoError InvalidUuid ) ) ) ; } _ => { if is_ty_vec ( ty ) { from_rust_fields . push ( quote ! ( field_ident val field_ident into_iter ( ) map ( v v into ( ) ) collect ( ) ) ) ; from_proto_fields . push ( quote ! ( field_ident val field_ident into_iter ( ) map ( v std convert TryFrom try_from ( v ) ) collect Result ( ) ) ) ; } else { from_rust_fields . push ( quote ! ( field_ident val field_ident into ( ) ) ) ; from_proto_fields . push ( quote ! ( field_ident val field_ident into ( ) ) ) ; } } } } let expanded = quote ! ( impl std convert TryFrom ident for schema ident { type Error TryFromProtoError [ allow ( unused_variables ) ] fn try_from ( val ident ) Result Self Self Error { Ok ( Self { ( from_proto_fields ) } ) } } impl std convert From schema ident for ident { [ allow ( unused_variables ) ] fn from ( val schema ident ) Self { Self { ( from_rust_fields ) } } } ) ; write ! ( code " " expanded to_string ( ) ) . unwrap ( ) ; } for service in & self . services { let ident = format_ident ! ( " " service name ) ; let service_path : TokenStream = service . module_path . parse ( ) . unwrap ( ) ; let service_server_name = format_ident ! ( " " service name to_snake_case ( ) ) ; let mut methods = Vec :: new ( ) ; for method in & service . methods { let name_ident = format_ident ! ( " " method name to_snake_case ( ) ) ; let param_ident = format_ident ! ( " " method param name ) ; let returns_ident = format_ident ! ( " " method returns name ) ; let expanded_call_method = if method . returns_result { if method . is_async { quote ! ( self name_ident ( param ) await ) } else { quote ! ( self name_ident ( param ) ) } } else if method . is_async { quote ! ( self name_ident ( param ) await ) } else { quote ! ( self name_ident ( param ) ) } ; methods . push ( quote ! ( async fn name_ident ( self request tonic Request param_ident ) Result tonic Response returns_ident tonic Status { let inner request into_inner ( ) let param std convert TryInto try_into ( inner ) map_err ( err TryFromProtoError tonic Status invalid_argument ( err to_string ( ) ) ) let value expanded_call_method Ok ( tonic Response new ( value into ( ) ) ) } ) ) ; } let expanded = quote ! ( [ tonic async_trait ] impl service_server_name ident for service_path ident { ( methods ) } ) ; write ! ( code " " expanded to_string ( ) ) . unwrap ( ) ; } code . trim ( ) . to_string ( ) }
Compiles generated Rust code from schemas and services.
Compiles generated Rust code from schemas and services .
&self
&self