diff --git a/src/api/mod.rs b/src/api/mod.rs index a87afb8..dc8ae20 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,5 +1,3 @@ -use std::collections::HashSet; - use serde::{Deserialize, Serialize}; mod project; @@ -47,24 +45,11 @@ where ret } -fn unique_tasks(tasks: Vec) -> Vec { - let mut seen_ids = HashSet::new(); - let mut unique_tasks = Vec::with_capacity(tasks.len()); - - for task in tasks { - if seen_ids.insert(task.id) { - unique_tasks.push(task); - } - } - - unique_tasks -} - pub struct ProjectID(pub isize); impl ProjectID { pub fn parse(api: &VikunjaAPI, project: &str) -> Option { - let project = project.trim_start_matches("#"); + let project = project.trim_start_matches('#'); if let Ok(num) = project.parse() { Some(Self(num)) @@ -95,29 +80,30 @@ impl VikunjaAPI { } fn get_request(&self, path: &str) -> String { - if let Some(cached) = self.cache.get(path) { - cached - } else { - let client = reqwest::blocking::Client::new(); + self.cache.get(path).map_or_else( + || { + let client = reqwest::blocking::Client::new(); - let ret = client - .get(&format!("{}/api/v1{}", self.host, path)) - .header("Authorization", format!("Bearer {}", self.token)) - .send() - .unwrap() - .text() - .unwrap(); + let ret = client + .get(format!("{}/api/v1{}", self.host, path)) + .header("Authorization", format!("Bearer {}", self.token)) + .send() + .unwrap() + .text() + .unwrap(); - self.cache.insert(path.to_string(), ret.clone()); - ret - } + self.cache.insert(path.to_string(), ret.clone()); + ret + }, + |cached| cached, + ) } - fn put_request(&self, path: &str, data: serde_json::Value) -> String { + fn put_request(&self, path: &str, data: &serde_json::Value) -> String { let client = reqwest::blocking::Client::new(); client - .put(&format!("{}/api/v1{}", self.host, path)) + .put(format!("{}/api/v1{}", self.host, path)) .header("Authorization", format!("Bearer {}", self.token)) .json(&data) .send() @@ -126,11 +112,11 @@ impl VikunjaAPI { .unwrap() } - fn post_request(&self, path: &str, data: serde_json::Value) -> String { + fn post_request(&self, path: &str, data: &serde_json::Value) -> String { let client = reqwest::blocking::Client::new(); client - .post(&format!("{}/api/v1{}", self.host, path)) + .post(format!("{}/api/v1{}", self.host, path)) .header("Authorization", format!("Bearer {}", self.token)) .json(&data) .send() @@ -180,7 +166,7 @@ impl VikunjaAPI { serde_json::from_str(&resp).unwrap() } - pub fn new_task(&self, title: &str, project: ProjectID) -> Task { + pub fn new_task(&self, title: &str, project: &ProjectID) -> Task { let id = project.0; let data = serde_json::json!({ @@ -194,14 +180,14 @@ impl VikunjaAPI { // labels // priority - let resp = self.put_request(&format!("/projects/{id}/tasks"), data); + let resp = self.put_request(&format!("/projects/{id}/tasks"), &data); serde_json::from_str(&resp).unwrap() } pub fn done_task(&self, task_id: isize) -> Task { let resp = self.post_request( &format!("/tasks/{task_id}"), - serde_json::json!({ + &serde_json::json!({ "done": true }), ); diff --git a/src/args.rs b/src/args.rs index 9739ce0..f5257fb 100644 --- a/src/args.rs +++ b/src/args.rs @@ -18,6 +18,8 @@ pub fn get_args() -> clap::ArgMatches { .name("prj") .about("Commands about projects") .subcommand(command!().name("ls").about("List projects")), + // todo : prj add + // todo : prj rm ) .subcommand( command!() @@ -35,6 +37,9 @@ pub fn get_args() -> clap::ArgMatches { .name("label") .about("Manage labels") .subcommand(command!().name("ls").about("List all labels")), + // todo : label new + // todo : label rm + // todo : label