xmpp-rs/icu/src/error.rs
Emmanuel Gil Peyrot e85b4260bd icu: Derive PartialEq and Eq
Also fixes a TODO about making comparisons better.
2022-09-20 19:15:51 +02:00

51 lines
1.4 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_error_code_to_name, UErrorCode};
use std::ffi::CStr;
/// Errors this library can produce.
#[derive(Debug, PartialEq, Eq)]
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 didnt produce a valid UTF-8 string, should never happen.
Utf8(std::string::FromUtf8Error),
/// Some ICU function didnt 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)
}
}