Skip to content

Commit

Permalink
Add ISO 8601 parser for duration format with designators
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 23, 2024
1 parent c87f5a8 commit 850054f
Show file tree
Hide file tree
Showing 3 changed files with 330 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/calendar_duration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use core::fmt;
use core::num::NonZeroU64;
use core::str;
use core::time::Duration;

use crate::format::{parse_iso8601_duration, ParseError, TOO_LONG};
use crate::{expect, try_opt};

/// ISO 8601 duration type.
Expand Down Expand Up @@ -114,6 +116,18 @@ impl fmt::Display for CalendarDuration {
}
}

impl str::FromStr for CalendarDuration {
type Err = ParseError;

fn from_str(s: &str) -> Result<CalendarDuration, ParseError> {
let (s, duration) = parse_iso8601_duration(s)?;
if !s.is_empty() {
return Err(TOO_LONG);
}
Ok(duration)
}

Check warning on line 128 in src/calendar_duration.rs

View check run for this annotation

Codecov / codecov/patch

src/calendar_duration.rs#L122-L128

Added lines #L122 - L128 were not covered by tests
}

impl CalendarDuration {
/// Create a new duration initialized to `0`.
///
Expand Down
1 change: 1 addition & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub use formatting::{format, format_item, DelayedFormat};
pub use locales::Locale;
pub(crate) use parse::parse_rfc3339;
pub use parse::{parse, parse_and_remainder};
pub(crate) use parse_iso8601::parse_iso8601_duration;
pub use parsed::Parsed;
pub use strftime::StrftimeItems;

Expand Down

0 comments on commit 850054f

Please sign in to comment.