diff --git a/Cargo.toml b/Cargo.toml index 720b2ca..62eab95 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +serde = {version = "1.0.203", features = ["derive"]} serde_json = "1.0" diff --git a/data/user.json b/data/user.json index d9eebec..a5500ce 100644 --- a/data/user.json +++ b/data/user.json @@ -1,28 +1 @@ -[ - { - "name": "Shipments", - "cards": [ - { - "name": "Dell Latitude 3410", - "desc": "UPS" - }, - { - "name": "Microsoft Surface Laptop Studio", - "desc": "USPS" - } - ] - }, - { - "name": "Projects", - "cards": [ - { - "name": "RustyPass", - "desc": "Done" - }, - { - "name": "Tasky", - "desc": "W.I.P" - } - ] - } -] \ No newline at end of file +[{"cards":[{"desc":"UPS","name":"Dell Latitude 3410"},{"desc":"USPS","name":"Microsoft Surface Laptop Studio"},{"desc":"UPS","name":"MacBook Pro"}],"name":"Shipments"},{"cards":[{"desc":"Done","name":"RustyPass"},{"desc":"W.I.P","name":"Tasky"}],"name":"Projects"}] \ No newline at end of file diff --git a/src/board.rs b/src/board.rs index 36e3242..baf558e 100755 --- a/src/board.rs +++ b/src/board.rs @@ -1,7 +1,9 @@ // Libraries +use serde::{Serialize, Deserialize}; use crate::card; // Structures +#[derive(Serialize, Deserialize)] pub struct TaskBoard { pub name: String, pub cards: Vec diff --git a/src/card.rs b/src/card.rs index 4da7b0a..f591a88 100755 --- a/src/card.rs +++ b/src/card.rs @@ -1,6 +1,8 @@ // Library +use serde::{Serialize, Deserialize}; // Structures +#[derive(Serialize, Deserialize)] pub struct TaskCard { pub name: String, pub desc: String diff --git a/src/data.rs b/src/data.rs index 509193a..4a84707 100644 --- a/src/data.rs +++ b/src/data.rs @@ -15,4 +15,7 @@ impl Data { // Returning the result return Data {content: content}; } + pub fn write(path: String, content: String){ + fs::write(path, content).expect("Failed to write file"); + } } \ No newline at end of file diff --git a/src/interface.rs b/src/interface.rs index 88c0189..8be6f77 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -3,6 +3,7 @@ use crate::{board, card, manager}; use std::{io, process::exit}; // Constants +const COLOR_ERROR: &str = "\x1b[31m"; const COLOR_CARD: &str = "\x1b[32m"; const COLOR_TITLE: &str = "\x1b[33m"; const COLOR_DESC: &str = "\x1b[35m"; @@ -335,5 +336,8 @@ impl<'a> Interface<'a> { } // Save + if !self.manager.save() { + println!("{}ERROR{}: Failed to save", COLOR_ERROR, COLOR_RESET); + } } } \ No newline at end of file diff --git a/src/manager.rs b/src/manager.rs index abe1774..7b84303 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -1,20 +1,24 @@ // Libraries -use serde_json::{self, Value}; +use serde_json::{self, json, Value}; use crate::board; use crate::card; use crate::data; // Structures pub struct Manager{ - pub boards: Vec + pub boards: Vec, + path: String } // Implementations impl Manager{ pub fn init() -> Manager { - return Manager {boards: vec![]}; + return Manager {boards: vec![], path: String::new()}; } pub fn parse(&mut self, path: String){ + // Setting variables + self.path = path.clone(); + // Loading a data object from the path let loaded: data::Data = data::Data::init(path); @@ -38,4 +42,20 @@ impl Manager{ self.boards.push(board::TaskBoard {name: String::from(item["name"].as_str().expect("Failed to parse string")), cards: cards}); } } + pub fn save(&mut self) -> bool { + // Did we even load from a file? + if self.path.is_empty() { + return false; + } + + // Turning all of my data into a JSON file + let boards = &self.boards; + let json_out = json!(boards); + + // Saving the file to our path + data::Data::write(self.path.clone(), json_out.to_string()); + + // Worked! + return true; + } } \ No newline at end of file