1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::fmt;

use crate::data::{DebugLine, Directive};

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum ErrorType {
    Badline, // un-parseable line found in input
    MultipleEmptyPostings,
    UnbalancedTransaction,
    NoAccount,
    ClosedAccount,
    DuplicateOpen,
    DuplicateClose,
    BalanceAssertion,
    UnusedPad,
    InvalidCcy,
}

#[allow(dead_code)]
#[derive(Debug)]
pub struct BeanError {
    pub ty: ErrorType, // not yet used anywhere
    pub debug: DebugLine,
    pub msg: String,
}

impl BeanError {
    pub fn new(ty: ErrorType, debug: &DebugLine, msg: &str, dir: Option<&Directive>) -> Self {
        let mut msg = msg.to_owned();
        let debug = debug.clone();
        if let Some(dir) = dir {
            let m = format!("\n{dir}");
            msg.push_str(&m);
        }
        Self { ty, debug, msg }
    }
}

impl fmt::Display for BeanError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "line:{debug}:  {msg}",
            debug = self.debug,
            msg = self.msg,
        )
    }
}