From 1c0db6940353d3d5dc5040776ebfcdf6f1040ab5 Mon Sep 17 00:00:00 2001 From: rochdev Date: Thu, 15 Aug 2024 17:47:34 -0400 Subject: [PATCH] improve crashtracker config deserialization --- crashtracker/src/crash_info/metadata.rs | 2 +- crashtracker/src/shared/configuration.rs | 10 ++++--- ddcommon/src/lib.rs | 34 +++++++++++++++--------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/crashtracker/src/crash_info/metadata.rs b/crashtracker/src/crash_info/metadata.rs index c56435ba1..a0e8d99d0 100644 --- a/crashtracker/src/crash_info/metadata.rs +++ b/crashtracker/src/crash_info/metadata.rs @@ -3,7 +3,7 @@ use ddcommon::tag::Tag; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] pub struct CrashtrackerMetadata { pub library_name: String, pub library_version: String, diff --git a/crashtracker/src/shared/configuration.rs b/crashtracker/src/shared/configuration.rs index 2df937bc2..8cecce79a 100644 --- a/crashtracker/src/shared/configuration.rs +++ b/crashtracker/src/shared/configuration.rs @@ -9,16 +9,17 @@ use serde::{Deserialize, Serialize}; /// We recommend fully enabling stacktrace collection, but having an environment /// variable to allow downgrading the collector. #[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum StacktraceCollection { /// Stacktrace collection occurs in the - Disabled, + #[default] Disabled, WithoutSymbols, EnabledWithInprocessSymbols, EnabledWithSymbolsInReceiver, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +#[serde(default)] pub struct CrashtrackerConfiguration { // Paths to any additional files to track, if any pub additional_files: Vec, @@ -28,7 +29,8 @@ pub struct CrashtrackerConfiguration { pub wait_for_receiver: bool, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] +#[serde(default)] pub struct CrashtrackerReceiverConfig { pub args: Vec, pub env: Vec<(String, String)>, diff --git a/ddcommon/src/lib.rs b/ddcommon/src/lib.rs index 089de4252..1198a05f0 100644 --- a/ddcommon/src/lib.rs +++ b/ddcommon/src/lib.rs @@ -62,6 +62,13 @@ struct SerializedUri<'a> { path_and_query: Option>, } +#[derive(Serialize, Deserialize)] +#[serde(untagged)] +enum StringOrSerializedUri<'a> { + String(String), + SerializedUri(SerializedUri<'a>) +} + fn serialize_uri(uri: &hyper::Uri, serializer: S) -> Result where S: Serializer, @@ -82,19 +89,22 @@ fn deserialize_uri<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { - let uri = SerializedUri::deserialize(deserializer)?; - let mut builder = hyper::Uri::builder(); - if let Some(v) = uri.authority { - builder = builder.authority(v.deref()); - } - if let Some(v) = uri.scheme { - builder = builder.scheme(v.deref()); - } - if let Some(v) = uri.path_and_query { - builder = builder.path_and_query(v.deref()); + match StringOrSerializedUri::deserialize(deserializer)? { + StringOrSerializedUri::String(str) => str.parse().map_err(Error::custom), + StringOrSerializedUri::SerializedUri(uri) => { + let mut builder = hyper::Uri::builder(); + if let Some(v) = uri.authority { + builder = builder.authority(v.deref()); + } + if let Some(v) = uri.scheme { + builder = builder.scheme(v.deref()); + } + if let Some(v) = uri.path_and_query { + builder = builder.path_and_query(v.deref()); + } + builder.build().map_err(Error::custom) + } } - - builder.build().map_err(Error::custom) } /// TODO: we should properly handle malformed urls