#[diplomat::bridge]
pub mod ffi {
use crate::errors::ffi::ICU4XError;
use crate::provider::ffi::ICU4XDataProvider;
use alloc::boxed::Box;
use core::convert::TryFrom;
use icu_provider::DataProvider;
use icu_segmenter::provider::GraphemeClusterBreakDataV1Marker;
use icu_segmenter::{
GraphemeClusterBreakIteratorLatin1, GraphemeClusterBreakIteratorPotentiallyIllFormedUtf8,
GraphemeClusterBreakIteratorUtf16, GraphemeClusterSegmenter,
};
#[diplomat::opaque]
#[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenter, Struct)]
pub struct ICU4XGraphemeClusterSegmenter(GraphemeClusterSegmenter);
#[diplomat::opaque]
pub struct ICU4XGraphemeClusterBreakIteratorUtf8<'a>(
GraphemeClusterBreakIteratorPotentiallyIllFormedUtf8<'a, 'a>,
);
#[diplomat::opaque]
pub struct ICU4XGraphemeClusterBreakIteratorUtf16<'a>(
GraphemeClusterBreakIteratorUtf16<'a, 'a>,
);
#[diplomat::opaque]
pub struct ICU4XGraphemeClusterBreakIteratorLatin1<'a>(
GraphemeClusterBreakIteratorLatin1<'a, 'a>,
);
impl ICU4XGraphemeClusterSegmenter {
#[diplomat::rust_link(
icu::segmenter::GraphemeClusterSegmenter::try_new_unstable,
FnInStruct
)]
pub fn create(
provider: &ICU4XDataProvider,
) -> Result<Box<ICU4XGraphemeClusterSegmenter>, ICU4XError> {
Self::try_new_impl(&provider.0)
}
fn try_new_impl<D>(provider: &D) -> Result<Box<ICU4XGraphemeClusterSegmenter>, ICU4XError>
where
D: DataProvider<GraphemeClusterBreakDataV1Marker> + ?Sized,
{
Ok(Box::new(ICU4XGraphemeClusterSegmenter(
GraphemeClusterSegmenter::try_new_unstable(provider)?,
)))
}
#[diplomat::rust_link(
icu::segmenter::GraphemeClusterSegmenter::segment_str,
FnInStruct,
hidden
)]
#[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenter::segment_utf8, FnInStruct)]
pub fn segment_utf8<'a>(
&'a self,
input: &'a str,
) -> Box<ICU4XGraphemeClusterBreakIteratorUtf8<'a>> {
let input = input.as_bytes(); Box::new(ICU4XGraphemeClusterBreakIteratorUtf8(
self.0.segment_utf8(input),
))
}
#[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenter::segment_utf16, FnInStruct)]
pub fn segment_utf16<'a>(
&'a self,
input: &'a [u16],
) -> Box<ICU4XGraphemeClusterBreakIteratorUtf16<'a>> {
Box::new(ICU4XGraphemeClusterBreakIteratorUtf16(
self.0.segment_utf16(input),
))
}
#[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenter::segment_latin1, FnInStruct)]
pub fn segment_latin1<'a>(
&'a self,
input: &'a [u8],
) -> Box<ICU4XGraphemeClusterBreakIteratorLatin1<'a>> {
Box::new(ICU4XGraphemeClusterBreakIteratorLatin1(
self.0.segment_latin1(input),
))
}
}
impl<'a> ICU4XGraphemeClusterBreakIteratorUtf8<'a> {
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> i32 {
self.0
.next()
.and_then(|u| i32::try_from(u).ok())
.unwrap_or(-1)
}
}
impl<'a> ICU4XGraphemeClusterBreakIteratorUtf16<'a> {
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> i32 {
self.0
.next()
.and_then(|u| i32::try_from(u).ok())
.unwrap_or(-1)
}
}
impl<'a> ICU4XGraphemeClusterBreakIteratorLatin1<'a> {
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> i32 {
self.0
.next()
.and_then(|u| i32::try_from(u).ok())
.unwrap_or(-1)
}
}
}