Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duration and Epoch serde to be human readable #217

Closed
ChristopherRabotin opened this issue Apr 8, 2023 · 1 comment
Closed

Duration and Epoch serde to be human readable #217

ChristopherRabotin opened this issue Apr 8, 2023 · 1 comment

Comments

@ChristopherRabotin
Copy link
Member

At the moment, the serde of a Duration exposes its internal representation, and is not very convenient for a human to read. In Nyx, I had to resort to adding the following methods to serde Epoch and Duration to be human readable:

pub(crate) fn duration_to_str<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_str(&format!("{duration}"))
}

/// A deserializer from Duration string
pub(crate) fn duration_from_str<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
    D: Deserializer<'de>,
{
    // implementation of the custom deserialization function
    let s = String::deserialize(deserializer)?;
    Duration::from_str(&s).map_err(serde::de::Error::custom)
}

This should be the default behavior of serde for Duration and Epoch.

This is a breaking change because it will break the current serde users.

@ChristopherRabotin
Copy link
Member Author

The serde capability should be kept with a crate feature like serde-3.9 or something making it clear which version introduced the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant