Skip to main content

limabean_booking/
errors.rs

1use std::{
2    error::Error,
3    fmt::{Debug, Display},
4};
5
6use super::Booking;
7
8/// A booking error, either for an individual posting or for the transaction as a whole.
9#[derive(PartialEq, Eq, Clone, Debug)]
10pub enum BookingError {
11    Transaction(TransactionBookingError),
12    Posting(usize, PostingBookingError),
13}
14
15impl Display for BookingError {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        use BookingError::*;
18
19        match self {
20            Transaction(e) => write!(f, "{e}"),
21            Posting(idx, e) => write!(f, "posting {idx} {e}"),
22        }
23    }
24}
25
26/// A booking error for the transaction as a whole.
27#[derive(PartialEq, Eq, Clone, Debug)]
28pub enum TransactionBookingError {
29    UnsupportedBookingMethod(Booking, String),
30    TooManyMissingNumbers,
31    Unbalanced(String),
32    CannotDetermineCurrencyForBalancing,
33    AutoPostMultipleBuckets(Vec<String>),
34}
35
36impl Display for TransactionBookingError {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        use TransactionBookingError::*;
39
40        match self {
41            UnsupportedBookingMethod(booking, account) => {
42                write!(f, "unsupported booking method {booking} for {account}")
43            }
44            TooManyMissingNumbers => f.write_str("too many missing numbers for interpolation"),
45            Unbalanced(residual) => write!(f, "unbalanced transaction with residual {residual}"),
46            CannotDetermineCurrencyForBalancing => {
47                f.write_str("can't determine currency for balancing transaction")
48            }
49            AutoPostMultipleBuckets(buckets) => write!(
50                f,
51                "can't have auto-post with multiple currencies {}",
52                buckets.join(","),
53            ),
54        }
55    }
56}
57
58impl Error for TransactionBookingError {}
59
60/// A booking error for an individual posting.
61#[derive(PartialEq, Eq, Clone, Debug)]
62pub enum PostingBookingError {
63    AmbiguousAutoPost,
64    AmbiguousMatches,
65    MultipleCostCurrenciesMatch,
66    CannotInferUnits,
67    CannotInferCurrency,
68    CannotInferAnything,
69    CannotInferPricePerUnit,
70    CannotInferPriceCurrency,
71    CannotInferPrice,
72    NotEnoughLotsToReduce,
73    NoPositionMatches,
74    InferredNegativeCostPerUnit,
75    InferredNegativePricePerUnit,
76}
77
78impl Display for PostingBookingError {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        use PostingBookingError::*;
81
82        match self {
83            AmbiguousAutoPost => f.write_str("ambiguous auto-post"),
84            AmbiguousMatches => f.write_str("ambiguous matches"),
85            MultipleCostCurrenciesMatch => {
86                f.write_str("multiple currencies in cost spec matches against inventory")
87            }
88            CannotInferUnits => f.write_str("cannot infer units"),
89            CannotInferCurrency => f.write_str("cannot infer currency"),
90            CannotInferAnything => f.write_str("cannot infer anything"),
91            CannotInferPricePerUnit => f.write_str("cannot infer price per-unit"),
92            CannotInferPriceCurrency => f.write_str("cannot infer price currency"),
93            CannotInferPrice => f.write_str("cannot infer price"),
94            NotEnoughLotsToReduce => f.write_str("not enough lots to reduce"),
95            NoPositionMatches => f.write_str("no position matches"),
96            InferredNegativeCostPerUnit => f.write_str("inferred negative cost per-unit"),
97            InferredNegativePricePerUnit => f.write_str("inferred negative price per-unit"),
98        }
99    }
100}