2019-12-02 01:57:20 +00:00
|
|
|
|
//! Crate wrapping what we need from ICU’s C API for JIDs.
|
|
|
|
|
//!
|
2022-09-20 17:13:23 +00:00
|
|
|
|
//! See <http://site.icu-project.org/>
|
2019-12-02 01:57:20 +00:00
|
|
|
|
|
|
|
|
|
use crate::bindings::{icu_error_code_to_name, UErrorCode};
|
|
|
|
|
use std::ffi::CStr;
|
|
|
|
|
|
|
|
|
|
/// Errors this library can produce.
|
2022-09-20 17:13:55 +00:00
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2019-12-02 01:57:20 +00:00
|
|
|
|
pub enum Error {
|
|
|
|
|
/// An error produced by one of the ICU functions.
|
|
|
|
|
Icu(String),
|
|
|
|
|
|
|
|
|
|
/// An error produced by one of the IDNA2008 ICU functions.
|
|
|
|
|
Idna(u32),
|
|
|
|
|
|
|
|
|
|
/// Some ICU function didn’t produce a valid UTF-8 string, should never happen.
|
|
|
|
|
Utf8(std::string::FromUtf8Error),
|
|
|
|
|
|
|
|
|
|
/// Some ICU function didn’t produce a valid UTF-8 string, should never happen.
|
|
|
|
|
Utf16(std::char::DecodeUtf16Error),
|
|
|
|
|
|
|
|
|
|
/// Some string was too long for its profile in JID.
|
|
|
|
|
TooLong,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
|
pub(crate) fn from_icu_code(err: UErrorCode) -> Error {
|
|
|
|
|
let ptr = unsafe { icu_error_code_to_name(err) };
|
|
|
|
|
let c_str = unsafe { CStr::from_ptr(ptr) };
|
|
|
|
|
Error::Icu(c_str.to_string_lossy().into_owned())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<UErrorCode> for Error {
|
|
|
|
|
fn from(err: UErrorCode) -> Error {
|
|
|
|
|
Error::from_icu_code(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<std::string::FromUtf8Error> for Error {
|
|
|
|
|
fn from(err: std::string::FromUtf8Error) -> Error {
|
|
|
|
|
Error::Utf8(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<std::char::DecodeUtf16Error> for Error {
|
|
|
|
|
fn from(err: std::char::DecodeUtf16Error) -> Error {
|
|
|
|
|
Error::Utf16(err)
|
|
|
|
|
}
|
|
|
|
|
}
|