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
use strkey::StrKey;
use error::{Result, Error};

pub struct Account {
    strkey: StrKey,
    seqnum: u64,
}


impl Account {
    pub fn from_account_id(account_id: &str, seqnum: u64) -> Result<Account> {
        let id = StrKey::from_account_id(&account_id)?;
        Self::new(&id, seqnum)
    }

    pub fn new(account_id: &StrKey, seqnum: u64) -> Result<Account> {
        match *account_id {            
            StrKey::AccountId(_) => {
                Ok(Account {
                    strkey: account_id.clone(),
                    seqnum: seqnum,
                })
            }
            _ => Err(Error::TBD), // invalid account id
        }
    }

    pub fn inc_seqnum(&mut self) -> u64 {
        let old = self.seqnum.clone();
        self.seqnum += 1;
        old
    }

    pub fn seqnum(&self) -> u64 {
        self.seqnum.clone()
    }

    pub fn public_key(&self) -> &StrKey {
        &self.strkey
    }
}


#[cfg(test)]
mod tests {
    use Account;

    #[test]
    fn test_inc_seqnum() {
        let mut account =
            Account::from_account_id("GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB",
                                     100)
                .unwrap();
        let mut expected = 100;
        for _ in 1..10 {
            assert_eq!(account.seqnum(), expected);
            expected += 1;
            account.inc_seqnum();
        }
    }
}