Adding details and round trip persistance in a json file
I have added some detail to the task structure:
// Task index is 0 when created so I can use the rocket_contrib
// Json struct (maybe there is a way to default fields???)
pub struct Task {
//index/id
pub index: u32,
pub name: String,
pub desc: String,
//time in minutes spent doing the task
pub minutes: u32,
//unix timestamps start and due.
pub started: u64,
pub due: u64,
}
The fields are fairly self explanitory, the dates will be unix timestamps for ease of storage.
There is a read_file and write_file method now, and I have an optional passed to the constructor so that I can either create a populated object from the file, or an empty list.
// takes optional filename so we can create populated list if required.
pub fn new(filename: Option<&str>) -> TaskList {
//create the list as mut in case there is a filename
let mut t = TaskList {
tasks: RwLock::new(TaskHashMap::new()),
};
//I'm starting to like the sum types and the matching
match filename {
Some(filename) => {
t.read_file(filename);
t
}
None => t,
}
}
pub fn read_file(&mut self, name: &str) {
let mut my_tasks = self.tasks.write().expect("Should have locked for write");
let f = OpenOptions::new().read(true).open(name);
match f {
Ok(f) => {
let fb = std::io::BufReader::new(f);
let my_vec: Vec<Task> = serde_json::from_reader(fb).expect("Failed to read tasks");
for t in my_vec {
my_tasks.insert(t.index, t);
}
}
_ => (),
}
}
I have now added Angular index page and can serve the app from the running rust executable
fn main() {
let task_db: tasks::TaskList = tasks::TaskList::new(Some("tasklist.json"));
rocket::ignite()
.mount("/", routes![get_tasks, add_task, get_task_by_id])
.mount("/", StaticFiles::from("view/dist"))
.manage(task_db)
.launch();
}
The view/dist
folder containst the output from the ng build
command when executed in the view/todo-app folder (or npm run build
)
view/dist
was generated using ng new and so is now set up for angular commands. Angular is installed with npm install -g @angular/cli
I changed the default output directory in the
{
"compileOnSave": false,
"compilerOptions": {
...
"outDir": "../dist", // <-- changed
...
}
}
and
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
...
"options": {
...
"outputPath": "../dist", //<-- Changed
...
}
...
}
Back to index page of the project is about getting the initial rust API running.