summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cli/src/main.rs59
1 files changed, 49 insertions, 10 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs
index a43bbca..9ab5a55 100644
--- a/cli/src/main.rs
+++ b/cli/src/main.rs
@@ -2,7 +2,7 @@ use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::convert::{AsRef, TryFrom};
use std::fs;
-use std::path::Path;
+use std::path::{Path, PathBuf};
type Account = String;
type Category = String;
@@ -33,6 +33,38 @@ struct Post {
removed: bool,
}
+#[derive(Debug)]
+struct Store {
+ root: PathBuf,
+ posts: Vec<Post>,
+}
+
+impl Store {
+ //TODO Result
+ fn open(root: PathBuf) -> Option<Self> {
+ Some(Self {
+ posts: Self::open_dir(&root)?,
+ root,
+ })
+ }
+
+ //TODO Result
+ //TODO overkill? maybe we can use subfolders later on
+ fn open_dir(dir: &Path) -> Option<Vec<Post>> {
+ 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);
+ } else {
+ res.push(Post::open(&entry.path())?);
+ }
+ }
+ Some(res)
+ }
+}
+
impl Post {
fn new(transaction: Transaction) -> Self {
Self {
@@ -44,16 +76,23 @@ impl Post {
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
}
+
+ //TODO Result
+ fn open<P: AsRef<Path>>(p: &P) -> Option<Self> {
+ fs::read_to_string(p).ok().as_ref().and_then(|s| serde_json::from_str(s).ok())
+ }
}
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();
+ // 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();
+ let store = Store::open(PathBuf::from("store")).unwrap();
+ println!("{:#?}", store);
}