Skip to main content

icu_capi/
segmenter_sentence.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#[diplomat::bridge]
6#[diplomat::abi_rename = "icu4x_{0}_mv1"]
7pub mod ffi {
8    use alloc::boxed::Box;
9    use icu_segmenter::scaffold::{Latin1, PotentiallyIllFormedUtf8, Utf16};
10
11    #[cfg(feature = "buffer_provider")]
12    use crate::unstable::provider::ffi::DataProvider;
13    #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
14    use crate::unstable::{errors::ffi::DataError, locale_core::ffi::Locale};
15
16    #[diplomat::opaque]
17    /// An ICU4X sentence-break segmenter, capable of finding sentence breakpoints in strings.
18    #[diplomat::rust_link(icu::segmenter::SentenceSegmenter, Struct)]
19    #[diplomat::rust_link(icu::segmenter::SentenceSegmenterBorrowed, Struct, hidden)]
20    pub struct SentenceSegmenter(icu_segmenter::SentenceSegmenter);
21
22    #[diplomat::opaque]
23    #[diplomat::rust_link(icu::segmenter::iterators::SentenceBreakIterator, Struct)]
24    #[diplomat::attr(demo_gen, disable)] // iterator type
25    pub struct SentenceBreakIteratorUtf8<'a>(
26        icu_segmenter::iterators::SentenceBreakIterator<'a, 'a, PotentiallyIllFormedUtf8>,
27    );
28
29    #[diplomat::opaque]
30    #[diplomat::rust_link(icu::segmenter::iterators::SentenceBreakIterator, Struct)]
31    #[diplomat::attr(demo_gen, disable)] // iterator type
32    pub struct SentenceBreakIteratorUtf16<'a>(
33        icu_segmenter::iterators::SentenceBreakIterator<'a, 'a, Utf16>,
34    );
35
36    #[diplomat::opaque]
37    #[diplomat::rust_link(icu::segmenter::iterators::SentenceBreakIterator, Struct)]
38    #[diplomat::attr(demo_gen, disable)] // iterator type
39    pub struct SentenceBreakIteratorLatin1<'a>(
40        icu_segmenter::iterators::SentenceBreakIterator<'a, 'a, Latin1>,
41    );
42
43    impl SentenceSegmenter {
44        /// Construct a [`SentenceSegmenter`] using compiled data. This does not assume any content locale.
45        #[diplomat::rust_link(icu::segmenter::SentenceSegmenter::new, FnInStruct)]
46        #[diplomat::rust_link(
47            icu::segmenter::options::SentenceBreakInvariantOptions,
48            Struct,
49            hidden
50        )]
51        #[diplomat::attr(auto, constructor)]
52        #[cfg(feature = "compiled_data")]
53        pub fn create() -> Box<SentenceSegmenter> {
54            Box::new(SentenceSegmenter(
55                icu_segmenter::SentenceSegmenter::new(Default::default()).static_to_owned(),
56            ))
57        }
58        /// Construct a [`SentenceSegmenter`] for content known to be of a given locale, using compiled data.
59        #[diplomat::rust_link(icu::segmenter::SentenceSegmenter::try_new, FnInStruct, hidden)]
60        #[diplomat::rust_link(icu::segmenter::options::SentenceBreakOptions, Struct, hidden)]
61        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_content_locale")]
62        #[cfg(feature = "compiled_data")]
63        pub fn create_with_content_locale(
64            locale: &Locale,
65        ) -> Result<Box<SentenceSegmenter>, DataError> {
66            Ok(Box::new(SentenceSegmenter(
67                icu_segmenter::SentenceSegmenter::try_new(locale.into())?,
68            )))
69        }
70
71        /// Construct a [`SentenceSegmenter`]  for content known to be of a given locale, using a particular data source.
72        #[diplomat::rust_link(icu::segmenter::SentenceSegmenter::try_new, FnInStruct, hidden)]
73        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_content_locale_and_provider")]
74        #[cfg(feature = "buffer_provider")]
75        pub fn create_with_content_locale_and_provider(
76            provider: &DataProvider,
77            locale: &Locale,
78        ) -> Result<Box<SentenceSegmenter>, DataError> {
79            Ok(Box::new(SentenceSegmenter(
80                icu_segmenter::SentenceSegmenter::try_new_with_buffer_provider(
81                    provider.get()?,
82                    locale.into(),
83                )?,
84            )))
85        }
86
87        /// Segments a string.
88        ///
89        /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
90        /// to the WHATWG Encoding Standard.
91        #[diplomat::rust_link(icu::segmenter::SentenceSegmenterBorrowed::segment_utf8, FnInStruct)]
92        #[diplomat::rust_link(
93            icu::segmenter::SentenceSegmenterBorrowed::segment_str,
94            FnInStruct,
95            hidden
96        )]
97        #[diplomat::attr(not(supports = utf8_strings), disable)]
98        #[diplomat::attr(*, rename = "segment")]
99        pub fn segment_utf8<'a>(
100            &'a self,
101            input: &'a DiplomatStr,
102        ) -> Box<SentenceBreakIteratorUtf8<'a>> {
103            Box::new(SentenceBreakIteratorUtf8(
104                self.0.as_borrowed().segment_utf8(input),
105            ))
106        }
107
108        /// Segments a string.
109        ///
110        /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
111        /// to the WHATWG Encoding Standard.
112        #[diplomat::rust_link(icu::segmenter::SentenceSegmenterBorrowed::segment_utf16, FnInStruct)]
113        #[diplomat::attr(not(supports = utf8_strings), rename = "segment")]
114        #[diplomat::attr(supports = utf8_strings, rename = "segment16")]
115        pub fn segment_utf16<'a>(
116            &'a self,
117            input: &'a DiplomatStr16,
118        ) -> Box<SentenceBreakIteratorUtf16<'a>> {
119            Box::new(SentenceBreakIteratorUtf16(
120                self.0.as_borrowed().segment_utf16(input),
121            ))
122        }
123
124        /// Segments a Latin-1 string.
125        #[diplomat::rust_link(
126            icu::segmenter::SentenceSegmenterBorrowed::segment_latin1,
127            FnInStruct
128        )]
129        #[diplomat::attr(not(supports = utf8_strings), disable)]
130        pub fn segment_latin1<'a>(
131            &'a self,
132            input: &'a [u8],
133        ) -> Box<SentenceBreakIteratorLatin1<'a>> {
134            Box::new(SentenceBreakIteratorLatin1(
135                self.0.as_borrowed().segment_latin1(input),
136            ))
137        }
138    }
139
140    impl<'a> SentenceBreakIteratorUtf8<'a> {
141        /// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
142        /// out of range of a 32-bit signed integer.
143        #[diplomat::rust_link(icu::segmenter::iterators::SentenceBreakIterator::next, FnInStruct)]
144        pub fn next(&mut self) -> i32 {
145            self.0
146                .next()
147                .and_then(|u| i32::try_from(u).ok())
148                .unwrap_or(-1)
149        }
150    }
151
152    impl<'a> SentenceBreakIteratorUtf16<'a> {
153        /// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
154        /// out of range of a 32-bit signed integer.
155        #[diplomat::rust_link(icu::segmenter::iterators::SentenceBreakIterator::next, FnInStruct)]
156        pub fn next(&mut self) -> i32 {
157            self.0
158                .next()
159                .and_then(|u| i32::try_from(u).ok())
160                .unwrap_or(-1)
161        }
162    }
163
164    impl<'a> SentenceBreakIteratorLatin1<'a> {
165        /// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
166        /// out of range of a 32-bit signed integer.
167        #[diplomat::rust_link(icu::segmenter::iterators::SentenceBreakIterator::next, FnInStruct)]
168        #[diplomat::rust_link(
169            icu::segmenter::iterators::SentenceBreakIterator::Item,
170            AssociatedTypeInStruct,
171            hidden
172        )]
173        pub fn next(&mut self) -> i32 {
174            self.0
175                .next()
176                .and_then(|u| i32::try_from(u).ok())
177                .unwrap_or(-1)
178        }
179    }
180}
181
182impl<'a> From<&'a crate::unstable::locale_core::ffi::Locale>
183    for icu_segmenter::options::SentenceBreakOptions<'a>
184{
185    fn from(other: &'a crate::unstable::locale_core::ffi::Locale) -> Self {
186        let mut options = icu_segmenter::options::SentenceBreakOptions::default();
187        options.content_locale = Some(&other.0.id);
188        options
189    }
190}