xmpp-rs/icu/src/spoof.rs
Emmanuel Gil Peyrot 8d4ed296d0 icu: Make links in docstrings actual links
Thanks `cargo doc` for the helpful warnings!
2022-09-20 19:14:57 +02:00

52 lines
1.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Crate wrapping what we need from ICUs C API for JIDs.
//!
//! See <http://site.icu-project.org/>
use crate::bindings::{
icu_spoof_get_skeleton, icu_spoof_open, icu_spoof_set_checks, UErrorCode, USpoofChecker,
U_ZERO_ERROR,
};
use crate::error::Error;
/// TODO: spoof checker.
pub struct SpoofChecker {
inner: *mut USpoofChecker,
}
impl SpoofChecker {
/// Create a new SpoofChecker.
pub fn new(checks: i32) -> Result<SpoofChecker, UErrorCode> {
let mut err: UErrorCode = U_ZERO_ERROR;
let inner = unsafe { icu_spoof_open(&mut err) };
if err != U_ZERO_ERROR {
return Err(err);
}
unsafe { icu_spoof_set_checks(inner, checks, &mut err) };
if err != U_ZERO_ERROR {
return Err(err);
}
Ok(SpoofChecker { inner })
}
/// Transform a string into a skeleton for matching it with other potentially similar strings.
pub fn get_skeleton(&self, input: &str) -> Result<String, Error> {
let mut err: UErrorCode = U_ZERO_ERROR;
let mut dest: Vec<u8> = vec![0u8; 256];
let len = unsafe {
icu_spoof_get_skeleton(
self.inner,
0,
input.as_ptr(),
input.len() as i32,
dest.as_mut_ptr(),
dest.len() as i32,
&mut err,
)
};
if err != U_ZERO_ERROR {
return Err(Error::from_icu_code(err));
}
dest.truncate(len as usize);
Ok(String::from_utf8(dest)?)
}
}