bean_rs/
error.rs

1use pyo3::pyclass;
2use std::fmt;
3
4use crate::data::{DebugLine, Directive};
5
6#[derive(Clone, Debug, Eq, Hash, PartialEq)]
7pub enum ErrorType {
8    Badline, // un-parseable line found in input
9    MultipleEmptyPostings,
10    UnbalancedTransaction,
11    NoAccount,
12    ClosedAccount,
13    DuplicateOpen,
14    DuplicateClose,
15    BalanceAssertion,
16    UnusedPad,
17    InvalidCcy,
18}
19
20#[pyclass]
21#[derive(Clone, Debug)]
22pub struct BeanError {
23    pub ty: ErrorType,
24    pub debug: DebugLine,
25    pub msg: String,
26}
27
28impl BeanError {
29    pub fn new(ty: ErrorType, debug: &DebugLine, msg: &str, dir: Option<&Directive>) -> Self {
30        let mut msg = msg.to_owned();
31        let debug = debug.clone();
32        if let Some(dir) = dir {
33            let m = format!("\n{dir}");
34            msg.push_str(&m);
35        }
36        Self { ty, debug, msg }
37    }
38}
39
40impl fmt::Display for BeanError {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(
43            f,
44            "line:{debug}:  {msg}",
45            debug = self.debug,
46            msg = self.msg,
47        )
48    }
49}