Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"fmtorp"
"fmtorp",
"messageonly"
]
}
43 changes: 43 additions & 0 deletions trace4rs-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,47 @@ pub struct Logger {
pub format: Format,
}

macro_rules! named_unit_variant {
($variant:ident) => {
pub mod $variant {
pub fn serialize<S>(serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(stringify!($variant))
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<(), D::Error>
where
D: serde::Deserializer<'de>,
{
struct V;
impl<'de> serde::de::Visitor<'de> for V {
type Value = ();

fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(concat!("\"", stringify!($variant), "\""))
}

fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
if value == stringify!($variant) {
Ok(())
} else {
Err(E::invalid_value(serde::de::Unexpected::Str(value), &self))
}
}
}
deserializer.deserialize_str(V)
}
}
};
}

mod format {
named_unit_variant!(normal);
named_unit_variant!(messageonly);
}

#[derive(PartialEq, Eq, Clone, Debug, SmartDefault)]
#[cfg_attr(
feature = "serde",
Expand All @@ -163,7 +204,9 @@ pub struct Logger {
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum Format {
#[default]
#[serde(with = "format::normal")]
Normal,
#[serde(with = "format::messageonly")]
MessageOnly,
Custom(String),
}
Expand Down