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
use std::fmt;
use std::cmp::Ordering;

/// Transaction trait
pub trait Transaction: Clone {
    /// begin a transaction
    fn begin(&mut self);

    /// commit a transaction
    fn commit(&mut self) -> Result<(), TransactionError>;

    /// revert a transaction
    fn revert(&mut self) -> Result<(), TransactionError>;

    /// amount of open transactions
    fn len(&self) -> usize;
}

#[derive(Clone)]
pub struct TransactionData<T> {
    pub t: Vec<T>
}

impl<T> TransactionData<T> {
    pub fn new() -> Self {
        Self {
            t: vec![]
        }
    }
}

impl<T> PartialEq<TransactionData<T>> for TransactionData<T>
where T: PartialEq<T> {
    fn eq(&self, other: &TransactionData<T>) -> bool {
        return self.t == other.t;
    }

    fn ne(&self, other: &TransactionData<T>) -> bool {
        return self.t != other.t;
    }
}

impl<T> Eq for TransactionData<T>
where T: Eq {}

impl<T> PartialOrd for TransactionData<T>
where T: PartialOrd {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.t.partial_cmp(&other.t)
    }
}

impl<T> Ord for TransactionData<T>
where T: Ord {
    fn cmp(&self, other: &Self) -> Ordering {
        self.t.cmp(&other.t)
    }
}

/// Transaction Errors
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum TransactionErrorType {
    TransactionNotStarted
}

/// Transaction Error Type
#[derive(Debug, Copy, Clone)]
pub struct TransactionError {
    pub cause: TransactionErrorType
}

impl TransactionError {
    pub fn new(cause: TransactionErrorType) -> Self {
        Self {
            cause
        }
    }

    pub fn to_string(&self) -> &str {
        match self.cause {
            TransactionErrorType::TransactionNotStarted => "Transaction not started"
        }
    }
}

impl std::error::Error for TransactionError {
    fn description(&self) -> &str {
        return self.to_string();
    }
}

impl fmt::Display for TransactionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}