1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

// Provider structs must be stable
#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]

//! Data provider struct definitions for this ICU4X component.
//!
//! Read more about data providers: [`icu_provider`]

use alloc::borrow::Cow;
use icu_provider::prelude::*;
use zerovec::ZeroMap;

#[cfg(feature = "compiled_data")]
#[derive(Debug)]
/// Baked data
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
pub struct Baked;

#[cfg(feature = "compiled_data")]
const _: () = {
    pub mod icu {
        pub use crate as relativetime;
        pub use icu_locid_transform as locid_transform;
    }
    icu_relativetime_data::make_provider!(Baked);
    icu_relativetime_data::impl_relativetime_long_day_v1!(Baked);
    icu_relativetime_data::impl_relativetime_long_hour_v1!(Baked);
    icu_relativetime_data::impl_relativetime_long_minute_v1!(Baked);
    icu_relativetime_data::impl_relativetime_long_month_v1!(Baked);
    icu_relativetime_data::impl_relativetime_long_quarter_v1!(Baked);
    icu_relativetime_data::impl_relativetime_long_second_v1!(Baked);
    icu_relativetime_data::impl_relativetime_long_week_v1!(Baked);
    icu_relativetime_data::impl_relativetime_long_year_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_day_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_hour_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_minute_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_month_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_quarter_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_second_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_week_v1!(Baked);
    icu_relativetime_data::impl_relativetime_narrow_year_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_day_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_hour_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_minute_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_month_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_quarter_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_second_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_week_v1!(Baked);
    icu_relativetime_data::impl_relativetime_short_year_v1!(Baked);
};

#[cfg(feature = "datagen")]
/// The latest minimum set of keys required by this component.
pub const KEYS: &[DataKey] = &[
    LongDayRelativeTimeFormatDataV1Marker::KEY,
    LongHourRelativeTimeFormatDataV1Marker::KEY,
    LongMinuteRelativeTimeFormatDataV1Marker::KEY,
    LongMonthRelativeTimeFormatDataV1Marker::KEY,
    LongQuarterRelativeTimeFormatDataV1Marker::KEY,
    LongSecondRelativeTimeFormatDataV1Marker::KEY,
    LongWeekRelativeTimeFormatDataV1Marker::KEY,
    LongYearRelativeTimeFormatDataV1Marker::KEY,
    NarrowDayRelativeTimeFormatDataV1Marker::KEY,
    NarrowHourRelativeTimeFormatDataV1Marker::KEY,
    NarrowMinuteRelativeTimeFormatDataV1Marker::KEY,
    NarrowMonthRelativeTimeFormatDataV1Marker::KEY,
    NarrowQuarterRelativeTimeFormatDataV1Marker::KEY,
    NarrowSecondRelativeTimeFormatDataV1Marker::KEY,
    NarrowWeekRelativeTimeFormatDataV1Marker::KEY,
    NarrowYearRelativeTimeFormatDataV1Marker::KEY,
    ShortDayRelativeTimeFormatDataV1Marker::KEY,
    ShortHourRelativeTimeFormatDataV1Marker::KEY,
    ShortMinuteRelativeTimeFormatDataV1Marker::KEY,
    ShortMonthRelativeTimeFormatDataV1Marker::KEY,
    ShortQuarterRelativeTimeFormatDataV1Marker::KEY,
    ShortSecondRelativeTimeFormatDataV1Marker::KEY,
    ShortWeekRelativeTimeFormatDataV1Marker::KEY,
    ShortYearRelativeTimeFormatDataV1Marker::KEY,
];

/// Relative time format V1 data struct.

#[icu_provider::data_struct(
    LongSecondRelativeTimeFormatDataV1Marker = "relativetime/long/second@1",
    ShortSecondRelativeTimeFormatDataV1Marker = "relativetime/short/second@1",
    NarrowSecondRelativeTimeFormatDataV1Marker = "relativetime/narrow/second@1",
    LongMinuteRelativeTimeFormatDataV1Marker = "relativetime/long/minute@1",
    ShortMinuteRelativeTimeFormatDataV1Marker = "relativetime/short/minute@1",
    NarrowMinuteRelativeTimeFormatDataV1Marker = "relativetime/narrow/minute@1",
    LongHourRelativeTimeFormatDataV1Marker = "relativetime/long/hour@1",
    ShortHourRelativeTimeFormatDataV1Marker = "relativetime/short/hour@1",
    NarrowHourRelativeTimeFormatDataV1Marker = "relativetime/narrow/hour@1",
    LongDayRelativeTimeFormatDataV1Marker = "relativetime/long/day@1",
    ShortDayRelativeTimeFormatDataV1Marker = "relativetime/short/day@1",
    NarrowDayRelativeTimeFormatDataV1Marker = "relativetime/narrow/day@1",
    LongWeekRelativeTimeFormatDataV1Marker = "relativetime/long/week@1",
    ShortWeekRelativeTimeFormatDataV1Marker = "relativetime/short/week@1",
    NarrowWeekRelativeTimeFormatDataV1Marker = "relativetime/narrow/week@1",
    LongMonthRelativeTimeFormatDataV1Marker = "relativetime/long/month@1",
    ShortMonthRelativeTimeFormatDataV1Marker = "relativetime/short/month@1",
    NarrowMonthRelativeTimeFormatDataV1Marker = "relativetime/narrow/month@1",
    LongQuarterRelativeTimeFormatDataV1Marker = "relativetime/long/quarter@1",
    ShortQuarterRelativeTimeFormatDataV1Marker = "relativetime/short/quarter@1",
    NarrowQuarterRelativeTimeFormatDataV1Marker = "relativetime/narrow/quarter@1",
    LongYearRelativeTimeFormatDataV1Marker = "relativetime/long/year@1",
    ShortYearRelativeTimeFormatDataV1Marker = "relativetime/short/year@1",
    NarrowYearRelativeTimeFormatDataV1Marker = "relativetime/narrow/year@1"
)]
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
    feature = "datagen", 
    derive(serde::Serialize, databake::Bake),
    databake(path = icu_relativetime::provider)
)]
#[yoke(prove_covariance_manually)]
pub struct RelativeTimePatternDataV1<'data> {
    /// Mapping for relative times with unique names.
    /// Example.
    /// In English, "-1" corresponds to "yesterday", "1" corresponds to "tomorrow".
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub relatives: ZeroMap<'data, i8, str>,
    /// How to display times in the past.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub past: PluralRulesCategoryMapping<'data>,
    /// How to display times in the future.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub future: PluralRulesCategoryMapping<'data>,
}

/// Display specification for relative times, split over potential plural patterns.
#[derive(Debug, Clone, Default, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
    feature = "datagen", 
    derive(serde::Serialize, databake::Bake),
    databake(path = icu_relativetime::provider)
)]
#[yoke(prove_covariance_manually)]
pub struct PluralRulesCategoryMapping<'data> {
    /// Mapping for PluralCategory::Zero or `None` if not present.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub zero: Option<SingularSubPattern<'data>>,
    /// Mapping for PluralCategory::One or `None` if not present.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub one: Option<SingularSubPattern<'data>>,
    /// Mapping for PluralCategory::Two or `None` if not present.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub two: Option<SingularSubPattern<'data>>,
    /// Mapping for PluralCategory::Few or `None` if not present.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub few: Option<SingularSubPattern<'data>>,
    /// Mapping for PluralCategory::Many or `None` if not present.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub many: Option<SingularSubPattern<'data>>,
    /// Mapping for PluralCategory::Other
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub other: SingularSubPattern<'data>,
}

/// Singular substitution for pattern that optionally uses "{0}" as a placeholder.
#[derive(Debug, Clone, Default, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
    feature = "datagen", 
    derive(serde::Serialize, databake::Bake),
    databake(path = icu_relativetime::provider)
)]
pub struct SingularSubPattern<'data> {
    #[cfg_attr(feature = "serde", serde(borrow))]
    /// The underlying pattern with the placeholder "{0}" removed.
    pub pattern: Cow<'data, str>,
    /// Denotes the substitution position in the pattern.
    /// Equals 255 if the pattern does not have a placeholder.
    pub index: u8,
}

impl<'data> SingularSubPattern<'data> {
    /// Construct a singular sub pattern from a pattern
    pub fn try_from_str(value: &str) -> Result<Self, DataError> {
        let (pattern, index) = if let Some(index) = value.find("{0}") {
            if index >= 255 {
                return Err(DataError::custom("Placeholder index too large to store."));
            }
            (
                format!("{}{}", &value[..index], &value[index + 3..]),
                index as u8,
            )
        } else {
            (value.to_string(), 255u8)
        };
        Ok(Self {
            pattern: Cow::Owned(pattern),
            index,
        })
    }
}

pub(crate) struct ErasedRelativeTimeFormatV1Marker;

impl DataMarker for ErasedRelativeTimeFormatV1Marker {
    type Yokeable = RelativeTimePatternDataV1<'static>;
}