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

zbraniecki / icu4x / 9457158389

10 Jun 2024 11:45PM UTC coverage: 75.174% (+0.05%) from 75.121%
9457158389

push

github

web-flow
Add constructing TinyAsciiStr from utf16 (#5025)

Introduces TinyAsciiStr constructors from utf16 and converges on the
consensus from #4931.

---------

Co-authored-by: Robert Bastian <4706271+robertbastian@users.noreply.github.com>

65 of 82 new or added lines in 14 files covered. (79.27%)

3441 existing lines in 141 files now uncovered.

52850 of 70304 relevant lines covered (75.17%)

563298.06 hits per line

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

91.3
/components/datetime/src/neo_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
//! Temporary module for neo datetime patterns
6

7
use core::str::FromStr;
8

9
use crate::helpers::size_test;
10
use crate::pattern::{runtime, PatternError, PatternItem};
11

12
size_test!(DateTimePattern, date_time_pattern_size, 32);
13

14
/// A pattern for formatting a datetime in a calendar.
15
///
16
/// Most clients should use [`DateTimeFormatter`] instead of directly
17
/// formatting with patterns.
18
///
19
/// There are two ways to make one of these:
20
///
21
/// 1. From a custom pattern string: [`DateTimePattern::try_from_pattern_str`]
22
/// 2. From a formatted datetime: [`FormattedNeoDateTime::pattern`]
23
#[doc = date_time_pattern_size!()]
24
///
25
/// # Examples
26
///
27
/// Create a pattern from a custom string and compare it to one from data:
28
///
29
/// ```
30
/// use icu::calendar::DateTime;
31
/// use icu::calendar::Gregorian;
32
/// use icu::datetime::neo::TypedNeoFormatter;
33
/// use icu::datetime::neo_marker::NeoYearMonthDayMarker;
34
/// use icu::datetime::neo_pattern::DateTimePattern;
35
/// use icu::datetime::neo_skeleton::NeoSkeletonLength;
36
/// use icu::locale::locale;
37
///
38
/// let custom_pattern =
39
///     DateTimePattern::try_from_pattern_str("d MMM y").unwrap();
40
///
41
/// let data_pattern =
42
///     TypedNeoFormatter::<Gregorian, NeoYearMonthDayMarker>::try_new(
43
///         &locale!("es-MX").into(),
44
///         NeoSkeletonLength::Medium,
45
///     )
46
///     .unwrap()
47
///     // The pattern can depend on the datetime being formatted.
48
///     // For this example, we'll choose the local Unix epoch.
49
///     .format(&DateTime::local_unix_epoch().to_calendar(Gregorian))
50
///     .pattern();
51
///
52
/// assert_eq!(custom_pattern, data_pattern);
53
/// ```
54
///
55
/// [`DateTimeFormatter`]: crate::DateTimeFormatter
56
/// [`FormattedNeoDateTime::pattern`]: crate::neo::FormattedNeoDateTime::pattern
UNCOV
57
#[derive(Debug)]
×
58
pub struct DateTimePattern {
UNCOV
59
    pattern: runtime::Pattern<'static>,
×
60
}
61

62
impl DateTimePattern {
63
    /// Creates a [`DateTimePattern`] from a pattern string.
64
    ///
65
    /// For more details on the syntax, see UTS 35:
66
    /// <https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns>
67
    pub fn try_from_pattern_str(pattern_str: &str) -> Result<Self, PatternError> {
45✔
68
        let pattern = runtime::Pattern::from_str(pattern_str)?;
45✔
69
        Ok(Self { pattern })
45✔
70
    }
45✔
71

72
    #[doc(hidden)] // TODO(#4467): Internal API
73
    pub fn from_runtime_pattern(pattern: runtime::Pattern<'static>) -> Self {
1✔
74
        Self { pattern }
1✔
75
    }
1✔
76

77
    pub(crate) fn iter_items(&self) -> impl Iterator<Item = PatternItem> + '_ {
1✔
78
        self.pattern.items.iter()
1✔
79
    }
1✔
80

81
    pub(crate) fn as_borrowed(&self) -> DateTimePatternBorrowed {
50✔
82
        DateTimePatternBorrowed(&self.pattern)
50✔
83
    }
50✔
84
}
85

86
impl FromStr for DateTimePattern {
87
    type Err = PatternError;
88
    fn from_str(s: &str) -> Result<Self, Self::Err> {
44✔
89
        Self::try_from_pattern_str(s)
44✔
90
    }
44✔
91
}
92

93
// Check equality on the borrowed variant since it flattens the difference
94
// between the three `Single` pattern variants, which is not public-facing
95
impl PartialEq for DateTimePattern {
96
    fn eq(&self, other: &Self) -> bool {
1✔
97
        self.as_borrowed().eq(&other.as_borrowed())
1✔
98
    }
1✔
99
}
100

101
impl Eq for DateTimePattern {}
102

103
// Not clear if this needs to be public. For now, make it private.
104
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2✔
105
pub(crate) struct DateTimePatternBorrowed<'a>(pub(crate) &'a runtime::Pattern<'a>);
1✔
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