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

zbraniecki / icu4x / 9281145371

29 May 2024 06:11AM UTC coverage: 76.254% (+0.1%) from 76.113%
9281145371

push

github

web-flow
Move generic Subtag to subtags (#4932)

First patch from the #1833 patchset.

It moves the `Subtag` to `locid::subtags::Subtag` - this is a generic
subtag (2..=8, separate from private::Subtag which is 1..=8) which will
be used to iterate over multi-part subtags like
`extensions::unicode::Value` to retrieve specific items without relying
on the underlying `TinyStr` storage.

It's a breaking change because we are removing
`extensions::other::Subtag`. I assume this is ok as we're working on 2.0
so I don't need to add alias and deprecate it, but please confirm.

16 of 16 new or added lines in 1 file covered. (100.0%)

1507 existing lines in 83 files now uncovered.

53859 of 70631 relevant lines covered (76.25%)

578959.07 hits per line

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

0.0
/ffi/capi/src/data_struct.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
#![cfg(feature = "icu_decimal")]
6

7
use alloc::borrow::Cow;
8

UNCOV
9
#[diplomat::bridge]
×
10
pub mod ffi {
11

12
    #[cfg(feature = "icu_decimal")]
13
    use crate::errors::ffi::ICU4XError;
14
    use alloc::boxed::Box;
15
    use icu_provider::AnyPayload;
16
    #[cfg(feature = "icu_decimal")]
17
    use icu_provider::DataPayload;
18

19
    #[diplomat::opaque]
20
    /// A generic data struct to be used by ICU4X
21
    ///
22
    /// This can be used to construct a StructDataProvider.
23
    #[diplomat::attr(*, disable)]
24
    pub struct ICU4XDataStruct(pub(crate) AnyPayload);
25

26
    impl ICU4XDataStruct {
27
        /// Construct a new DecimalSymbolsV1 data struct.
28
        ///
29
        /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
30
        /// to the WHATWG Encoding Standard.
31
        #[diplomat::rust_link(icu::decimal::provider::DecimalSymbolsV1, Struct)]
32
        #[allow(clippy::too_many_arguments)]
33
        pub fn create_decimal_symbols_v1(
×
34
            plus_sign_prefix: &DiplomatStr,
35
            plus_sign_suffix: &DiplomatStr,
36
            minus_sign_prefix: &DiplomatStr,
37
            minus_sign_suffix: &DiplomatStr,
38
            decimal_separator: &DiplomatStr,
39
            grouping_separator: &DiplomatStr,
40
            primary_group_size: u8,
41
            secondary_group_size: u8,
42
            min_group_size: u8,
43
            digits: &[DiplomatChar],
44
        ) -> Result<Box<ICU4XDataStruct>, ICU4XError> {
45
            use super::str_to_cow;
46
            use icu_decimal::provider::{
47
                AffixesV1, DecimalSymbolsV1, DecimalSymbolsV1Marker, GroupingSizesV1,
48
            };
49
            let digits = if digits.len() == 10 {
×
50
                let mut new_digits = ['\0'; 10];
×
51
                for (old, new) in digits.iter().zip(new_digits.iter_mut()) {
×
52
                    *new = char::from_u32(*old).ok_or(ICU4XError::DataStructValidityError)?;
×
53
                }
54
                new_digits
×
55
            } else {
56
                return Err(ICU4XError::DataStructValidityError);
×
57
            };
58
            let plus_sign_affixes = AffixesV1 {
×
59
                prefix: str_to_cow(plus_sign_prefix),
×
60
                suffix: str_to_cow(plus_sign_suffix),
×
61
            };
×
62
            let minus_sign_affixes = AffixesV1 {
×
63
                prefix: str_to_cow(minus_sign_prefix),
×
64
                suffix: str_to_cow(minus_sign_suffix),
×
65
            };
×
66
            let grouping_sizes = GroupingSizesV1 {
×
67
                primary: primary_group_size,
68
                secondary: secondary_group_size,
69
                min_grouping: min_group_size,
70
            };
71

72
            let symbols = DecimalSymbolsV1 {
×
73
                plus_sign_affixes,
×
74
                minus_sign_affixes,
×
75
                decimal_separator: str_to_cow(decimal_separator),
×
76
                grouping_separator: str_to_cow(grouping_separator),
×
77
                grouping_sizes,
×
78
                digits,
×
79
            };
×
80

81
            let payload: DataPayload<DecimalSymbolsV1Marker> = DataPayload::from_owned(symbols);
×
82
            Ok(Box::new(ICU4XDataStruct(payload.wrap_into_any_payload())))
×
83
        }
×
84
    }
85
}
86

87
fn str_to_cow(s: &diplomat_runtime::DiplomatStr) -> Cow<'static, str> {
×
88
    if s.is_empty() {
×
89
        Cow::default()
×
90
    } else {
91
        Cow::Owned(alloc::string::String::from_utf8_lossy(s).into_owned())
×
92
    }
93
}
×
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

© 2026 Coveralls, Inc