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>(&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(); }