use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum DocumentType {
Factura,
Boleta,
NotaCredito,
NotaDebito,
ResumenDiario,
ComunicacionBaja,
GuiaRemision,
Retencion,
Percepcion,
ResumenReversion,
}
impl DocumentType {
pub const ALL: [Self; 10] = [
Self::Factura,
Self::Boleta,
Self::NotaCredito,
Self::NotaDebito,
Self::ResumenDiario,
Self::ComunicacionBaja,
Self::GuiaRemision,
Self::Retencion,
Self::Percepcion,
Self::ResumenReversion,
];
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::Factura => "01",
Self::Boleta => "03",
Self::NotaCredito => "07",
Self::NotaDebito => "08",
Self::ResumenDiario => "RC",
Self::ComunicacionBaja => "RA",
Self::GuiaRemision => "09",
Self::Retencion => "20",
Self::Percepcion => "40",
Self::ResumenReversion => "RR",
}
}
#[must_use]
pub fn from_code(code: &str) -> Option<Self> {
Self::ALL.into_iter().find(|t| t.code() == code)
}
}
impl fmt::Display for DocumentType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ErrorLevel {
Exception,
Observation,
}
impl ErrorLevel {
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::Exception => "1",
Self::Observation => "2",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct CpeError {
pub code: Option<String>,
pub message: Option<String>,
pub level: Option<ErrorLevel>,
pub node_path: Option<String>,
pub node_value: Option<String>,
}
impl CpeError {
#[must_use]
pub fn unknown_rule() -> Self {
Self {
code: Some("0305".to_owned()),
level: Some(ErrorLevel::Exception),
..Self::default()
}
}
#[must_use]
pub fn is_exception(&self) -> bool {
self.level == Some(ErrorLevel::Exception)
}
}