Implement `FromStr` for types
TehPers opened this issue · 1 comments
I think it would be useful if, in addition to (or in place of?) the parse_str
methods on the data types (Date
, DateTime
, Duration
, Time
), there was a FromStr
implementation for the types. The implementation can be simple and just call the equivalent existing parse_str
methods:
impl FromStr for Duration {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse_str(s)
}
}
For Date
/DateTime
, one question is whether to call parse_str
or parse_str_rfc3339
. I think it would make sense to call the existing parse_str
methods instead of parse_str_rfc3339
since it encompasses the RFC 3339 formats as well, which would be a more permissive FromStr
implementation. A user could opt out of this support by calling parse_str_rfc3339
instead.
My use case is using these structs in combination with serde
(through serde_with
). If they implement both Display
and FromStr
, they can be serialized/deserialized using DisplayFromStr
:
#[serde_as]
#[derive(Serialize, Deserialize)]
struct Thing {
#[serde_as(as = "DisplayFromStr")]
pub published: Duration,
}
Sounds good, pr welcome.