tailhook / humantime

A parser and formatter for std::time::{SystemTime, Duration}
Apache License 2.0
283 stars 34 forks source link

Expose inner values from formatting wrappers #18

Closed NeoLegends closed 3 years ago

NeoLegends commented 3 years ago

Hey! 👋

First of all, thank you for this great library!

We're using this library together with thiserror to provide nicely formatted durations & timestamps in our error messages, for example, like this:

#[derive(Debug, thiserror::Error)]
enum Error {
  #[error("operation timed out after {0}")]
  Timeout(FormattedDuration),
}

This works great, but limits programmatic usability of the error enum, since once a duration (or system time) is formatted, there is no way to access it again, because the library doesn't expose them. If we ever wanted to programmatically process timeout errors we'd need to store the duration value we're formatting as well, which isn't really nice because we'd be doubling the size of that variant and there is no static guarantee that those values are actually the same.

Therefore this PR adds two accessor methods to the formatting wrappers that expose the time values that are being formatted.

tailhook commented 3 years ago

I'm like this. Just a little bike-shedding on the names. Should we add a pretty standard get_ref()?

tailhook commented 3 years ago

Merged. Thanks!