paymentlib/enums/
transaction_type.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use serde::Deserialize;
use serde::Serialize;
use std::fmt;
use std::fmt::Formatter;
use std::str;

#[cfg(feature = "diesel")]
use {
    diesel::backend::Backend,
    diesel::deserialize::FromSql,
    diesel::pg::Pg,
    diesel::serialize::{IsNull, Output, ToSql},
    diesel::types::Varchar,
    diesel::{deserialize, not_none, serialize},
    diesel::{AsExpression, FromSqlRow},
    std::io::Write,
};

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "diesel", derive(AsExpression, FromSqlRow))]
#[cfg_attr(feature = "diesel", sql_type = "Varchar")]
pub enum TransactionType {
    DayStart,
    Sale,
    CashDeposit,
    CashWithdrawal,
    BankDraw,
    Refund,
    Expense,
    Rounding,
    Reconciliation,
    Exchange,
    CheckDeposit,
    CheckIssuance,
    CheckCashing,
    CheckRefund,
    CheckClearing,
    VoidCheck,
    Undefined,
}

impl fmt::Display for TransactionType {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl Default for TransactionType {
    fn default() -> Self {
        TransactionType::Undefined
    }
}
#[cfg(feature = "diesel")]
impl ToSql<Varchar, Pg> for TransactionType {
    fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
        match *self {
            TransactionType::DayStart => out.write_all(b"DayStart")?,
            TransactionType::Sale => out.write_all(b"Sale")?,
            TransactionType::CashDeposit => out.write_all(b"CashDeposit")?,
            TransactionType::CashWithdrawal => out.write_all(b"CashWithdrawal")?,
            TransactionType::BankDraw => out.write_all(b"BankDraw")?,
            TransactionType::Refund => out.write_all(b"Refund")?,
            TransactionType::Expense => out.write_all(b"Expense")?,
            TransactionType::Rounding => out.write_all(b"Rounding")?,
            TransactionType::Reconciliation => out.write_all(b"Reconciliation")?,
            TransactionType::Exchange => out.write_all(b"Exchange")?,
            TransactionType::CheckDeposit => out.write_all(b"CheckDeposit")?,
            TransactionType::CheckIssuance => out.write_all(b"CheckCashing")?,
            TransactionType::CheckCashing => out.write_all(b"DayStart")?,
            TransactionType::CheckRefund => out.write_all(b"CheckRefund")?,
            TransactionType::CheckClearing => out.write_all(b"CheckClearing")?,
            TransactionType::VoidCheck => out.write_all(b"VoidCheck")?,
            TransactionType::Undefined => out.write_all(b"Undefined")?,
        }
        Ok(IsNull::No)
    }
}

#[cfg(feature = "diesel")]
impl FromSql<Varchar, Pg> for TransactionType {
    fn from_sql(bytes: Option<&<Pg as Backend>::RawValue>) -> deserialize::Result<Self> {
        match not_none!(bytes) {
            b"DayStart" => Ok(TransactionType::DayStart),
            b"Sale" => Ok(TransactionType::Sale),
            b"CashDeposit" => Ok(TransactionType::CashDeposit),
            b"CashWithdrawal" => Ok(TransactionType::CashWithdrawal),
            b"BankDraw" => Ok(TransactionType::BankDraw),
            b"Refund" => Ok(TransactionType::Refund),
            b"Expense" => Ok(TransactionType::Expense),
            b"Rounding" => Ok(TransactionType::Rounding),
            b"Reconciliation" => Ok(TransactionType::Reconciliation),
            b"Exchange" => Ok(TransactionType::Exchange),
            b"CheckDeposit" => Ok(TransactionType::CheckDeposit),
            b"CheckIssuance" => Ok(TransactionType::CheckIssuance),
            b"CheckCashing" => Ok(TransactionType::CheckCashing),
            b"CheckRefund" => Ok(TransactionType::CheckRefund),
            b"CheckClearing" => Ok(TransactionType::CheckClearing),
            b"VoidCheck" => Ok(TransactionType::VoidCheck),
            b"Undefined" => Ok(TransactionType::Undefined),
            _ => Err("Unrecognized enum variant".into()),
        }
    }
}