41 lines
1.4 KiB
Rust
41 lines
1.4 KiB
Rust
use crate::{CurrentTaskInfo, Task};
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
|
|
pub struct Request {
|
|
pub current_task_info: Option<CurrentTaskInfo>,
|
|
pub tasks: Vec<Task>,
|
|
pub finished_tasks_file_path: PathBuf,
|
|
pub tasks_file_path: PathBuf,
|
|
pub current_task_info_file_path: PathBuf,
|
|
}
|
|
|
|
pub fn execute(mut req: Request) {
|
|
match req.current_task_info.take() {
|
|
None => {
|
|
panic!("You can use the finish subcommand only when you have an active task")
|
|
}
|
|
Some(info) => {
|
|
let mut finished_tasks: Vec<Task> = std::fs::File::open(&req.finished_tasks_file_path)
|
|
.map(|file| serde_json::from_reader(file).unwrap())
|
|
.unwrap_or_default();
|
|
|
|
let task = req.tasks.remove(info.task_idx - 1);
|
|
finished_tasks.push(task);
|
|
|
|
let mut file = std::fs::File::create(req.tasks_file_path).unwrap();
|
|
file.write_all(&serde_json::to_vec(&req.tasks).unwrap())
|
|
.unwrap();
|
|
|
|
let mut file = std::fs::File::create(req.finished_tasks_file_path).unwrap();
|
|
file.write_all(&serde_json::to_vec(&finished_tasks).unwrap())
|
|
.unwrap();
|
|
|
|
let mut file = std::fs::File::create(req.current_task_info_file_path).unwrap();
|
|
file.write_all(&serde_json::to_vec(&req.current_task_info).unwrap())
|
|
.unwrap();
|
|
|
|
println!("finished");
|
|
}
|
|
}
|
|
}
|