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
|
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::convert::{AsRef, TryFrom};
use std::fs;
use std::path::Path;
type Account = String;
type Category = String;
#[derive(Debug)]
#[derive(Deserialize, Serialize)]
enum TransactionKind {
Expense,
Income,
//TODO Transfer,
}
#[derive(Debug)]
#[derive(Deserialize, Serialize)]
struct Transaction {
description: String,
category: Category,
amount: Decimal,
kind: TransactionKind,
from: Account,
to: Account,
}
#[derive(Debug)]
#[derive(Deserialize, Serialize)]
struct Post {
transaction: Transaction,
removed: bool,
}
impl Post {
fn new(transaction: Transaction) -> Self {
Self {
transaction,
removed: false,
}
}
fn write<P: AsRef<Path>>(&self, p: &P) -> std::io::Result<()> {
fs::write(p, serde_json::to_string_pretty(self).unwrap()) //TODO control pretty or not
}
}
fn main() {
let post = Post::new(Transaction {
description: "Test".to_string(),
category: "Mat".to_string(),
amount: Decimal::try_from(120.4_f64).unwrap(),
kind: TransactionKind::Expense,
from: "Kortkonto".to_string(),
to: "Coop Lambohov".to_string(),
});
post.write(&"test").unwrap();
}
|