diff options
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/src/main.rs | 14 | ||||
| -rw-r--r-- | cli/src/model.rs | 41 |
2 files changed, 23 insertions, 32 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs index 606d3c1..a760401 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -35,6 +35,8 @@ struct Mn { // mn insert expense --account Kortkonto --category a --category b fn main() { + let mut store = model::Store::open(PathBuf::from("store")).unwrap(); + println!("{:?}", store); match Mn::from_args().command { Command::Insert { kind, @@ -43,19 +45,17 @@ fn main() { amount, description, } => { - let post = model::Post::new(model::Transaction { + let transaction = model::Transaction { kind, to: account, from: "Default".to_string(), category, amount, description - }); - println!("{:?}", post); + }; + println!("{:?}", transaction); + store.push(transaction); + store.write().unwrap(); } } - // post.write(&"test").unwrap(); - // let store = model::Store::open(PathBuf::from("store")).unwrap(); - // println!("{:#?}", store); - // store.write().unwrap(); } diff --git a/cli/src/model.rs b/cli/src/model.rs index c93897f..01b645a 100644 --- a/cli/src/model.rs +++ b/cli/src/model.rs @@ -46,24 +46,18 @@ pub struct Transaction { } #[derive(Debug)] -#[derive(Hash)] -#[derive(Deserialize, Serialize)] -pub struct Post { - transaction: Transaction, - removed: bool, -} - -#[derive(Debug)] pub struct Store { root: PathBuf, - posts: Vec<Post>, + transactions: Vec<Transaction>, + new_transactions: Vec<Transaction>, } impl Store { //TODO Result pub fn open(root: PathBuf) -> Option<Self> { Some(Self { - posts: Self::open_dir(&root)?, + transactions: Self::open_dir(&root)?, + new_transactions: Vec::new(), root, }) } @@ -71,38 +65,35 @@ impl Store { //TODO check if hash matches //TODO Result //TODO overkill? maybe we can use subfolders later on - fn open_dir(dir: &Path) -> Option<Vec<Post>> { + fn open_dir(dir: &Path) -> Option<Vec<Transaction>> { let mut res = Vec::new(); for entry in std::fs::read_dir(dir).ok()? { let entry = entry.ok()?; if entry.file_type().ok()?.is_dir() { - let mut posts = Self::open_dir(&entry.path())?; - res.append(&mut posts); + let mut transactions = Self::open_dir(&entry.path())?; + res.append(&mut transactions); } else { - res.push(Post::open(&entry.path())?); + res.push(Transaction::open(&entry.path())?); } } Some(res) } + pub fn push(&mut self, transaction: Transaction) { + self.new_transactions.push(transaction); + } + pub fn write(&self) -> std::io::Result<()> { - for post in &self.posts { + for transaction in &self.new_transactions { let mut path = self.root.clone(); - path.push(format!("{}", post.id())); - post.write(&path)?; + path.push(format!("{}", transaction.id())); + transaction.write(&path)?; } Ok(()) } } -impl Post { - pub fn new(transaction: Transaction) -> Self { - Self { - transaction, - removed: false, - } - } - +impl Transaction { 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 } |
