summaryrefslogtreecommitdiffstats
path: root/cli/src
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-07-29 00:40:46 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-07-29 00:41:03 +0200
commit447647a01d345246afd8b103be0f4ff506f45bcc (patch)
tree3a171170ab9c24204a6e24311af2654705ed3f73 /cli/src
parentb2820ad0c0a6c8a116c8d7eb09eccb9838a9e30e (diff)
downloadmoney-447647a01d345246afd8b103be0f4ff506f45bcc.tar.gz
move model file
Diffstat (limited to 'cli/src')
-rw-r--r--cli/src/main.rs108
-rw-r--r--cli/src/model.rs106
2 files changed, 109 insertions, 105 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs
index f853bd2..9c6edb6 100644
--- a/cli/src/main.rs
+++ b/cli/src/main.rs
@@ -1,108 +1,6 @@
-use rust_decimal::Decimal;
-use serde::{Deserialize, Serialize};
-use std::convert::{AsRef, TryFrom};
-use std::fs;
-use std::hash::{Hash, Hasher};
-use std::path::{Path, PathBuf};
-use twox_hash::XxHash64;
+use std::path::PathBuf;
-type Account = String;
-type Category = String;
-
-#[derive(Debug)]
-#[derive(Hash)]
-#[derive(Deserialize, Serialize)]
-enum TransactionKind {
- Expense,
- Income,
- //TODO Transfer,
-}
-
-#[derive(Debug)]
-#[derive(Hash)]
-#[derive(Deserialize, Serialize)]
-struct Transaction {
- description: String,
- category: Category,
- amount: Decimal,
- kind: TransactionKind,
- from: Account,
- to: Account,
-}
-
-#[derive(Debug)]
-#[derive(Hash)]
-#[derive(Deserialize, Serialize)]
-struct Post {
- transaction: Transaction,
- 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 check if hash matches
- //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)
- }
-
- fn write(&self) -> std::io::Result<()> {
- for post in &self.posts {
- let mut path = self.root.clone();
- path.push(format!("{}", post.id()));
- post.write(&path)?;
- }
- Ok(())
- }
-}
-
-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
- }
-
- //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 id(&self) -> u64 {
- let mut h = XxHash64::default();
- self.hash(&mut h);
- h.finish()
- }
-}
+mod model;
fn main() {
// let post = Post::new(Transaction {
@@ -114,7 +12,7 @@ fn main() {
// to: "Coop Lambohov".to_string(),
// });
// post.write(&"test").unwrap();
- let store = Store::open(PathBuf::from("store")).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
new file mode 100644
index 0000000..b0dab7a
--- /dev/null
+++ b/cli/src/model.rs
@@ -0,0 +1,106 @@
+use rust_decimal::Decimal;
+use serde::{Deserialize, Serialize};
+use std::convert::AsRef;
+use std::fs;
+use std::hash::{Hash, Hasher};
+use std::path::{Path, PathBuf};
+use twox_hash::XxHash64;
+
+type Account = String;
+type Category = String;
+
+#[derive(Debug)]
+#[derive(Hash)]
+#[derive(Deserialize, Serialize)]
+pub enum TransactionKind {
+ Expense,
+ Income,
+ //TODO Transfer,
+}
+
+#[derive(Debug)]
+#[derive(Hash)]
+#[derive(Deserialize, Serialize)]
+pub struct Transaction {
+ description: String,
+ category: Category,
+ amount: Decimal,
+ kind: TransactionKind,
+ from: Account,
+ to: Account,
+}
+
+#[derive(Debug)]
+#[derive(Hash)]
+#[derive(Deserialize, Serialize)]
+pub struct Post {
+ transaction: Transaction,
+ removed: bool,
+}
+
+#[derive(Debug)]
+pub struct Store {
+ root: PathBuf,
+ posts: Vec<Post>,
+}
+
+impl Store {
+ //TODO Result
+ pub fn open(root: PathBuf) -> Option<Self> {
+ Some(Self {
+ posts: Self::open_dir(&root)?,
+ root,
+ })
+ }
+
+ //TODO check if hash matches
+ //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)
+ }
+
+ pub fn write(&self) -> std::io::Result<()> {
+ for post in &self.posts {
+ let mut path = self.root.clone();
+ path.push(format!("{}", post.id()));
+ post.write(&path)?;
+ }
+ Ok(())
+ }
+}
+
+impl Post {
+ pub 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
+ }
+
+ //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 id(&self) -> u64 {
+ let mut h = XxHash64::default();
+ self.hash(&mut h);
+ h.finish()
+ }
+}
+