Added saving system

This commit is contained in:
Maddox Werts 2024-06-23 15:51:43 -04:00
parent a33495d27f
commit 016acf0ca0
7 changed files with 36 additions and 31 deletions

View file

@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
serde = {version = "1.0.203", features = ["derive"]}
serde_json = "1.0" serde_json = "1.0"

View file

@ -1,28 +1 @@
[ [{"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"}]
{
"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"
}
]
}
]

View file

@ -1,7 +1,9 @@
// Libraries // Libraries
use serde::{Serialize, Deserialize};
use crate::card; use crate::card;
// Structures // Structures
#[derive(Serialize, Deserialize)]
pub struct TaskBoard { pub struct TaskBoard {
pub name: String, pub name: String,
pub cards: Vec<card::TaskCard> pub cards: Vec<card::TaskCard>

View file

@ -1,6 +1,8 @@
// Library // Library
use serde::{Serialize, Deserialize};
// Structures // Structures
#[derive(Serialize, Deserialize)]
pub struct TaskCard { pub struct TaskCard {
pub name: String, pub name: String,
pub desc: String pub desc: String

View file

@ -15,4 +15,7 @@ impl Data {
// Returning the result // Returning the result
return Data {content: content}; return Data {content: content};
} }
pub fn write(path: String, content: String){
fs::write(path, content).expect("Failed to write file");
}
} }

View file

@ -3,6 +3,7 @@ use crate::{board, card, manager};
use std::{io, process::exit}; use std::{io, process::exit};
// Constants // Constants
const COLOR_ERROR: &str = "\x1b[31m";
const COLOR_CARD: &str = "\x1b[32m"; const COLOR_CARD: &str = "\x1b[32m";
const COLOR_TITLE: &str = "\x1b[33m"; const COLOR_TITLE: &str = "\x1b[33m";
const COLOR_DESC: &str = "\x1b[35m"; const COLOR_DESC: &str = "\x1b[35m";
@ -335,5 +336,8 @@ impl<'a> Interface<'a> {
} }
// Save // Save
if !self.manager.save() {
println!("{}ERROR{}: Failed to save", COLOR_ERROR, COLOR_RESET);
}
} }
} }

View file

@ -1,20 +1,24 @@
// Libraries // Libraries
use serde_json::{self, Value}; use serde_json::{self, json, Value};
use crate::board; use crate::board;
use crate::card; use crate::card;
use crate::data; use crate::data;
// Structures // Structures
pub struct Manager{ pub struct Manager{
pub boards: Vec<board::TaskBoard> pub boards: Vec<board::TaskBoard>,
path: String
} }
// Implementations // Implementations
impl Manager{ impl Manager{
pub fn init() -> Manager { pub fn init() -> Manager {
return Manager {boards: vec![]}; return Manager {boards: vec![], path: String::new()};
} }
pub fn parse(&mut self, path: String){ pub fn parse(&mut self, path: String){
// Setting variables
self.path = path.clone();
// Loading a data object from the path // Loading a data object from the path
let loaded: data::Data = data::Data::init(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}); 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;
}
} }