From 381f85c61ac088a5e4066316521e6368ef86655d Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 3 Sep 2024 14:29:07 -0700 Subject: [PATCH] Make some options bags have optional fields --- Cargo.lock | 8 +- Cargo.toml | 8 +- ffi/capi/src/bidi.rs | 10 +- ffi/capi/src/casemap.rs | 17 +- ffi/capi/src/collator.rs | 270 +++++---------------------- ffi/capi/src/decimal.rs | 33 +--- ffi/capi/src/displaynames.rs | 33 ++-- ffi/capi/src/segmenter_line.rs | 42 ++--- tutorials/c-tiny/fixeddecimal/test.c | 4 +- tutorials/c/Cargo.lock | 9 +- tutorials/c/fixeddecimal.c | 4 +- 11 files changed, 115 insertions(+), 323 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ceee47df608..e33bf60e399 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -613,7 +613,7 @@ dependencies = [ [[package]] name = "diplomat" version = "0.8.0" -source = "git+https://github.com/rust-diplomat/diplomat?rev=6807b4a92f3f8a6dd565d044b2e2fac181fc997f#6807b4a92f3f8a6dd565d044b2e2fac181fc997f" +source = "git+https://github.com/rust-diplomat/diplomat?rev=fe5201dd04ee6614942f817b291e90cb01440c19#fe5201dd04ee6614942f817b291e90cb01440c19" dependencies = [ "diplomat_core", "proc-macro2", @@ -643,7 +643,7 @@ dependencies = [ [[package]] name = "diplomat-runtime" version = "0.8.1" -source = "git+https://github.com/rust-diplomat/diplomat?rev=6807b4a92f3f8a6dd565d044b2e2fac181fc997f#6807b4a92f3f8a6dd565d044b2e2fac181fc997f" +source = "git+https://github.com/rust-diplomat/diplomat?rev=fe5201dd04ee6614942f817b291e90cb01440c19#fe5201dd04ee6614942f817b291e90cb01440c19" dependencies = [ "log", ] @@ -651,7 +651,7 @@ dependencies = [ [[package]] name = "diplomat-tool" version = "0.8.0" -source = "git+https://github.com/rust-diplomat/diplomat?rev=6807b4a92f3f8a6dd565d044b2e2fac181fc997f#6807b4a92f3f8a6dd565d044b2e2fac181fc997f" +source = "git+https://github.com/rust-diplomat/diplomat?rev=fe5201dd04ee6614942f817b291e90cb01440c19#fe5201dd04ee6614942f817b291e90cb01440c19" dependencies = [ "askama", "clap", @@ -671,7 +671,7 @@ dependencies = [ [[package]] name = "diplomat_core" version = "0.8.0" -source = "git+https://github.com/rust-diplomat/diplomat?rev=6807b4a92f3f8a6dd565d044b2e2fac181fc997f#6807b4a92f3f8a6dd565d044b2e2fac181fc997f" +source = "git+https://github.com/rust-diplomat/diplomat?rev=fe5201dd04ee6614942f817b291e90cb01440c19#fe5201dd04ee6614942f817b291e90cb01440c19" dependencies = [ "displaydoc", "either", diff --git a/Cargo.toml b/Cargo.toml index 3f8e61828dd..4665a720023 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -209,10 +209,10 @@ icu_benchmark_macros = { path = "tools/benchmark/macros" } # The version here can either be a `version = ".."` spec or `git = "https://github.com/rust-diplomat/diplomat", rev = ".."` # Diplomat must be published preceding a new ICU4X release but may use git versions in between -diplomat = { git = "https://github.com/rust-diplomat/diplomat", rev = "6807b4a92f3f8a6dd565d044b2e2fac181fc997f" } -diplomat-runtime = { git = "https://github.com/rust-diplomat/diplomat", rev = "6807b4a92f3f8a6dd565d044b2e2fac181fc997f" } -diplomat_core = { git = "https://github.com/rust-diplomat/diplomat", rev = "6807b4a92f3f8a6dd565d044b2e2fac181fc997f" } -diplomat-tool = { git = "https://github.com/rust-diplomat/diplomat", rev = "6807b4a92f3f8a6dd565d044b2e2fac181fc997f" } +diplomat = { git = "https://github.com/rust-diplomat/diplomat", rev = "fe5201dd04ee6614942f817b291e90cb01440c19" } +diplomat-runtime = { git = "https://github.com/rust-diplomat/diplomat", rev = "fe5201dd04ee6614942f817b291e90cb01440c19" } +diplomat_core = { git = "https://github.com/rust-diplomat/diplomat", rev = "fe5201dd04ee6614942f817b291e90cb01440c19" } +diplomat-tool = { git = "https://github.com/rust-diplomat/diplomat", rev = "fe5201dd04ee6614942f817b291e90cb01440c19" } # EXTERNAL DEPENDENCIES # diff --git a/ffi/capi/src/bidi.rs b/ffi/capi/src/bidi.rs index ba8d6b61c46..f10afe36cdd 100644 --- a/ffi/capi/src/bidi.rs +++ b/ffi/capi/src/bidi.rs @@ -39,7 +39,7 @@ pub mod ffi { /// Use the data loaded in this object to process a string and calculate bidi information /// - /// Takes in a Level for the default level, if it is an invalid value it will default to LTR + /// Takes in a Level for the default level, if it is an invalid value or None it will default to Auto. /// /// Returns nothing if `text` is invalid UTF-8. #[diplomat::rust_link(unicode_bidi::BidiInfo::new_with_data_source, FnInStruct)] @@ -53,7 +53,7 @@ pub mod ffi { pub fn for_text_utf8<'text>( &self, text: &'text DiplomatStr, - default_level: u8, + default_level: Option, ) -> Option>> { let text = core::str::from_utf8(text).ok()?; @@ -64,7 +64,7 @@ pub mod ffi { unicode_bidi::BidiInfo::new_with_data_source( &adapter, text, - unicode_bidi::Level::new(default_level).ok(), + default_level.and_then(|l| unicode_bidi::Level::new(l).ok()), ), ))) } @@ -84,7 +84,7 @@ pub mod ffi { pub fn for_text_valid_utf8<'text>( &self, text: &'text str, - default_level: u8, + default_level: Option, ) -> Box> { let data = self.0.as_borrowed(); let adapter = icu_properties::bidi::BidiClassAdapter::new(data); @@ -92,7 +92,7 @@ pub mod ffi { Box::new(BidiInfo(unicode_bidi::BidiInfo::new_with_data_source( &adapter, text, - unicode_bidi::Level::new(default_level).ok(), + default_level.and_then(|l| unicode_bidi::Level::new(l).ok()), ))) } diff --git a/ffi/capi/src/casemap.rs b/ffi/capi/src/casemap.rs index 729bb0e7a6e..04108107344 100644 --- a/ffi/capi/src/casemap.rs +++ b/ffi/capi/src/casemap.rs @@ -11,6 +11,7 @@ pub mod ffi { use alloc::boxed::Box; use crate::{errors::ffi::DataError, locale_core::ffi::Locale, provider::ffi::DataProvider}; + use diplomat_runtime::DiplomatOption; use writeable::Writeable; @@ -32,8 +33,8 @@ pub mod ffi { #[diplomat::rust_link(icu::casemap::titlecase::TitlecaseOptions, Struct)] #[diplomat::attr(supports = non_exhaustive_structs, rename = "TitlecaseOptions")] pub struct TitlecaseOptionsV1 { - pub leading_adjustment: LeadingAdjustment, - pub trailing_case: TrailingCase, + pub leading_adjustment: DiplomatOption, + pub trailing_case: DiplomatOption, } impl TitlecaseOptionsV1 { @@ -42,8 +43,8 @@ pub mod ffi { #[diplomat::attr(any(cpp, js), rename = "default_options")] pub fn default() -> TitlecaseOptionsV1 { Self { - leading_adjustment: LeadingAdjustment::Auto, - trailing_case: TrailingCase::Lower, + leading_adjustment: None.into(), + trailing_case: None.into(), } } } @@ -300,8 +301,12 @@ impl From for TitlecaseOptions { fn from(other: ffi::TitlecaseOptionsV1) -> Self { let mut ret = Self::default(); - ret.leading_adjustment = other.leading_adjustment.into(); - ret.trailing_case = other.trailing_case.into(); + if let Some(l) = other.leading_adjustment.into_converted_option() { + ret.leading_adjustment = l; + } + if let Some(t) = other.trailing_case.into_converted_option() { + ret.trailing_case = t; + } ret } } diff --git a/ffi/capi/src/collator.rs b/ffi/capi/src/collator.rs index 2459efee543..095cd73eb9b 100644 --- a/ffi/capi/src/collator.rs +++ b/ffi/capi/src/collator.rs @@ -9,6 +9,7 @@ pub mod ffi { use alloc::boxed::Box; use crate::{errors::ffi::DataError, locale_core::ffi::Locale, provider::ffi::DataProvider}; + use diplomat_runtime::DiplomatOption; #[diplomat::opaque] #[diplomat::rust_link(icu::collator::Collator, Struct)] @@ -18,13 +19,13 @@ pub mod ffi { #[diplomat::rust_link(icu::collator::CollatorOptions::new, FnInStruct, hidden)] #[diplomat::attr(supports = non_exhaustive_structs, rename = "CollatorOptions")] pub struct CollatorOptionsV1 { - pub strength: CollatorStrength, - pub alternate_handling: CollatorAlternateHandling, - pub case_first: CollatorCaseFirst, - pub max_variable: CollatorMaxVariable, - pub case_level: CollatorCaseLevel, - pub numeric: CollatorNumeric, - pub backward_second_level: CollatorBackwardSecondLevel, + pub strength: DiplomatOption, + pub alternate_handling: DiplomatOption, + pub case_first: DiplomatOption, + pub max_variable: DiplomatOption, + pub case_level: DiplomatOption, + pub numeric: DiplomatOption, + pub backward_second_level: DiplomatOption, } // Note the flipped order of the words `Collator` and `Resolved`, because @@ -45,64 +46,64 @@ pub mod ffi { #[diplomat::rust_link(icu::collator::Strength, Enum)] #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)] + #[diplomat::enum_convert(icu_collator::Strength, needs_wildcard)] pub enum CollatorStrength { - Auto = 0, - Primary = 1, - Secondary = 2, - Tertiary = 3, - Quaternary = 4, - Identical = 5, + Primary = 0, + Secondary = 1, + Tertiary = 2, + Quaternary = 3, + Identical = 4, } #[diplomat::rust_link(icu::collator::AlternateHandling, Enum)] #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)] + #[diplomat::enum_convert(icu_collator::AlternateHandling, needs_wildcard)] pub enum CollatorAlternateHandling { - Auto = 0, - NonIgnorable = 1, - Shifted = 2, + NonIgnorable = 0, + Shifted = 1, } #[diplomat::rust_link(icu::collator::CaseFirst, Enum)] #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)] + #[diplomat::enum_convert(icu_collator::CaseFirst, needs_wildcard)] pub enum CollatorCaseFirst { - Auto = 0, - Off = 1, - LowerFirst = 2, - UpperFirst = 3, + Off = 0, + LowerFirst = 1, + UpperFirst = 2, } #[diplomat::rust_link(icu::collator::MaxVariable, Enum)] #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)] + #[diplomat::enum_convert(icu_collator::MaxVariable, needs_wildcard)] pub enum CollatorMaxVariable { - Auto = 0, - Space = 1, - Punctuation = 2, - Symbol = 3, - Currency = 4, + Space = 0, + Punctuation = 1, + Symbol = 2, + Currency = 3, } #[diplomat::rust_link(icu::collator::CaseLevel, Enum)] #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)] + #[diplomat::enum_convert(icu_collator::CaseLevel, needs_wildcard)] pub enum CollatorCaseLevel { - Auto = 0, - Off = 1, - On = 2, + Off = 0, + On = 1, } #[diplomat::rust_link(icu::collator::Numeric, Enum)] #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)] + #[diplomat::enum_convert(icu_collator::Numeric, needs_wildcard)] pub enum CollatorNumeric { - Auto = 0, - Off = 1, - On = 2, + Off = 0, + On = 1, } #[diplomat::rust_link(icu::collator::BackwardSecondLevel, Enum)] #[derive(Eq, PartialEq, Debug, PartialOrd, Ord)] + #[diplomat::enum_convert(icu_collator::BackwardSecondLevel, needs_wildcard)] pub enum CollatorBackwardSecondLevel { - Auto = 0, - Off = 1, - On = 2, + Off = 0, + On = 1, } impl Collator { @@ -167,203 +168,16 @@ pub mod ffi { } } -impl From for Option { - fn from(strength: ffi::CollatorStrength) -> Option { - match strength { - ffi::CollatorStrength::Auto => None, - ffi::CollatorStrength::Primary => Some(icu_collator::Strength::Primary), - ffi::CollatorStrength::Secondary => Some(icu_collator::Strength::Secondary), - ffi::CollatorStrength::Tertiary => Some(icu_collator::Strength::Tertiary), - ffi::CollatorStrength::Quaternary => Some(icu_collator::Strength::Quaternary), - ffi::CollatorStrength::Identical => Some(icu_collator::Strength::Identical), - } - } -} - -impl From for Option { - fn from( - alternate_handling: ffi::CollatorAlternateHandling, - ) -> Option { - match alternate_handling { - ffi::CollatorAlternateHandling::Auto => None, - ffi::CollatorAlternateHandling::NonIgnorable => { - Some(icu_collator::AlternateHandling::NonIgnorable) - } - ffi::CollatorAlternateHandling::Shifted => { - Some(icu_collator::AlternateHandling::Shifted) - } - } - } -} - -impl From for Option { - fn from(case_first: ffi::CollatorCaseFirst) -> Option { - match case_first { - ffi::CollatorCaseFirst::Auto => None, - ffi::CollatorCaseFirst::Off => Some(icu_collator::CaseFirst::Off), - ffi::CollatorCaseFirst::LowerFirst => Some(icu_collator::CaseFirst::LowerFirst), - ffi::CollatorCaseFirst::UpperFirst => Some(icu_collator::CaseFirst::UpperFirst), - } - } -} - -impl From for Option { - fn from(max_variable: ffi::CollatorMaxVariable) -> Option { - match max_variable { - ffi::CollatorMaxVariable::Auto => None, - ffi::CollatorMaxVariable::Space => Some(icu_collator::MaxVariable::Space), - ffi::CollatorMaxVariable::Punctuation => Some(icu_collator::MaxVariable::Punctuation), - ffi::CollatorMaxVariable::Symbol => Some(icu_collator::MaxVariable::Symbol), - ffi::CollatorMaxVariable::Currency => Some(icu_collator::MaxVariable::Currency), - } - } -} - -impl From for Option { - fn from(case_level: ffi::CollatorCaseLevel) -> Option { - match case_level { - ffi::CollatorCaseLevel::Auto => None, - ffi::CollatorCaseLevel::Off => Some(icu_collator::CaseLevel::Off), - ffi::CollatorCaseLevel::On => Some(icu_collator::CaseLevel::On), - } - } -} - -impl From for Option { - fn from(numeric: ffi::CollatorNumeric) -> Option { - match numeric { - ffi::CollatorNumeric::Auto => None, - ffi::CollatorNumeric::Off => Some(icu_collator::Numeric::Off), - ffi::CollatorNumeric::On => Some(icu_collator::Numeric::On), - } - } -} - -impl From for Option { - fn from( - backward_second_level: ffi::CollatorBackwardSecondLevel, - ) -> Option { - match backward_second_level { - ffi::CollatorBackwardSecondLevel::Auto => None, - ffi::CollatorBackwardSecondLevel::Off => Some(icu_collator::BackwardSecondLevel::Off), - ffi::CollatorBackwardSecondLevel::On => Some(icu_collator::BackwardSecondLevel::On), - } - } -} - -impl From for ffi::CollatorStrength { - fn from(strength: icu_collator::Strength) -> ffi::CollatorStrength { - match strength { - icu_collator::Strength::Primary => ffi::CollatorStrength::Primary, - icu_collator::Strength::Secondary => ffi::CollatorStrength::Secondary, - icu_collator::Strength::Tertiary => ffi::CollatorStrength::Tertiary, - icu_collator::Strength::Quaternary => ffi::CollatorStrength::Quaternary, - icu_collator::Strength::Identical => ffi::CollatorStrength::Identical, - _ => { - debug_assert!(false, "FFI out of sync"); - ffi::CollatorStrength::Identical // Highest we know of - } - } - } -} - -impl From for ffi::CollatorAlternateHandling { - fn from(alternate_handling: icu_collator::AlternateHandling) -> ffi::CollatorAlternateHandling { - match alternate_handling { - icu_collator::AlternateHandling::NonIgnorable => { - ffi::CollatorAlternateHandling::NonIgnorable - } - icu_collator::AlternateHandling::Shifted => ffi::CollatorAlternateHandling::Shifted, - _ => { - debug_assert!(false, "FFI out of sync"); - // Possible future values: ShiftTrimmed, Blanked - ffi::CollatorAlternateHandling::Shifted // Highest we know of - } - } - } -} - -impl From for ffi::CollatorCaseFirst { - fn from(case_first: icu_collator::CaseFirst) -> ffi::CollatorCaseFirst { - match case_first { - icu_collator::CaseFirst::Off => ffi::CollatorCaseFirst::Off, - icu_collator::CaseFirst::LowerFirst => ffi::CollatorCaseFirst::LowerFirst, - icu_collator::CaseFirst::UpperFirst => ffi::CollatorCaseFirst::UpperFirst, - _ => { - debug_assert!(false, "FFI out of sync"); - // Does it even make sense that `CaseFirst` is non-exhaustive? - ffi::CollatorCaseFirst::Off // The most neutral value - } - } - } -} - -impl From for ffi::CollatorMaxVariable { - fn from(max_variable: icu_collator::MaxVariable) -> ffi::CollatorMaxVariable { - match max_variable { - icu_collator::MaxVariable::Space => ffi::CollatorMaxVariable::Space, - icu_collator::MaxVariable::Punctuation => ffi::CollatorMaxVariable::Punctuation, - icu_collator::MaxVariable::Symbol => ffi::CollatorMaxVariable::Symbol, - icu_collator::MaxVariable::Currency => ffi::CollatorMaxVariable::Currency, - _ => { - debug_assert!(false, "FFI out of sync"); - ffi::CollatorMaxVariable::Currency // Highest we know of - } - } - } -} - -impl From for ffi::CollatorCaseLevel { - fn from(case_level: icu_collator::CaseLevel) -> ffi::CollatorCaseLevel { - match case_level { - icu_collator::CaseLevel::Off => ffi::CollatorCaseLevel::Off, - icu_collator::CaseLevel::On => ffi::CollatorCaseLevel::On, - _ => { - debug_assert!(false, "FFI out of sync"); - ffi::CollatorCaseLevel::On // The most enabled that we know of - } - } - } -} - -impl From for ffi::CollatorNumeric { - fn from(numeric: icu_collator::Numeric) -> ffi::CollatorNumeric { - match numeric { - icu_collator::Numeric::Off => ffi::CollatorNumeric::Off, - icu_collator::Numeric::On => ffi::CollatorNumeric::On, - _ => { - debug_assert!(false, "FFI out of sync"); - ffi::CollatorNumeric::On // The most enabled that we know of - } - } - } -} - -impl From for ffi::CollatorBackwardSecondLevel { - fn from( - backward_second_level: icu_collator::BackwardSecondLevel, - ) -> ffi::CollatorBackwardSecondLevel { - match backward_second_level { - icu_collator::BackwardSecondLevel::Off => ffi::CollatorBackwardSecondLevel::Off, - icu_collator::BackwardSecondLevel::On => ffi::CollatorBackwardSecondLevel::On, - _ => { - debug_assert!(false, "FFI out of sync"); - ffi::CollatorBackwardSecondLevel::On // The most enabled that we know of - } - } - } -} - impl From for icu_collator::CollatorOptions { fn from(options: ffi::CollatorOptionsV1) -> icu_collator::CollatorOptions { let mut result = icu_collator::CollatorOptions::new(); - result.strength = options.strength.into(); - result.alternate_handling = options.alternate_handling.into(); - result.case_first = options.case_first.into(); - result.max_variable = options.max_variable.into(); - result.case_level = options.case_level.into(); - result.numeric = options.numeric.into(); - result.backward_second_level = options.backward_second_level.into(); + result.strength = options.strength.into_converted_option(); + result.alternate_handling = options.alternate_handling.into_converted_option(); + result.case_first = options.case_first.into_converted_option(); + result.max_variable = options.max_variable.into_converted_option(); + result.case_level = options.case_level.into_converted_option(); + result.numeric = options.numeric.into_converted_option(); + result.backward_second_level = options.backward_second_level.into_converted_option(); result } diff --git a/ffi/capi/src/decimal.rs b/ffi/capi/src/decimal.rs index d30034fdac9..9c2b21311f7 100644 --- a/ffi/capi/src/decimal.rs +++ b/ffi/capi/src/decimal.rs @@ -22,6 +22,7 @@ pub mod ffi { pub struct FixedDecimalFormatter(pub icu_decimal::FixedDecimalFormatter); #[diplomat::rust_link(icu::decimal::options::GroupingStrategy, Enum)] + #[diplomat::enum_convert(icu_decimal::options::GroupingStrategy, needs_wildcard)] pub enum FixedDecimalGroupingStrategy { Auto, Never, @@ -37,22 +38,14 @@ pub mod ffi { pub fn create_with_grouping_strategy( provider: &DataProvider, locale: &Locale, - grouping_strategy: FixedDecimalGroupingStrategy, + grouping_strategy: Option, ) -> Result, DataError> { let locale = locale.to_datalocale(); - let grouping_strategy = match grouping_strategy { - FixedDecimalGroupingStrategy::Auto => icu_decimal::options::GroupingStrategy::Auto, - FixedDecimalGroupingStrategy::Never => { - icu_decimal::options::GroupingStrategy::Never - } - FixedDecimalGroupingStrategy::Always => { - icu_decimal::options::GroupingStrategy::Always - } - FixedDecimalGroupingStrategy::Min2 => icu_decimal::options::GroupingStrategy::Min2, - }; let mut options = icu_decimal::options::FixedDecimalFormatterOptions::default(); - options.grouping_strategy = grouping_strategy; + options.grouping_strategy = grouping_strategy + .map(Into::into) + .unwrap_or(options.grouping_strategy); Ok(Box::new(FixedDecimalFormatter(call_constructor!( icu_decimal::FixedDecimalFormatter::try_new, icu_decimal::FixedDecimalFormatter::try_new_with_any_provider, @@ -77,7 +70,7 @@ pub mod ffi { secondary_group_size: u8, min_group_size: u8, digits: &[DiplomatChar], - grouping_strategy: FixedDecimalGroupingStrategy, + grouping_strategy: Option, ) -> Result, DataError> { use alloc::borrow::Cow; fn str_to_cow(s: &diplomat_runtime::DiplomatStr) -> Cow<'static, str> { @@ -115,18 +108,10 @@ pub mod ffi { min_grouping: min_group_size, }; - let grouping_strategy = match grouping_strategy { - FixedDecimalGroupingStrategy::Auto => icu_decimal::options::GroupingStrategy::Auto, - FixedDecimalGroupingStrategy::Never => { - icu_decimal::options::GroupingStrategy::Never - } - FixedDecimalGroupingStrategy::Always => { - icu_decimal::options::GroupingStrategy::Always - } - FixedDecimalGroupingStrategy::Min2 => icu_decimal::options::GroupingStrategy::Min2, - }; let mut options = icu_decimal::options::FixedDecimalFormatterOptions::default(); - options.grouping_strategy = grouping_strategy; + options.grouping_strategy = grouping_strategy + .map(Into::into) + .unwrap_or(options.grouping_strategy); Ok(Box::new(FixedDecimalFormatter( icu_decimal::FixedDecimalFormatter::try_new_with_any_provider( &icu_provider_adapters::any_payload::AnyPayloadProvider::from_any_payload::< diff --git a/ffi/capi/src/displaynames.rs b/ffi/capi/src/displaynames.rs index 10b2ce32c16..3234f87b8ce 100644 --- a/ffi/capi/src/displaynames.rs +++ b/ffi/capi/src/displaynames.rs @@ -11,6 +11,7 @@ pub mod ffi { use crate::errors::ffi::{DataError, LocaleParseError}; use crate::locale_core::ffi::Locale; use crate::provider::ffi::DataProvider; + use diplomat_runtime::DiplomatOption; use writeable::Writeable; @@ -28,17 +29,17 @@ pub mod ffi { #[diplomat::attr(supports = non_exhaustive_structs, rename = "DisplayNamesOptions")] pub struct DisplayNamesOptionsV1 { /// The optional formatting style to use for display name. - pub style: DisplayNamesStyle, + pub style: DiplomatOption, /// The fallback return when the system does not have the /// requested display name, defaults to "code". - pub fallback: DisplayNamesFallback, + pub fallback: DiplomatOption, /// The language display kind, defaults to "dialect". - pub language_display: LanguageDisplay, + pub language_display: DiplomatOption, } #[diplomat::rust_link(icu::displaynames::options::Style, Enum)] + #[diplomat::enum_convert(icu_experimental::displaynames::Style, needs_wildcard)] pub enum DisplayNamesStyle { - Auto, Narrow, Short, Long, @@ -133,26 +134,20 @@ pub mod ffi { } } -impl From for Option { - fn from(style: ffi::DisplayNamesStyle) -> Option { - match style { - ffi::DisplayNamesStyle::Auto => None, - ffi::DisplayNamesStyle::Narrow => Some(icu_experimental::displaynames::Style::Narrow), - ffi::DisplayNamesStyle::Short => Some(icu_experimental::displaynames::Style::Short), - ffi::DisplayNamesStyle::Long => Some(icu_experimental::displaynames::Style::Long), - ffi::DisplayNamesStyle::Menu => Some(icu_experimental::displaynames::Style::Menu), - } - } -} - impl From for icu_experimental::displaynames::DisplayNamesOptions { fn from( other: ffi::DisplayNamesOptionsV1, ) -> icu_experimental::displaynames::DisplayNamesOptions { let mut options = icu_experimental::displaynames::DisplayNamesOptions::default(); - options.style = other.style.into(); - options.fallback = other.fallback.into(); - options.language_display = other.language_display.into(); + options.style = other.style.into_converted_option(); + options.fallback = other + .fallback + .into_converted_option() + .unwrap_or(options.fallback); + options.language_display = other + .language_display + .into_converted_option() + .unwrap_or(options.language_display); options } } diff --git a/ffi/capi/src/segmenter_line.rs b/ffi/capi/src/segmenter_line.rs index 22d858c764f..d4f0733de66 100644 --- a/ffi/capi/src/segmenter_line.rs +++ b/ffi/capi/src/segmenter_line.rs @@ -10,6 +10,7 @@ pub mod ffi { use crate::errors::ffi::DataError; use crate::provider::ffi::DataProvider; + use diplomat_runtime::DiplomatOption; #[diplomat::opaque] /// An ICU4X line-break segmenter, capable of finding breakpoints in strings. @@ -17,6 +18,7 @@ pub mod ffi { pub struct LineSegmenter(icu_segmenter::LineSegmenter); #[diplomat::rust_link(icu::segmenter::LineBreakStrictness, Enum)] + #[diplomat::enum_convert(icu_segmenter::LineBreakStrictness, needs_wildcard)] pub enum LineBreakStrictness { Loose, Normal, @@ -25,6 +27,7 @@ pub mod ffi { } #[diplomat::rust_link(icu::segmenter::LineBreakWordOption, Enum)] + #[diplomat::enum_convert(icu_segmenter::LineBreakWordOption, needs_wildcard)] pub enum LineBreakWordOption { Normal, BreakAll, @@ -34,9 +37,9 @@ pub mod ffi { #[diplomat::rust_link(icu::segmenter::LineBreakOptions, Struct)] #[diplomat::attr(supports = non_exhaustive_structs, rename = "LineBreakOptions")] pub struct LineBreakOptionsV1 { - pub strictness: LineBreakStrictness, - pub word_option: LineBreakWordOption, - pub ja_zh: bool, + pub strictness: DiplomatOption, + pub word_option: DiplomatOption, + pub ja_zh: DiplomatOption, } #[diplomat::opaque] @@ -247,33 +250,18 @@ pub mod ffi { } } -impl From for icu_segmenter::LineBreakStrictness { - fn from(other: ffi::LineBreakStrictness) -> Self { - match other { - ffi::LineBreakStrictness::Loose => Self::Loose, - ffi::LineBreakStrictness::Normal => Self::Normal, - ffi::LineBreakStrictness::Strict => Self::Strict, - ffi::LineBreakStrictness::Anywhere => Self::Anywhere, - } - } -} - -impl From for icu_segmenter::LineBreakWordOption { - fn from(other: ffi::LineBreakWordOption) -> Self { - match other { - ffi::LineBreakWordOption::Normal => Self::Normal, - ffi::LineBreakWordOption::BreakAll => Self::BreakAll, - ffi::LineBreakWordOption::KeepAll => Self::KeepAll, - } - } -} - impl From for icu_segmenter::LineBreakOptions { fn from(other: ffi::LineBreakOptionsV1) -> Self { let mut options = icu_segmenter::LineBreakOptions::default(); - options.strictness = other.strictness.into(); - options.word_option = other.word_option.into(); - options.ja_zh = other.ja_zh; + options.strictness = other + .strictness + .into_converted_option() + .unwrap_or(options.strictness); + options.word_option = other + .word_option + .into_converted_option() + .unwrap_or(options.word_option); + options.ja_zh = other.ja_zh.into_option().unwrap_or(options.ja_zh); options } } diff --git a/tutorials/c-tiny/fixeddecimal/test.c b/tutorials/c-tiny/fixeddecimal/test.c index e71d1b83e1a..670ef8616d3 100644 --- a/tutorials/c-tiny/fixeddecimal/test.c +++ b/tutorials/c-tiny/fixeddecimal/test.c @@ -29,8 +29,10 @@ int main(int argc, char *argv[]) { FixedDecimal* decimal = icu4x_FixedDecimal_from_uint64_mv1(1000007); icu4x_FixedDecimal_round_mv1(decimal, 0); + FixedDecimalGroupingStrategy_option o = {.ok = FixedDecimalGroupingStrategy_Auto, .is_ok = true}; + icu4x_FixedDecimalFormatter_create_with_grouping_strategy_mv1_result fdf_result = - icu4x_FixedDecimalFormatter_create_with_grouping_strategy_mv1(provider, locale, FixedDecimalGroupingStrategy_Auto); + icu4x_FixedDecimalFormatter_create_with_grouping_strategy_mv1(provider, locale, o); if (!fdf_result.is_ok) { printf("Failed to create FixedDecimalFormatter\n"); return 1; diff --git a/tutorials/c/Cargo.lock b/tutorials/c/Cargo.lock index 1e3c46fe866..1dbc0595c89 100644 --- a/tutorials/c/Cargo.lock +++ b/tutorials/c/Cargo.lock @@ -47,7 +47,7 @@ dependencies = [ [[package]] name = "diplomat" version = "0.8.0" -source = "git+https://github.com/rust-diplomat/diplomat?rev=8744ac97162341f347b63131969ad736e1047f6d#8744ac97162341f347b63131969ad736e1047f6d" +source = "git+https://github.com/rust-diplomat/diplomat?rev=fe5201dd04ee6614942f817b291e90cb01440c19#fe5201dd04ee6614942f817b291e90cb01440c19" dependencies = [ "diplomat_core", "proc-macro2", @@ -58,7 +58,7 @@ dependencies = [ [[package]] name = "diplomat-runtime" version = "0.8.1" -source = "git+https://github.com/rust-diplomat/diplomat?rev=8744ac97162341f347b63131969ad736e1047f6d#8744ac97162341f347b63131969ad736e1047f6d" +source = "git+https://github.com/rust-diplomat/diplomat?rev=fe5201dd04ee6614942f817b291e90cb01440c19#fe5201dd04ee6614942f817b291e90cb01440c19" dependencies = [ "log", ] @@ -66,7 +66,7 @@ dependencies = [ [[package]] name = "diplomat_core" version = "0.8.0" -source = "git+https://github.com/rust-diplomat/diplomat?rev=8744ac97162341f347b63131969ad736e1047f6d#8744ac97162341f347b63131969ad736e1047f6d" +source = "git+https://github.com/rust-diplomat/diplomat?rev=fe5201dd04ee6614942f817b291e90cb01440c19#fe5201dd04ee6614942f817b291e90cb01440c19" dependencies = [ "proc-macro2", "quote", @@ -267,6 +267,7 @@ name = "icu_experimental" version = "0.1.0" dependencies = [ "displaydoc", + "either", "fixed_decimal", "icu_collections", "icu_decimal", @@ -409,10 +410,10 @@ version = "1.5.0" dependencies = [ "displaydoc", "icu_collections", + "icu_locale_core", "icu_properties_data", "icu_provider", "potential_utf", - "tinystr", "unicode-bidi", "zerovec", ] diff --git a/tutorials/c/fixeddecimal.c b/tutorials/c/fixeddecimal.c index 742e99220b3..89a4d7a90d0 100644 --- a/tutorials/c/fixeddecimal.c +++ b/tutorials/c/fixeddecimal.c @@ -26,8 +26,10 @@ int main() { FixedDecimal* decimal = icu4x_FixedDecimal_from_uint64_mv1(1000007); + FixedDecimalGroupingStrategy_option o = {.ok = FixedDecimalGroupingStrategy_Auto, .is_ok = true}; + icu4x_FixedDecimalFormatter_create_with_grouping_strategy_mv1_result fdf_result = - icu4x_FixedDecimalFormatter_create_with_grouping_strategy_mv1(provider, locale, FixedDecimalGroupingStrategy_Auto); + icu4x_FixedDecimalFormatter_create_with_grouping_strategy_mv1(provider, locale, o); if (!fdf_result.is_ok) { printf("Failed to create FixedDecimalFormatter\n"); return 1;