icu_datetime/provider/neo/
adapter.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use crate::provider::calendar::*;
6use crate::provider::neo::*;
7use alloc::vec;
8use icu_calendar::types::MonthCode;
9
10impl<'a> From<&months::Symbols<'a>> for MonthNames<'a> {
11    fn from(other: &months::Symbols<'a>) -> Self {
12        match other {
13            months::Symbols::SolarTwelve(cow_list) => {
14                // Can't zero-copy convert a cow list to a VarZeroVec, so we need to allocate
15                // a new VarZeroVec. Since VarZeroVec does not implement `from_iter`, first we
16                // make a Vec of string references.
17                let vec: alloc::vec::Vec<&str> = cow_list.iter().map(|x| &**x).collect();
18                MonthNames::Linear((&vec).into())
19            }
20            months::Symbols::Other(zero_map) => {
21                // Only calendar that uses this is hebrew, we can assume it is 12-month
22                let mut vec = vec![""; 24];
23
24                for (k, v) in zero_map.iter() {
25                    let Some((number, leap)) = MonthCode(*k).parsed() else {
26                        debug_assert!(false, "Found unknown month code {k}");
27                        continue;
28                    };
29                    let offset = if leap { 12 } else { 0 };
30                    if let Some(entry) = vec.get_mut((number + offset - 1) as usize) {
31                        *entry = v;
32                    } else {
33                        debug_assert!(false, "Found out of bounds hebrew month code {k}")
34                    }
35                }
36                MonthNames::LeapLinear((&vec).into())
37            }
38        }
39    }
40}
41
42impl<'a> From<&weekdays::Symbols<'a>> for LinearNames<'a> {
43    fn from(other: &weekdays::Symbols<'a>) -> Self {
44        // Input is a cow array of length 7. Need to make it a VarZeroVec.
45        let vec: alloc::vec::Vec<&str> = other.0.iter().map(|x| &**x).collect();
46        LinearNames {
47            names: (&vec).into(),
48        }
49    }
50}
51
52impl<'a> From<&day_periods::Symbols<'a>> for LinearNames<'a> {
53    fn from(other: &day_periods::Symbols<'a>) -> Self {
54        // Input is a struct with four fields. Need to make it a VarZeroVec.
55        let vec: alloc::vec::Vec<&str> = match (other.noon.as_ref(), other.midnight.as_ref()) {
56            (Some(noon), Some(midnight)) => vec![&other.am, &other.pm, &noon, &midnight],
57            (Some(noon), None) => vec![&other.am, &other.pm, &noon],
58            (None, Some(midnight)) => vec![&other.am, &other.pm, "", &midnight],
59            (None, None) => vec![&other.am, &other.pm],
60        };
61        LinearNames {
62            names: (&vec).into(),
63        }
64    }
65}