use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ErrorCode {
NoTextError = 0,
FormatError = 1,
FileLoadError = 2,
DuplicateLangError = 3,
NeedLoadProfileError = 4,
CantDetectError = 5,
CantOpenTrainData = 6,
TrainDataFormatError = 7,
InitParamError = 8,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
LangDetect { code: ErrorCode, message: String },
ZeroDivision { langprob: Option<Vec<f64>> },
Index {
message: &'static str,
langprob: Option<Vec<f64>>,
},
}
impl Error {
pub(crate) fn new(code: ErrorCode, message: impl Into<String>) -> Self {
Error::LangDetect {
code,
message: message.into(),
}
}
pub(crate) fn zero_division() -> Self {
Error::ZeroDivision { langprob: None }
}
pub(crate) fn zero_division_with(langprob: Vec<f64>) -> Self {
Error::ZeroDivision {
langprob: Some(langprob),
}
}
pub(crate) fn index(message: &'static str) -> Self {
Error::Index {
message,
langprob: None,
}
}
pub(crate) fn index_with(message: &'static str, langprob: Vec<f64>) -> Self {
Error::Index {
message,
langprob: Some(langprob),
}
}
pub fn code(&self) -> Option<ErrorCode> {
match self {
Error::LangDetect { code, .. } => Some(*code),
_ => None,
}
}
pub fn langprob(&self) -> Option<&[f64]> {
match self {
Error::ZeroDivision { langprob } | Error::Index { langprob, .. } => langprob.as_deref(),
Error::LangDetect { .. } => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::LangDetect { message, .. } => f.write_str(message),
Error::ZeroDivision { .. } => f.write_str("float division by zero"),
Error::Index { message, .. } => f.write_str(message),
}
}
}
impl std::error::Error for Error {}