Skip to content

Commit

Permalink
Rename FormatItem to BorrowedFormatItem
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Mar 30, 2024
1 parent fd664ee commit 06a096d
Show file tree
Hide file tree
Showing 21 changed files with 396 additions and 329 deletions.
20 changes: 10 additions & 10 deletions tests/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::Hash;

use time::error::{self, ConversionRange, IndeterminateOffset, TryFromParsed};
use time::ext::NumericalDuration;
use time::format_description::{self, modifier, well_known, Component, FormatItem, OwnedFormatItem};
use time::format_description::{self, modifier, well_known, Component, BorrowedFormatItem, OwnedFormatItem};
use time::macros::{date, offset, time};
use time::parsing::Parsed;
use time::{Duration, Error, Month, Time, Weekday};
Expand Down Expand Up @@ -64,7 +64,7 @@ fn clone() {
assert_cloned_eq!(well_known::iso8601::OffsetPrecision::Hour);
assert_cloned_eq!(well_known::iso8601::FormattedComponents::None);
assert_cloned_eq!(component_range_error());
assert_cloned_eq!(FormatItem::Literal(b""));
assert_cloned_eq!(BorrowedFormatItem::Literal(b""));
assert_cloned_eq!(time::util::local_offset::Soundness::Sound);

assert_cloned_eq!(modifier::Day::default());
Expand Down Expand Up @@ -189,13 +189,13 @@ fn debug() {
modifier::OffsetSecond::default();
modifier::Padding::default();

FormatItem::Literal(b"abcdef");
FormatItem::Compound(&[FormatItem::Component(Component::Day(modifier::Day::default()))]);
FormatItem::Optional(&FormatItem::Compound(&[]));
FormatItem::First(&[]);
OwnedFormatItem::from(FormatItem::Literal(b"abcdef"));
OwnedFormatItem::from(FormatItem::Compound(&[FormatItem::Component(Component::Day(modifier::Day::default()))]));
OwnedFormatItem::from(FormatItem::Optional(&FormatItem::Compound(&[])));
OwnedFormatItem::from(FormatItem::First(&[]));
BorrowedFormatItem::Literal(b"abcdef");
BorrowedFormatItem::Compound(&[BorrowedFormatItem::Component(Component::Day(modifier::Day::default()))]);
BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(&[]));
BorrowedFormatItem::First(&[]);
OwnedFormatItem::from(BorrowedFormatItem::Literal(b"abcdef"));
OwnedFormatItem::from(BorrowedFormatItem::Compound(&[BorrowedFormatItem::Component(Component::Day(modifier::Day::default()))]));
OwnedFormatItem::from(BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(&[])));
OwnedFormatItem::from(BorrowedFormatItem::First(&[]));
}
}
26 changes: 13 additions & 13 deletions tests/format_description.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use time::format_description::{modifier, Component, FormatItem, OwnedFormatItem};
use time::format_description::{modifier, BorrowedFormatItem, Component, OwnedFormatItem};

#[test]
fn borrowed_format_item_component_conversions() {
let component = Component::Year(modifier::Year::default());
let item = FormatItem::from(component);
assert!(matches!(item, FormatItem::Component(inner) if inner == component));
let item = BorrowedFormatItem::from(component);
assert!(matches!(item, BorrowedFormatItem::Component(inner) if inner == component));
assert_eq!(Component::try_from(item), Ok(component));
assert!(Component::try_from(FormatItem::Literal(b"")).is_err());
assert!(<&[FormatItem<'_>]>::try_from(FormatItem::Literal(b"")).is_err());
assert!(Component::try_from(BorrowedFormatItem::Literal(b"")).is_err());
assert!(<&[BorrowedFormatItem<'_>]>::try_from(BorrowedFormatItem::Literal(b"")).is_err());
}

#[test]
fn borrowed_format_item_compound_conversions() {
let compound = [FormatItem::Literal(b"")].as_slice();
let item = FormatItem::from(compound);
assert!(matches!(item, FormatItem::Compound(inner) if inner == compound));
assert_eq!(<&[FormatItem<'_>]>::try_from(item), Ok(compound));
let compound = [BorrowedFormatItem::Literal(b"")].as_slice();
let item = BorrowedFormatItem::from(compound);
assert!(matches!(item, BorrowedFormatItem::Compound(inner) if inner == compound));
assert_eq!(<&[BorrowedFormatItem<'_>]>::try_from(item), Ok(compound));
}

#[test]
fn borrowed_format_item_equality() {
let component = Component::Year(modifier::Year::default());
let compound = [FormatItem::Literal(b"")].as_slice();
let component_item = FormatItem::from(component);
let compound_item = FormatItem::from(compound);
let compound = [BorrowedFormatItem::Literal(b"")].as_slice();
let component_item = BorrowedFormatItem::from(component);
let compound_item = BorrowedFormatItem::from(compound);

assert_eq!(component, component_item);
assert_eq!(component_item, component);
Expand Down Expand Up @@ -52,7 +52,7 @@ fn owned_format_item_compound_conversions() {
#[test]
fn owned_format_item_equality() {
let component = Component::Year(modifier::Year::default());
let compound = OwnedFormatItem::from([FormatItem::Literal(b"")].as_slice());
let compound = OwnedFormatItem::from([BorrowedFormatItem::Literal(b"")].as_slice());
let component_item = OwnedFormatItem::from(component);

assert_eq!(component, component_item);
Expand Down
26 changes: 15 additions & 11 deletions tests/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;

use time::format_description::well_known::iso8601::{DateKind, OffsetPrecision, TimePrecision};
use time::format_description::well_known::{iso8601, Iso8601, Rfc2822, Rfc3339};
use time::format_description::{self, FormatItem, OwnedFormatItem};
use time::format_description::{self, BorrowedFormatItem, OwnedFormatItem};
use time::macros::{date, datetime, format_description as fd, offset, time};
use time::{OffsetDateTime, Time};

Expand Down Expand Up @@ -562,9 +562,9 @@ fn insufficient_type_information() {
assert_insufficient_type_information(Time::MIDNIGHT.format(&Rfc2822));
assert_insufficient_type_information(date!(2021 - 001).format(&Rfc2822));
assert_insufficient_type_information(datetime!(2021 - 001 0:00).format(&Rfc2822));
assert_insufficient_type_information(
Time::MIDNIGHT.format(&FormatItem::First(&[FormatItem::Compound(fd!("[year]"))])),
);
assert_insufficient_type_information(Time::MIDNIGHT.format(&BorrowedFormatItem::First(&[
BorrowedFormatItem::Compound(fd!("[year]")),
])));
assert_insufficient_type_information(Time::MIDNIGHT.format(&Iso8601::DEFAULT));
assert_insufficient_type_information(date!(2021 - 001).format(&Iso8601::DEFAULT));
assert_insufficient_type_information(datetime!(2021-001 0:00).format(&Iso8601::DEFAULT));
Expand All @@ -591,14 +591,16 @@ fn failed_write() -> time::Result<()> {

assert_err!(Time::MIDNIGHT, fd!("foo"));
assert_err!(Time::MIDNIGHT, OwnedFormatItem::from(fd!("foo")));
assert_err!(Time::MIDNIGHT, FormatItem::Compound(fd!("foo")));
assert_err!(Time::MIDNIGHT, BorrowedFormatItem::Compound(fd!("foo")));
assert_err!(
Time::MIDNIGHT,
FormatItem::Optional(&FormatItem::Compound(fd!("foo")))
BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(fd!("foo")))
);
assert_err!(
Time::MIDNIGHT,
OwnedFormatItem::from(FormatItem::Optional(&FormatItem::Compound(fd!("foo"))))
OwnedFormatItem::from(BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(
fd!("foo")
)))
);
assert_err!(OffsetDateTime::UNIX_EPOCH, Rfc3339);
assert_err!(datetime!(2021-001 0:00:00.1 UTC), Rfc3339);
Expand Down Expand Up @@ -720,18 +722,20 @@ fn failed_write() -> time::Result<()> {

#[test]
fn first() -> time::Result<()> {
assert_eq!(Time::MIDNIGHT.format(&FormatItem::First(&[]))?, "");
assert_eq!(Time::MIDNIGHT.format(&BorrowedFormatItem::First(&[]))?, "");
assert_eq!(
Time::MIDNIGHT.format(&FormatItem::First(&[FormatItem::Compound(fd!("[hour]"))]))?,
Time::MIDNIGHT.format(&BorrowedFormatItem::First(&[BorrowedFormatItem::Compound(
fd!("[hour]")
)]))?,
"00"
);
assert_eq!(
Time::MIDNIGHT.format(&OwnedFormatItem::First(Box::new([])))?,
""
);
assert_eq!(
Time::MIDNIGHT.format(&OwnedFormatItem::from(FormatItem::First(&[
FormatItem::Compound(fd!("[hour]"))
Time::MIDNIGHT.format(&OwnedFormatItem::from(BorrowedFormatItem::First(&[
BorrowedFormatItem::Compound(fd!("[hour]"))
])))?,
"00"
);
Expand Down
Loading

0 comments on commit 06a096d

Please sign in to comment.