use chrono::NaiveDate;
use rustledger_parser::{Span, Spanned};
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorCode {
AccountNotOpen,
AccountAlreadyOpen,
AccountClosed,
AccountCloseNotEmpty,
InvalidAccountName,
BalanceAssertionFailed,
BalanceToleranceExceeded,
PadWithoutBalance,
MultiplePadForBalance,
TransactionUnbalanced,
MultipleInterpolation,
NoPostings,
SinglePosting,
NoMatchingLot,
InsufficientUnits,
AmbiguousLotMatch,
NegativeInventory,
NegativeCost,
UndeclaredCurrency,
CurrencyNotAllowed,
DuplicateMetadataKey,
InvalidMetadataValue,
UnknownOption,
InvalidOptionValue,
DuplicateOption,
DocumentNotFound,
DateOutOfOrder,
FutureDate,
}
impl ErrorCode {
#[must_use]
pub const fn code(&self) -> &'static str {
match self {
Self::AccountNotOpen => "E1001",
Self::AccountAlreadyOpen => "E1002",
Self::AccountClosed => "E1003",
Self::AccountCloseNotEmpty => "E1004",
Self::InvalidAccountName => "E1005",
Self::BalanceAssertionFailed => "E2001",
Self::BalanceToleranceExceeded => "E2002",
Self::PadWithoutBalance => "E2003",
Self::MultiplePadForBalance => "E2004",
Self::TransactionUnbalanced => "E3001",
Self::MultipleInterpolation => "E3002",
Self::NoPostings => "E3003",
Self::SinglePosting => "E3004",
Self::NoMatchingLot => "E4001",
Self::InsufficientUnits => "E4002",
Self::AmbiguousLotMatch => "E4003",
Self::NegativeInventory => "E4004",
Self::NegativeCost => "E4005",
Self::UndeclaredCurrency => "E5001",
Self::CurrencyNotAllowed => "E5002",
Self::DuplicateMetadataKey => "E6001",
Self::InvalidMetadataValue => "E6002",
Self::UnknownOption => "E7001",
Self::InvalidOptionValue => "E7002",
Self::DuplicateOption => "E7003",
Self::DocumentNotFound => "E8001",
Self::DateOutOfOrder => "E10001",
Self::FutureDate => "E10002",
}
}
#[must_use]
pub const fn is_warning(&self) -> bool {
matches!(
self,
Self::FutureDate
| Self::SinglePosting
| Self::AccountCloseNotEmpty
| Self::DateOutOfOrder
)
}
#[must_use]
pub const fn is_info(&self) -> bool {
matches!(self, Self::DateOutOfOrder)
}
#[must_use]
pub const fn severity(&self) -> Severity {
if self.is_info() {
Severity::Info
} else if self.is_warning() {
Severity::Warning
} else {
Severity::Error
}
}
#[must_use]
pub const fn is_parse_phase(&self) -> bool {
matches!(self, Self::InvalidAccountName)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Severity {
Error,
Warning,
Info,
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.code())
}
}
#[derive(Debug, Clone, Error)]
#[error("[{code}] {message}")]
pub struct ValidationError {
pub code: ErrorCode,
pub message: String,
pub date: NaiveDate,
pub context: Option<String>,
pub span: Option<Span>,
pub file_id: Option<u16>,
}
impl ValidationError {
#[must_use]
pub fn new(code: ErrorCode, message: impl Into<String>, date: NaiveDate) -> Self {
Self {
code,
message: message.into(),
date,
context: None,
span: None,
file_id: None,
}
}
#[must_use]
pub fn with_location<T>(
code: ErrorCode,
message: impl Into<String>,
date: NaiveDate,
spanned: &Spanned<T>,
) -> Self {
Self {
code,
message: message.into(),
date,
context: None,
span: Some(spanned.span),
file_id: Some(spanned.file_id),
}
}
#[must_use]
pub fn with_context(mut self, context: impl Into<String>) -> Self {
self.context = Some(context.into());
self
}
#[must_use]
pub const fn at_location<T>(mut self, spanned: &Spanned<T>) -> Self {
self.span = Some(spanned.span);
self.file_id = Some(spanned.file_id);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn invalid_account_name_is_parse_phase() {
assert!(ErrorCode::InvalidAccountName.is_parse_phase());
}
#[test]
fn other_account_errors_are_validate_phase() {
assert!(!ErrorCode::AccountNotOpen.is_parse_phase());
assert!(!ErrorCode::AccountAlreadyOpen.is_parse_phase());
assert!(!ErrorCode::AccountClosed.is_parse_phase());
}
#[test]
fn non_account_errors_are_validate_phase() {
assert!(!ErrorCode::TransactionUnbalanced.is_parse_phase());
assert!(!ErrorCode::BalanceAssertionFailed.is_parse_phase());
assert!(!ErrorCode::UnknownOption.is_parse_phase());
}
}