63 lines
1.5 KiB
Rust
63 lines
1.5 KiB
Rust
use crate::{CurrentTaskInfo, Task};
|
|
|
|
use std::cmp::Ordering;
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(clap::Args)]
|
|
pub struct Args {
|
|
idx: usize,
|
|
#[clap(subcommand)]
|
|
priority: Priority,
|
|
}
|
|
|
|
#[derive(clap::Subcommand)]
|
|
pub enum Priority {
|
|
Before { idx: usize },
|
|
After { idx: usize },
|
|
}
|
|
|
|
pub struct Request {
|
|
pub args: Args,
|
|
pub tasks: Vec<Task>,
|
|
pub current_task_info: Option<CurrentTaskInfo>,
|
|
pub tasks_file_path: PathBuf,
|
|
}
|
|
|
|
pub fn execute(mut req: Request) {
|
|
if req.current_task_info.is_some() {
|
|
panic!("You can change priority only when you don't have an active task, yet");
|
|
}
|
|
|
|
let target_idx = req.args.idx;
|
|
if target_idx == 0 || target_idx > req.tasks.len() {
|
|
panic!("invalid index");
|
|
}
|
|
|
|
let idx = match req.args.priority {
|
|
Priority::Before { idx } | Priority::After { idx } => match target_idx.cmp(&idx) {
|
|
Ordering::Equal => return,
|
|
Ordering::Less => idx - 1,
|
|
Ordering::Greater => idx,
|
|
},
|
|
};
|
|
if idx == 0 || idx > req.tasks.len() {
|
|
panic!("invalid index");
|
|
}
|
|
|
|
let target = req.tasks.remove(target_idx - 1);
|
|
|
|
match req.args.priority {
|
|
Priority::Before { .. } => {
|
|
req.tasks.insert(idx - 1, target);
|
|
}
|
|
Priority::After { .. } => {
|
|
req.tasks.insert(idx, target);
|
|
}
|
|
}
|
|
|
|
let mut file = std::fs::File::create(&req.tasks_file_path).unwrap();
|
|
file.write_all(&serde_json::to_vec(&req.tasks).unwrap())
|
|
.unwrap();
|
|
}
|