1//! Crate wrapping what we need from ICU’s C API for JIDs.
2//!
3//! See <http://site.icu-project.org/>
4
5use crate::bindings::{icu_error_code_to_name, UErrorCode};
6use std::ffi::CStr;
7
8/// Errors this library can produce.
9#[derive(Debug, PartialEq, Eq)]
10pub enum Error {
11 /// An error produced by one of the ICU functions.
12 Icu(String),
13
14 /// An error produced by one of the IDNA2008 ICU functions.
15 Idna(u32),
16
17 /// Some ICU function didn’t produce a valid UTF-8 string, should never happen.
18 Utf8(std::string::FromUtf8Error),
19
20 /// Some ICU function didn’t produce a valid UTF-8 string, should never happen.
21 Utf16(std::char::DecodeUtf16Error),
22
23 /// Some string was too long for its profile in JID.
24 TooLong,
25}
26
27impl Error {
28 pub(crate) fn from_icu_code(err: UErrorCode) -> Error {
29 let ptr = unsafe { icu_error_code_to_name(err) };
30 let c_str = unsafe { CStr::from_ptr(ptr) };
31 Error::Icu(c_str.to_string_lossy().into_owned())
32 }
33}
34
35impl From<UErrorCode> for Error {
36 fn from(err: UErrorCode) -> Error {
37 Error::from_icu_code(err)
38 }
39}
40
41impl From<std::string::FromUtf8Error> for Error {
42 fn from(err: std::string::FromUtf8Error) -> Error {
43 Error::Utf8(err)
44 }
45}
46
47impl From<std::char::DecodeUtf16Error> for Error {
48 fn from(err: std::char::DecodeUtf16Error) -> Error {
49 Error::Utf16(err)
50 }
51}