use crate::format::datetime::try_write_pattern_items;
pub use crate::format::DateTimeInputUnchecked;
use crate::pattern::*;
use crate::raw::neo::*;
use crate::scaffold::*;
use crate::DateTimeFormatter;
use crate::FixedCalendarDateTimeFormatter;
use core::fmt;
use icu_calendar::types::MonthCode;
use tinystr::TinyStr16;
use writeable::TryWriteable;
#[cfg(doc)]
use crate::fieldsets::enums::CompositeFieldSet;
#[cfg(doc)]
use crate::FormattedDateTime;
#[cfg(doc)]
use icu_calendar::types::CyclicYear;
#[cfg(doc)]
use icu_decimal::DecimalFormatter;
impl<C: CldrCalendar, FSet: DateTimeNamesMarker> FixedCalendarDateTimeFormatter<C, FSet> {
pub fn format_unchecked(
&self,
datetime: DateTimeInputUnchecked,
) -> FormattedDateTimeUnchecked<'_> {
FormattedDateTimeUnchecked {
pattern: self.selection.select(&datetime),
input: datetime,
names: self.names.as_borrowed(),
}
}
}
impl<FSet: DateTimeNamesMarker> DateTimeFormatter<FSet> {
pub fn format_unchecked(
&self,
datetime: DateTimeInputUnchecked,
) -> FormattedDateTimeUnchecked<'_> {
FormattedDateTimeUnchecked {
pattern: self.selection.select(&datetime),
input: datetime,
names: self.names.as_borrowed(),
}
}
}
#[derive(Debug)]
pub struct FormattedDateTimeUnchecked<'a> {
pattern: DateTimeZonePatternDataBorrowed<'a>,
input: DateTimeInputUnchecked,
names: RawDateTimeNamesBorrowed<'a>,
}
impl TryWriteable for FormattedDateTimeUnchecked<'_> {
type Error = FormattedDateTimeUncheckedError;
fn try_write_to_parts<S: writeable::PartsWrite + ?Sized>(
&self,
sink: &mut S,
) -> Result<Result<(), Self::Error>, fmt::Error> {
let err = match try_write_pattern_items(
self.pattern.metadata(),
self.pattern.iter_items(),
&self.input,
&self.names,
self.names.decimal_formatter,
sink,
) {
Ok(Ok(())) => return Ok(Ok(())),
Err(fmt::Error) => return Err(fmt::Error),
Ok(Err(err)) => err,
};
Ok(Err(match err {
FormattedDateTimePatternError::InvalidMonthCode(month_code) => {
Self::Error::InvalidMonthCode(month_code)
}
FormattedDateTimePatternError::InvalidEra(tiny_ascii_str) => {
Self::Error::InvalidEra(tiny_ascii_str)
}
FormattedDateTimePatternError::InvalidCyclicYear { value, max } => {
Self::Error::InvalidCyclicYear { value, max }
}
FormattedDateTimePatternError::DecimalFormatterNotLoaded => {
Self::Error::DecimalFormatterNotLoaded
}
FormattedDateTimePatternError::NamesNotLoaded(error_field) => {
Self::Error::NamesNotLoaded(error_field)
}
FormattedDateTimePatternError::MissingInputField(name) => {
Self::Error::MissingInputField(name)
}
FormattedDateTimePatternError::UnsupportedLength(error_field) => {
Self::Error::UnsupportedLength(error_field)
}
FormattedDateTimePatternError::UnsupportedField(error_field) => {
Self::Error::UnsupportedField(error_field)
}
}))
}
}
impl FormattedDateTimeUnchecked<'_> {
pub fn pattern(&self) -> DateTimePattern {
self.pattern.to_pattern()
}
}
#[non_exhaustive]
#[derive(Debug, PartialEq, Copy, Clone, displaydoc::Display)]
pub enum MissingInputFieldKind {
DayOfMonth,
DayOfYear,
RataDie,
Hour,
Minute,
Month,
Second,
Subsecond,
Weekday,
Year,
YearCyclic,
YearEra,
TimeZoneId,
TimeZoneNameTimestamp,
#[deprecated(since = "2.1.0", note = "unused, never returned")]
TimeZoneVariant,
}
#[non_exhaustive]
#[derive(Debug, PartialEq, Copy, Clone, displaydoc::Display)]
pub enum FormattedDateTimeUncheckedError {
#[displaydoc("Invalid month {0:?}")]
InvalidMonthCode(MonthCode),
#[displaydoc("Invalid era {0:?}")]
InvalidEra(TinyStr16),
#[displaydoc("Invalid cyclic year {value} (maximum {max})")]
InvalidCyclicYear {
value: u8,
max: u8,
},
#[displaydoc("Names for {0:?} not loaded")]
NamesNotLoaded(ErrorField),
#[displaydoc("DecimalFormatter not loaded")]
DecimalFormatterNotLoaded,
#[displaydoc("Incomplete input, missing value for {0:?}")]
MissingInputField(MissingInputFieldKind),
#[displaydoc("Unsupported field {0:?}")]
UnsupportedField(ErrorField),
#[displaydoc("Field length for {0:?} is invalid")]
UnsupportedLength(ErrorField),
}