Skip to main content

DateFromFieldsError

Enum DateFromFieldsError 

Source
#[non_exhaustive]
pub enum DateFromFieldsError { InvalidDay { max: u8, }, InvalidOrdinalMonth { max: u8, }, MonthCodeInvalidSyntax, MonthNotInCalendar, MonthNotInYear, InvalidEra, InconsistentYear, InconsistentMonth, TooManyFields, NotEnoughFields, Overflow, }
Expand description

Error type for date creation via Date::try_from_fields.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

InvalidDay

The day is invalid for the given month.

§Examples

use icu::calendar::error::DateFromFieldsError;
use icu::calendar::error::RangeError;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
use icu::calendar::Iso;

let mut fields = DateFields::default();
fields.extended_year = Some(2000);
fields.ordinal_month = Some(11);
fields.day = Some(31);

let err = Date::try_from_fields(fields, Default::default(), Iso)
    .expect_err("no day 31 in November");

assert!(matches!(err, DateFromFieldsError::InvalidDay { max: 30 }));

Fields

§max: u8

The maximum allowed value (the minimum is 1).

§

InvalidOrdinalMonth

The ordinal month is is invalid for the given year.

§Examples

use icu::calendar::error::DateFromFieldsError;
use icu::calendar::error::RangeError;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
use icu::calendar::Iso;

let mut fields = DateFields::default();
fields.extended_year = Some(2000);
fields.ordinal_month = Some(13);
fields.day = Some(1);

let err = Date::try_from_fields(fields, Default::default(), Iso)
    .expect_err("no month 13 in the ISO calendar");

assert!(matches!(
    err,
    DateFromFieldsError::InvalidOrdinalMonth { max: 12 }
));

Fields

§max: u8

The maximum allowed value (the minimum is 1).

§

MonthCodeInvalidSyntax

The month code syntax is invalid.

§Examples

use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
use icu::calendar::Iso;

let mut fields = DateFields::default();
fields.extended_year = Some(2000);
fields.month_code = Some(b"sep");
fields.day = Some(1);

let err = Date::try_from_fields(fields, Default::default(), Iso)
    .expect_err("month code is invalid");

assert_eq!(err, DateFromFieldsError::MonthCodeInvalidSyntax);
§

MonthNotInCalendar

The specified month does not exist in this calendar.

§Examples

use icu::calendar::cal::Hebrew;
use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::{DateFields, Month};
use icu::calendar::Date;

let mut fields = DateFields::default();
fields.extended_year = Some(5783);
fields.month = Some(Month::new(13));
fields.day = Some(1);

let err = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect_err("no month M13 in Hebrew");
assert_eq!(err, DateFromFieldsError::MonthNotInCalendar);
§

MonthNotInYear

The specified month exists in this calendar, but not in the specified year.

§Examples

use icu::calendar::cal::Hebrew;
use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::{DateFields, Month};
use icu::calendar::Date;

let mut fields = DateFields::default();
fields.extended_year = Some(5783);
fields.month = Some(Month::leap(5));
fields.day = Some(1);

let err = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect_err("no month M05L in Hebrew year 5783");
assert_eq!(err, DateFromFieldsError::MonthNotInYear);
§

InvalidEra

The era code is invalid for the calendar.

§Examples

use icu::calendar::cal::Hebrew;
use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::DateFields;
use icu::calendar::Date;

let mut fields = DateFields::default();
fields.era = Some(b"ce"); // valid in Gregorian, but not Hebrew
fields.era_year = Some(1);
fields.ordinal_month = Some(1);
fields.day = Some(1);

let err = Date::try_from_fields(fields, Default::default(), Hebrew::new())
    .expect_err("era is unknown for Hebrew");

assert_eq!(err, DateFromFieldsError::InvalidEra);
§

InconsistentYear

The year was specified in multiple inconsistent ways.

§Examples

use icu::calendar::cal::Japanese;
use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::DateFields;
use icu::calendar::Date;

let mut fields = DateFields::default();
fields.era = Some(b"reiwa");
fields.era_year = Some(6);
fields.ordinal_month = Some(1);
fields.day = Some(1);

Date::try_from_fields(fields, Default::default(), Japanese::new())
    .expect("a well-defined Japanese date");

fields.extended_year = Some(1900);

let err =
    Date::try_from_fields(fields, Default::default(), Japanese::new())
        .expect_err("year 1900 is not the same as 6 Reiwa");

assert_eq!(err, DateFromFieldsError::InconsistentYear);
§

InconsistentMonth

The month was specified in multiple inconsistent ways.

§Examples

use icu::calendar::cal::Hebrew;
use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::{DateFields, Month};
use icu::calendar::Date;
use tinystr::tinystr;

let mut fields = DateFields::default();
fields.extended_year = Some(5783);
fields.month = Some(Month::new(6));
fields.ordinal_month = Some(6);
fields.day = Some(1);

Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect("a well-defined Hebrew date in a common year");

fields.extended_year = Some(5784);

let err = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect_err("month M06 is not the 6th month in leap year 5784");

assert_eq!(err, DateFromFieldsError::InconsistentMonth);
§

TooManyFields

Too many fields were specified to form a well-defined date.

§Examples

use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::{DateFields, Month};
use icu::calendar::Date;
use icu::calendar::Iso;

let mut fields = DateFields::default();
fields.extended_year = Some(2000);
fields.month = Some(Month::new(1));
fields.month_code = Some(b"M01");
fields.day = Some(1);

let err = Date::try_from_fields(fields, Default::default(), Iso)
    .expect_err("cannot specify both month and month_code");

assert_eq!(err, DateFromFieldsError::TooManyFields);
§

NotEnoughFields

Not enough fields were specified to form a well-defined date.

§Examples

use icu::calendar::cal::Hebrew;
use icu::calendar::error::DateFromFieldsError;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
use tinystr::tinystr;

let mut fields = DateFields::default();

fields.ordinal_month = Some(3);

let err = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect_err("need more than just an ordinal month");
assert_eq!(err, DateFromFieldsError::NotEnoughFields);

fields.era_year = Some(5783);

let err = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect_err("need more than an ordinal month and an era year");
assert_eq!(err, DateFromFieldsError::NotEnoughFields);

fields.extended_year = Some(5783);

let err = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect_err("era year still needs an era");
assert_eq!(err, DateFromFieldsError::NotEnoughFields);

fields.era = Some(b"am");

let date = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect_err("still missing the day");
assert_eq!(err, DateFromFieldsError::NotEnoughFields);

fields.day = Some(1);
let date = Date::try_from_fields(fields, Default::default(), Hebrew)
    .expect("we have enough fields!");
§

Overflow

The date is out of range (see docs on Date for more information about Date’s fundamental range invariant).

§Examples

use icu::calendar::error::DateFromFieldsError;
use icu::calendar::error::RangeError;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
use icu::calendar::Iso;

let mut fields = DateFields::default();
fields.extended_year = Some(12345678);
fields.ordinal_month = Some(12);
fields.day = Some(31);

let err = Date::try_from_fields(fields, Default::default(), Iso)
    .expect_err("date out of range");

assert!(matches!(
    err,
    DateFromFieldsError::Overflow
));

Trait Implementations§

Source§

impl Clone for DateFromFieldsError

Source§

fn clone(&self) -> DateFromFieldsError

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DateFromFieldsError

Source§

impl Debug for DateFromFieldsError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for DateFromFieldsError

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Error for DateFromFieldsError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<EcmaReferenceYearError> for DateFromFieldsError

Source§

fn from(value: EcmaReferenceYearError) -> DateFromFieldsError

Converts to this type from the input type.
Source§

impl From<MonthCodeParseError> for DateFromFieldsError

Source§

fn from(value: MonthCodeParseError) -> DateFromFieldsError

Converts to this type from the input type.
Source§

impl From<MonthError> for DateFromFieldsError

Source§

fn from(value: MonthError) -> DateFromFieldsError

Converts to this type from the input type.
Source§

impl From<UnknownEraError> for DateFromFieldsError

Source§

fn from(_value: UnknownEraError) -> DateFromFieldsError

Converts to this type from the input type.
Source§

impl PartialEq for DateFromFieldsError

Source§

fn eq(&self, other: &DateFromFieldsError) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for DateFromFieldsError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.