Added saving system
This commit is contained in:
parent
a33495d27f
commit
016acf0ca0
7 changed files with 36 additions and 31 deletions
|
@ -4,4 +4,5 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serde = {version = "1.0.203", features = ["derive"]}
|
||||
serde_json = "1.0"
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
[{"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"}]
|
|
@ -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<card::TaskCard>
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Library
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
// Structures
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct TaskCard {
|
||||
pub name: String,
|
||||
pub desc: String
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<board::TaskBoard>
|
||||
pub boards: Vec<board::TaskBoard>,
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue