@@ -10,7 +10,7 @@ use alloc::string::String;
1010use alloc:: vec;
1111use alloc:: vec:: Vec ;
1212use core:: char;
13- use icu_locale_core:: subtags:: language;
13+ use icu_locale_core:: subtags:: { language, Language } ;
1414use icu_locale_core:: LanguageIdentifier ;
1515use icu_provider:: prelude:: * ;
1616use utf8_iter:: Utf8CharIndices ;
@@ -208,24 +208,42 @@ pub struct LineBreakOptions<'a> {
208208 pub content_locale : Option < & ' a LanguageIdentifier > ,
209209}
210210
211+ impl LineBreakOptions < ' _ > {
212+ /// `const` version of [`Default::default`]
213+ pub const fn default ( ) -> Self {
214+ Self {
215+ strictness : None ,
216+ word_option : None ,
217+ content_locale : None ,
218+ }
219+ }
220+ }
221+
211222#[ derive( Debug , Clone , Copy ) ]
212223struct ResolvedLineBreakOptions {
213224 strictness : LineBreakStrictness ,
214225 word_option : LineBreakWordOption ,
215226 ja_zh : bool ,
216227}
217228
218- impl From < LineBreakOptions < ' _ > > for ResolvedLineBreakOptions {
219- fn from ( options : LineBreakOptions < ' _ > ) -> Self {
220- let ja_zh = if let Some ( content_locale) = options. content_locale . as_ref ( ) {
221- content_locale. language == language ! ( "ja" ) || content_locale. language == language ! ( "zh" )
222- } else {
223- false
224- } ;
225- Self {
226- strictness : options. strictness . unwrap_or_default ( ) ,
227- word_option : options. word_option . unwrap_or_default ( ) ,
228- ja_zh,
229+ impl LineBreakOptions < ' _ > {
230+ const fn resolve ( self ) -> ResolvedLineBreakOptions {
231+ ResolvedLineBreakOptions {
232+ strictness : match self . strictness {
233+ Some ( s) => s,
234+ None => LineBreakStrictness :: Strict ,
235+ } ,
236+ word_option : match self . word_option {
237+ Some ( s) => s,
238+ None => LineBreakWordOption :: Normal ,
239+ } ,
240+ ja_zh : if let Some ( content_locale) = self . content_locale . as_ref ( ) {
241+ const JA : Language = language ! ( "ja" ) ;
242+ const ZH : Language = language ! ( "zh" ) ;
243+ matches ! ( content_locale. language, JA | ZH )
244+ } else {
245+ false
246+ } ,
229247 }
230248 }
231249}
@@ -426,7 +444,7 @@ impl LineSegmenter {
426444 #[ cfg( feature = "compiled_data" ) ]
427445 pub fn new_lstm ( options : LineBreakOptions ) -> LineSegmenterBorrowed < ' static > {
428446 LineSegmenterBorrowed {
429- options : options. into ( ) ,
447+ options : options. resolve ( ) ,
430448 data : crate :: provider:: Baked :: SINGLETON_SEGMENTER_BREAK_LINE_V1 ,
431449 complex : ComplexPayloadsBorrowed :: new_lstm ( ) ,
432450 }
@@ -456,7 +474,7 @@ impl LineSegmenter {
456474 + ?Sized ,
457475 {
458476 Ok ( Self {
459- options : options. into ( ) ,
477+ options : options. resolve ( ) ,
460478 payload : provider. load ( Default :: default ( ) ) ?. payload ,
461479 complex : ComplexPayloads :: try_new_lstm ( provider) ?,
462480 } )
@@ -474,7 +492,7 @@ impl LineSegmenter {
474492 #[ cfg( feature = "compiled_data" ) ]
475493 pub fn new_dictionary ( options : LineBreakOptions ) -> LineSegmenterBorrowed < ' static > {
476494 LineSegmenterBorrowed {
477- options : options. into ( ) ,
495+ options : options. resolve ( ) ,
478496 data : crate :: provider:: Baked :: SINGLETON_SEGMENTER_BREAK_LINE_V1 ,
479497 // Line segmenter doesn't need to load CJ dictionary because UAX 14 rules handles CJK
480498 // characters [1]. Southeast Asian languages however require complex context analysis
@@ -508,7 +526,7 @@ impl LineSegmenter {
508526 + ?Sized ,
509527 {
510528 Ok ( Self {
511- options : options. into ( ) ,
529+ options : options. resolve ( ) ,
512530 payload : provider. load ( Default :: default ( ) ) ?. payload ,
513531 // Line segmenter doesn't need to load CJ dictionary because UAX 14 rules handles CJK
514532 // characters [1]. Southeast Asian languages however require complex context analysis
@@ -527,9 +545,9 @@ impl LineSegmenter {
527545 ///
528546 /// [📚 Help choosing a constructor](icu_provider::constructors)
529547 #[ cfg( feature = "compiled_data" ) ]
530- pub fn new_non_complex ( options : LineBreakOptions ) -> LineSegmenterBorrowed < ' static > {
548+ pub const fn new_non_complex ( options : LineBreakOptions ) -> LineSegmenterBorrowed < ' static > {
531549 LineSegmenterBorrowed {
532- options : options. into ( ) ,
550+ options : options. resolve ( ) ,
533551 data : crate :: provider:: Baked :: SINGLETON_SEGMENTER_BREAK_LINE_V1 ,
534552 complex : ComplexPayloadsBorrowed :: empty ( ) ,
535553 }
@@ -556,7 +574,7 @@ impl LineSegmenter {
556574 + ?Sized ,
557575 {
558576 Ok ( Self {
559- options : options. into ( ) ,
577+ options : options. resolve ( ) ,
560578 payload : provider. load ( Default :: default ( ) ) ?. payload ,
561579 complex : ComplexPayloads :: try_new_empty ( provider) ?,
562580 } )
0 commit comments