From f10228ed61ffe5910fe0cca8c14675c5025b73a1 Mon Sep 17 00:00:00 2001 From: Tin Rabzelj Date: Tue, 6 Feb 2024 08:27:12 +0100 Subject: [PATCH] Fix status details --- bomboni_proto/build.rs | 36 ++++++++++++++++++++----------- bomboni_proto/src/lib.rs | 2 +- bomboni_proto/src/protobuf/any.rs | 1 + bomboni_wasm/Cargo.toml | 3 +++ bomboni_wasm/src/lib.rs | 2 +- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/bomboni_proto/build.rs b/bomboni_proto/build.rs index 9f65ecf..bdfe95d 100644 --- a/bomboni_proto/build.rs +++ b/bomboni_proto/build.rs @@ -122,22 +122,13 @@ fn build_serde(config: &mut Config) { ".google.rpc.Status", r"#[derive(::serde::Serialize, ::serde::Deserialize)]", ); - - config.field_attribute( - ".google.rpc.Status.details", - r#"#[serde(with = "crate::google::rpc::status::details_serde")]"#, - ); config.field_attribute( ".google.rpc.Status.code", r#"#[serde(with = "crate::google::rpc::helpers::code_serde")]"#, ); config.field_attribute( - ".tools.Status", - r#"#[serde(with = "crate::tools::helpers::status_serde")]"#, - ); - config.field_attribute( - ".tools.CommandResponse.Status", - r#"#[serde(with = "crate::tools::helpers::command_response::status_serde")]"#, + ".google.rpc.Status.details", + r#"#[serde(with = "crate::rpc::status::details_serde")]"#, ); } @@ -149,7 +140,7 @@ fn get_copy_type_paths() -> impl Iterator { #[cfg(feature = "wasm")] fn build_wasm(config: &mut Config) { - for type_name in [ + let error_details = [ "Status", "RetryInfo", "DebugInfo", @@ -161,7 +152,8 @@ fn build_wasm(config: &mut Config) { "ResourceInfo", "Help", "LocalizedMessage", - ] { + ]; + for type_name in &error_details { config.type_attribute( format!(".google.rpc.{type_name}"), r" @@ -170,4 +162,22 @@ fn build_wasm(config: &mut Config) { ", ); } + config.field_attribute( + ".google.rpc.Status.code", + "#[wasm(override_type = \"string\")]", + ); + config.field_attribute( + ".google.rpc.Status.details", + format!( + "#[wasm(override_type = \"(\n{})[]\")]", + error_details + .iter() + .skip(1) + .map(|type_name| format!( + " ( {{'@type': 'type.googleapis.com/google.rpc.{type_name}';}} & ({type_name}) )\n" + )) + .collect::>() + .join(" | ") + ), + ); } diff --git a/bomboni_proto/src/lib.rs b/bomboni_proto/src/lib.rs index b713e7c..4fba639 100644 --- a/bomboni_proto/src/lib.rs +++ b/bomboni_proto/src/lib.rs @@ -1,5 +1,5 @@ mod protobuf; -mod rpc; +pub mod rpc; pub mod serde; /// Includes generated protobuf code. diff --git a/bomboni_proto/src/protobuf/any.rs b/bomboni_proto/src/protobuf/any.rs index 1d50139..5c7d79c 100644 --- a/bomboni_proto/src/protobuf/any.rs +++ b/bomboni_proto/src/protobuf/any.rs @@ -168,6 +168,7 @@ macro_rules! impl_proto_any_seq_serde { $any_serde::serialize(self.0, serializer) } } + let mut seq = serializer.serialize_seq(Some(details.len()))?; for detail in details { seq.serialize_element(&Proxy(detail))?; diff --git a/bomboni_wasm/Cargo.toml b/bomboni_wasm/Cargo.toml index dcf2c85..0c85ead 100644 --- a/bomboni_wasm/Cargo.toml +++ b/bomboni_wasm/Cargo.toml @@ -28,3 +28,6 @@ bomboni_wasm_derive = { path = "../bomboni_wasm_derive", version = "0.1.54", opt [dev-dependencies] serde = { version = "1.0.196", features = ["derive"] } serde_json = "1.0.113" +bomboni_proto = { path = "../bomboni_proto", features = [ + "wasm", +], version = "0.1.54" } diff --git a/bomboni_wasm/src/lib.rs b/bomboni_wasm/src/lib.rs index 5dc5843..a1b00c4 100644 --- a/bomboni_wasm/src/lib.rs +++ b/bomboni_wasm/src/lib.rs @@ -23,7 +23,7 @@ pub trait Wasm { #[cfg(feature = "derive")] pub use bomboni_wasm_derive::*; -#[cfg(test)] +#[cfg(all(test, feature = "derive"))] mod tests { use super::*; use serde::{Deserialize, Serialize};