• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zbraniecki / icu4x / 8611306223

09 Apr 2024 06:09AM UTC coverage: 76.234% (+0.2%) from 75.985%
8611306223

push

github

web-flow
Updating tutorial lock files (#4784)

Needed for https://github.com/unicode-org/icu4x/pull/4776

51696 of 67812 relevant lines covered (76.23%)

512021.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.11
/components/datetime/src/pattern/runtime/pattern.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

5
#![allow(clippy::exhaustive_structs)] // part of data struct and internal API
6

7
use super::super::{reference, PatternError, PatternItem, TimeGranularity};
8
use alloc::vec::Vec;
9
use core::str::FromStr;
10
use icu_provider::prelude::*;
11
use zerovec::{ule::AsULE, ZeroSlice, ZeroVec};
12

13
#[derive(Debug, PartialEq, Eq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
69,511✔
14
#[cfg_attr(
15
    feature = "datagen",
16
    derive(databake::Bake),
×
17
    databake(path = icu_datetime::pattern::runtime),
18
)]
19
#[zerovec::make_varule(PatternULE)]
5,940✔
20
#[zerovec::derive(Debug)]
21
#[zerovec::skip_derive(Ord)]
22
#[cfg_attr(feature = "serde", zerovec::derive(Deserialize))]
23
#[cfg_attr(feature = "datagen", zerovec::derive(Serialize))]
24
pub struct Pattern<'data> {
25
    pub items: ZeroVec<'data, PatternItem>,
41,610✔
26
    /// This field should contain the smallest time unit from the `items` vec.
27
    /// If it doesn't, unexpected results for day periods may be encountered.
28
    pub metadata: PatternMetadata,
41,610✔
29
}
30

31
#[derive(Debug, Copy, Clone)]
×
32
pub struct PatternBorrowed<'data> {
33
    pub items: &'data ZeroSlice<PatternItem>,
×
34
    pub metadata: PatternMetadata,
×
35
}
36

37
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
55,343✔
38
#[zerovec::make_ule(PatternMetadataULE)]
4,242✔
39
#[zerovec::skip_derive(Ord)]
40
pub struct PatternMetadata(u8);
14,082✔
41

42
impl PatternMetadata {
43
    pub(crate) const DEFAULT: PatternMetadata = Self::from_time_granularity(TimeGranularity::None);
44

45
    #[inline]
46
    pub(crate) fn time_granularity(self) -> TimeGranularity {
14,170✔
47
        TimeGranularity::from_ordinal(self.0)
14,170✔
48
    }
14,170✔
49

50
    pub(crate) fn from_items(items: &[PatternItem]) -> Self {
484✔
51
        let time_granularity: TimeGranularity =
52
            items.iter().map(Into::into).max().unwrap_or_default();
484✔
53
        Self::from_time_granularity(time_granularity)
484✔
54
    }
484✔
55

56
    /// Merges the metadata from a date pattern and a time pattern into one.
57
    #[cfg(feature = "experimental")]
58
    #[inline]
59
    pub(crate) fn merge_date_and_time_metadata(
68✔
60
        _date: PatternMetadata,
61
        time: PatternMetadata,
62
    ) -> PatternMetadata {
63
        // Currently we only have time granularity so we ignore the date metadata.
64
        time
65
    }
68✔
66

67
    #[inline]
68
    #[doc(hidden)] // databake
69
    pub const fn from_time_granularity(time_granularity: TimeGranularity) -> Self {
36,210✔
70
        Self(time_granularity.ordinal())
36,210✔
71
    }
36,210✔
72

73
    #[cfg(any(feature = "datagen", feature = "experimental"))]
74
    #[inline]
75
    pub(crate) fn set_time_granularity(&mut self, time_granularity: TimeGranularity) {
9✔
76
        self.0 = time_granularity.ordinal();
9✔
77
    }
9✔
78
}
79

80
impl Default for PatternMetadata {
81
    #[inline]
82
    fn default() -> Self {
277✔
83
        Self::DEFAULT
84
    }
277✔
85
}
86

87
impl<'data> Pattern<'data> {
88
    pub fn into_owned(self) -> Pattern<'static> {
313✔
89
        Pattern {
313✔
90
            items: self.items.into_owned(),
313✔
91
            metadata: self.metadata,
313✔
92
        }
93
    }
313✔
94

95
    pub fn as_borrowed(&self) -> PatternBorrowed {
92✔
96
        PatternBorrowed {
92✔
97
            items: &self.items,
92✔
98
            metadata: self.metadata,
92✔
99
        }
100
    }
92✔
101
}
102

103
impl PatternULE {
104
    pub fn as_borrowed(&self) -> PatternBorrowed {
2✔
105
        PatternBorrowed {
2✔
106
            items: &self.items,
2✔
107
            metadata: PatternMetadata::from_unaligned(self.metadata),
2✔
108
        }
109
    }
2✔
110
}
111

112
impl<'data> PatternBorrowed<'data> {
113
    #[cfg(feature = "experimental")]
114
    pub(crate) const DEFAULT: PatternBorrowed<'static> = PatternBorrowed {
115
        items: ZeroSlice::new_empty(),
116
        metadata: PatternMetadata::DEFAULT,
117
    };
118

119
    pub fn to_pattern(&self) -> Pattern<'data> {
1✔
120
        Pattern {
1✔
121
            items: self.items.as_zerovec(),
1✔
122
            metadata: self.metadata,
1✔
123
        }
124
    }
1✔
125
}
126

127
impl From<Vec<PatternItem>> for Pattern<'_> {
128
    fn from(items: Vec<PatternItem>) -> Self {
484✔
129
        Self {
484✔
130
            metadata: PatternMetadata::from_items(&items),
484✔
131
            items: ZeroVec::alloc_from_slice(&items),
484✔
132
        }
133
    }
484✔
134
}
135

136
impl From<&reference::Pattern> for Pattern<'_> {
137
    fn from(input: &reference::Pattern) -> Self {
22,481✔
138
        Self {
22,481✔
139
            items: ZeroVec::alloc_from_slice(&input.items),
22,481✔
140
            metadata: PatternMetadata::from_time_granularity(input.time_granularity),
22,481✔
141
        }
×
142
    }
22,481✔
143
}
144

145
impl From<&Pattern<'_>> for reference::Pattern {
146
    fn from(input: &Pattern<'_>) -> Self {
306✔
147
        Self {
306✔
148
            items: input.items.to_vec(),
306✔
149
            time_granularity: input.metadata.time_granularity(),
306✔
150
        }
×
151
    }
306✔
152
}
153

154
impl FromStr for Pattern<'_> {
155
    type Err = PatternError;
156

157
    fn from_str(input: &str) -> Result<Self, Self::Err> {
22,110✔
158
        let reference = crate::pattern::reference::Pattern::from_str(input)?;
22,110✔
159
        Ok(Self::from(&reference))
22,110✔
160
    }
22,110✔
161
}
162

163
impl Default for Pattern<'_> {
164
    fn default() -> Self {
277✔
165
        Self {
277✔
166
            items: ZeroVec::new(),
277✔
167
            metadata: PatternMetadata::default(),
277✔
168
        }
×
169
    }
277✔
170
}
171

172
#[cfg(feature = "datagen")]
173
impl core::fmt::Display for Pattern<'_> {
174
    fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
10✔
175
        let reference = crate::pattern::reference::Pattern::from(self);
10✔
176
        reference.fmt(formatter)
10✔
177
    }
10✔
178
}
179

180
#[cfg(feature = "datagen")]
181
impl databake::Bake for PatternMetadata {
182
    fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream {
1✔
183
        ctx.insert("icu_datetime");
1✔
184
        let time_granularity = databake::Bake::bake(&self.time_granularity(), ctx);
1✔
185
        databake::quote! {
1✔
186
            icu_datetime::pattern::runtime::PatternMetadata::from_time_granularity(#time_granularity)
187
        }
188
    }
1✔
189
}
190

191
#[test]
192
#[cfg(feature = "datagen")]
193
fn databake() {
2✔
194
    databake::test_bake!(
1✔
195
        PatternMetadata,
196
        const: crate::pattern::runtime::PatternMetadata::from_time_granularity(
1✔
197
            crate::pattern::TimeGranularity::Hours
1✔
198
        ),
199
        icu_datetime,
200
    );
201
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc