Skip to content

Commit

Permalink
improve crashtracker config deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Aug 15, 2024
1 parent 16528ff commit 7fbf47a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crashtracker/src/crash_info/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions crashtracker/src/shared/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -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<String>,
pub env: Vec<(String, String)>,
Expand Down
34 changes: 22 additions & 12 deletions ddcommon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ struct SerializedUri<'a> {
path_and_query: Option<Cow<'a, str>>,
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum StringOrSerializedUri<'a> {
String(String),
SerializedUri(SerializedUri<'a>)
}

fn serialize_uri<S>(uri: &hyper::Uri, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -82,19 +89,22 @@ fn deserialize_uri<'de, D>(deserializer: D) -> Result<hyper::Uri, D::Error>
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
Expand Down

0 comments on commit 7fbf47a

Please sign in to comment.