Skip to content

Commit 26bdb84

Browse files
committed
Refactor ConflictingField error to contain the previously loaded field
1 parent 9c48ed5 commit 26bdb84

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

Diff for: components/datetime/src/pattern/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ pub enum PatternLoadError {
7373
/// Fields conflict if they require the same type of data, for example the
7474
/// `EEE` and `EEEE` fields (short vs long weekday) conflict, or the `M`
7575
/// and `L` (format vs standalone month) conflict.
76-
#[displaydoc("A field {0:?} conflicts with a previous field.")]
77-
ConflictingField(ErrorField),
76+
#[displaydoc(
77+
"A field {requested_field:?} conflicts with a previously loaded field {loaded_field:?}."
78+
)]
79+
ConflictingField {
80+
requested_field: ErrorField,
81+
loaded_field: ErrorField,
82+
},
7883
/// The field symbol is not supported in that length.
7984
///
8085
/// Some fields, such as `O` are not defined for all lengths (e.g. `OO`).

Diff for: components/datetime/src/scaffold/names_storage.rs

+61-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,45 @@ pub trait DateTimeNamesMarker: UnstableSealed {
4343
type MetazoneLookup: NamesContainer<tz::MzPeriodV1, ()>;
4444
}
4545

46+
/// A trait for `Variables` that can be converted to [`ErrorField`]
47+
pub trait MaybeAsErrorField: UnstableSealed {
48+
fn maybe_as_error_field(&self) -> Option<ErrorField>;
49+
}
50+
51+
impl MaybeAsErrorField for () {
52+
fn maybe_as_error_field(&self) -> Option<ErrorField> {
53+
None
54+
}
55+
}
56+
57+
impl UnstableSealed for YearNameLength {}
58+
impl MaybeAsErrorField for YearNameLength {
59+
fn maybe_as_error_field(&self) -> Option<ErrorField> {
60+
Some(self.to_approximate_error_field())
61+
}
62+
}
63+
64+
impl UnstableSealed for MonthNameLength {}
65+
impl MaybeAsErrorField for MonthNameLength {
66+
fn maybe_as_error_field(&self) -> Option<ErrorField> {
67+
Some(self.to_approximate_error_field())
68+
}
69+
}
70+
71+
impl UnstableSealed for WeekdayNameLength {}
72+
impl MaybeAsErrorField for WeekdayNameLength {
73+
fn maybe_as_error_field(&self) -> Option<ErrorField> {
74+
Some(self.to_approximate_error_field())
75+
}
76+
}
77+
78+
impl UnstableSealed for DayPeriodNameLength {}
79+
impl MaybeAsErrorField for DayPeriodNameLength {
80+
fn maybe_as_error_field(&self) -> Option<ErrorField> {
81+
Some(self.to_approximate_error_field())
82+
}
83+
}
84+
4685
/// Trait that associates a container for a payload parameterized by the given variables.
4786
///
4887
/// <div class="stab unstable">
@@ -69,7 +108,7 @@ macro_rules! impl_holder_trait {
69108
impl UnstableSealed for $marker {}
70109
impl<Variables> NamesContainer<$marker, Variables> for $marker
71110
where
72-
Variables: PartialEq + Copy + fmt::Debug,
111+
Variables: PartialEq + Copy + MaybeAsErrorField + fmt::Debug,
73112
{
74113
type Container = DataPayloadWithVariables<$marker, Variables>;
75114
}
@@ -97,10 +136,10 @@ impl_holder_trait!(tz::MzPeriodV1);
97136
#[derive(Debug, displaydoc::Display)]
98137
#[non_exhaustive]
99138
pub enum MaybePayloadError {
100-
/// TODO
139+
/// The container's field set doesn't support the field
101140
FormatterTooSpecific,
102-
/// TODO
103-
ConflictingField,
141+
/// The field is already loaded with a different length
142+
ConflictingField(ErrorField),
104143
}
105144

106145
impl core::error::Error for MaybePayloadError {}
@@ -109,7 +148,10 @@ impl MaybePayloadError {
109148
pub(crate) fn into_load_error(self, error_field: ErrorField) -> PatternLoadError {
110149
match self {
111150
Self::FormatterTooSpecific => PatternLoadError::FormatterTooSpecific(error_field),
112-
Self::ConflictingField => PatternLoadError::ConflictingField(error_field),
151+
Self::ConflictingField(loaded_field) => PatternLoadError::ConflictingField {
152+
requested_field: error_field,
153+
loaded_field,
154+
},
113155
}
114156
}
115157
}
@@ -200,7 +242,7 @@ where
200242
impl<M: DynamicDataMarker, Variables> MaybePayload<M, Variables>
201243
for DataPayloadWithVariables<M, Variables>
202244
where
203-
Variables: PartialEq + Copy,
245+
Variables: PartialEq + Copy + MaybeAsErrorField,
204246
{
205247
#[inline]
206248
fn new_empty() -> Self {
@@ -224,8 +266,19 @@ where
224266
// TODO(#6063): probably not correct
225267
return Ok(Ok(Default::default()));
226268
}
227-
OptionalNames::SingleLength { .. } => {
228-
return Err(MaybePayloadError::ConflictingField);
269+
OptionalNames::SingleLength { variables, .. } => {
270+
let loaded_field = match variables.maybe_as_error_field() {
271+
Some(x) => x,
272+
None => {
273+
debug_assert!(false, "all non-unit variables implement this trait");
274+
use crate::provider::fields::*;
275+
ErrorField(Field {
276+
symbol: FieldSymbol::Era,
277+
length: FieldLength::Six,
278+
})
279+
}
280+
};
281+
return Err(MaybePayloadError::ConflictingField(loaded_field));
229282
}
230283
OptionalNames::None => (),
231284
};

0 commit comments

Comments
 (0)