From cc8f3c919d5f6700553fcc1cdd83fc4ae91b5164 Mon Sep 17 00:00:00 2001 From: Junko Date: Tue, 21 Nov 2023 16:03:51 +0100 Subject: [PATCH] better docs --- README.org | 95 ++++++++++++++++++++++++++++++++------------------ TODO.org | 36 ++++++++++--------- src/main.rs | 99 ++++++++++++++++++++++++++++++----------------------- 3 files changed, 138 insertions(+), 92 deletions(-) diff --git a/README.org b/README.org index 30424c0..e36593e 100644 --- a/README.org +++ b/README.org @@ -111,41 +111,8 @@ fn main() { *** Functions **** Main commands +***** Sync #+begin_src rust :tangle src/main.rs :comments link -fn set_member(config_path: String, member: String) -> Result<(), &'static str> { - let config = get_config(&config_path); - if config == Value::Null { - return Err("Config not found. Stopping"); - } - - let system: System = get_system(&config_path); - - let mut flag = false; - for mem in &system.members { - if mem.name.to_lowercase() == member.to_lowercase() { - flag = true; - } - } - - if flag { - println!("Member {member} found"); - get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system); - - - Ok(()) - } else { - Err("Member {member} not found") - } - -} - -/* -fn set_empty(config_path: String) { - let config = load_json(format!("{}/config.json", config_path)); - let system = get_system(&config_path); -} -*/ - fn sync(config_path: String) -> Result<(), String>{ // Get config let config = get_config(&config_path); @@ -202,7 +169,46 @@ fn sync(config_path: String) -> Result<(), String>{ } #+end_src +***** Set member +#+begin_src rust :tangle src/main.rs :comments link + +/* +TODO fn set_empty(config_path: String) { +TODO let config = load_json(format!("{}/config.json", config_path)); +TODO let system = get_system(&config_path); +TODO } +*/ + +fn set_member(config_path: String, member: String) -> Result<(), &'static str> { + let config = get_config(&config_path); + if config == Value::Null { + return Err("Config not found. Stopping"); + } + + let system: System = get_system(&config_path); + + let mut flag = false; + for mem in &system.members { + if mem.name.to_lowercase() == member.to_lowercase() { + flag = true; + } + } + + if flag { + println!("Member {member} found"); + get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system); + + + Ok(()) + } else { + Err("Member {member} not found") + } + +} +#+end_src + **** Pluralkit +***** Get system #+begin_src rust :tangle src/main.rs :comments link fn pk_get_system(key: &str) -> Value { let url = format!("{}/systems/@me", PK_URL); @@ -219,12 +225,16 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec { return datas; } +#+end_src +***** Get fronters +#+begin_src rust :tangle src/main.rs :comments link fn pk_get_fronters(key: &str, sysid: &str) -> Vec { let url = format!("{}/systems/{}/fronters", PK_URL, sysid); let res = http_get_request(url,key); let data: Value = serde_json::from_str(&res.unwrap()).unwrap(); + println!("{:?}", data); let memberdata = &data["members"].as_array(); let mut members: Vec = Vec::new(); @@ -240,6 +250,7 @@ fn pk_get_fronters(key: &str, sysid: &str) -> Vec { #+end_src **** Simplyplural +***** Get user ID #+begin_src rust :tangle src/main.rs :comments link fn sp_get_userid(key: &str) -> String { let url = format!("{}/me", SP_URL); @@ -248,7 +259,10 @@ fn sp_get_userid(key: &str) -> String { let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap(); return json_res["id"].as_str().unwrap().to_string(); } +#+end_src +***** Get members ID +#+begin_src rust :tangle src/main.rs :comments link fn sp_get_memberids(key: &str, system_id: &str) -> HashMap { let url = format!("{}/members/{}", SP_URL, system_id); @@ -262,7 +276,10 @@ fn sp_get_memberids(key: &str, system_id: &str) -> HashMap { return sp_memberdata; } +#+end_src +***** Get ID from member +#+begin_src rust :tangle src/main.rs :comments link fn get_sp_id(mem: &Member, ids: &HashMap) -> String { let mut member_id = String::new(); @@ -275,7 +292,10 @@ fn get_sp_id(mem: &Member, ids: &HashMap) -> String { return member_id; } +#+end_src +***** Get fronters +#+begin_src rust :tangle src/main.rs :comments link fn sp_get_fronters(key: &str, sys: &System) -> Vec { let url = format!("{}/fronters", SP_URL); @@ -299,6 +319,7 @@ fn sp_get_fronters(key: &str, sys: &System) -> Vec { #+end_src **** Utilities +***** Load JSON #+begin_src rust :tangle src/main.rs :comments link fn load_json(path: String) -> Value { if Path::new(&path).exists() { @@ -309,7 +330,10 @@ fn load_json(path: String) -> Value { return Value::Null; } } +#+end_src +***** Get config json +#+begin_src rust :tangle src/main.rs :comments link fn get_config(config_path: &str) -> Value { let path = format!("{}/config.json", config_path); if Path::new(config_path).exists() { @@ -339,7 +363,10 @@ fn get_config(config_path: &str) -> Value { return Value::Null; } } +#+end_src +***** Get system json +#+begin_src rust :tangle src/main.rs :comments link fn get_system(config_path: &str) -> System { let path = format!("{}/system.json", config_path); diff --git a/TODO.org b/TODO.org index 31422bf..fe99e20 100644 --- a/TODO.org +++ b/TODO.org @@ -8,34 +8,38 @@ :PROPERTIES: :COOKIE_DATA: recursive :END: -*** Milestone 1.0 [3/11][27%] +*** Milestone 1.0 [4/16][25%] **** Main functions [1/9][11%] ***** Finished Add `sync` command ***** InProgress Add `set` command [0/2] ****** HighPriority [SET] Add empty ****** InProgress [SET] Add set -***** HighPriority Add `get` command +***** LowPriority Add `get` command ***** LowPriority Add `Add` command [0/2] ****** LowPriority [ADD] Add empty ****** LowPriority [ADD] Add set ***** LowPriority Add `setgroup` command -**** Utils [2/2][100%] +**** Utils [3/7][42%] ***** Json loading [2/2] ****** Finished Add new function to get the config create empty in path if not exists ****** Finished `Get system` if Value::Null sync and load json -***** InProgress Get current front +***** Finished Get current front +***** InProgress Compare if sent members are currently fronting and keep them otherwise add to fronting array +***** HighPriority Check for mismatch in fronting between pluralkit and simplyplural +***** LowPriority Get fronters handles the front file +***** LowPriority Rofi stuff ** Kanban -| Backlog | LowPriority | HighPriority | InProgress | Finished | -|---------+-------------------------+-------------------+-------------------------+--------------------------------| -| | [[/home/alicia/git/pluralshit/TODO.org::Add `Add` command \[0/2\]][Add `Add` command {0/2}]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add empty][{SET} Add empty]] | [[/home/alicia/git/pluralshit/TODO.org::Add `set` command \[0/2\]][Add `set` command {0/2}]] | [[/home/alicia/git/pluralshit/TODO.org::Add `sync` command][Add `sync` command]] | -| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add empty][{ADD} Add empty]] | [[/home/alicia/git/pluralshit/TODO.org::Add `get` command][Add `get` command]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add set][{SET} Add set]] | [[/home/alicia/git/pluralshit/TODO.org::Add new function to get the config create empty in path if not exists][Add new function to get the co]] | -| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add set][{ADD} Add set]] | | | [[/home/alicia/git/pluralshit/TODO.org::`Get system` if Value::Null sync and load json][`Get system` if Value::Null sy]] | -| | [[/home/alicia/git/pluralshit/TODO.org::Add `setgroup` command][Add `setgroup` command]] | | | | -| | | | | | -| | | | | | -| | | | | | -| | | | | | -| | | | | | -| | | | | | +| Backlog | LowPriority | HighPriority | InProgress | Finished | +|---------+--------------------------------+--------------------------------+--------------------------------+--------------------------------| +| | [[/home/alicia/git/pluralshit/TODO.org::Add `get` command][Add `get` command]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add empty][{SET} Add empty]] | [[/home/alicia/git/pluralshit/TODO.org::Add `set` command \[0/2\]][Add `set` command {0/2}]] | [[/home/alicia/git/pluralshit/TODO.org::Add `sync` command][Add `sync` command]] | +| | [[/home/alicia/git/pluralshit/TODO.org::Add `Add` command \[0/2\]][Add `Add` command {0/2}]] | [[/home/alicia/git/pluralshit/TODO.org::Check for mismatch in fronting between pluralkit and simplyplural][Check for mismatch in fronting]] | [[/home/alicia/git/pluralshit/TODO.org::\[SET\] Add set][{SET} Add set]] | [[/home/alicia/git/pluralshit/TODO.org::Add new function to get the config create empty in path if not exists][Add new function to get the co]] | +| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add empty][{ADD} Add empty]] | | [[/home/alicia/git/pluralshit/TODO.org::Compare if sent members are currently fronting and keep them otherwise add to fronting array][Compare if sent members are cu]] | [[/home/alicia/git/pluralshit/TODO.org::`Get system` if Value::Null sync and load json][`Get system` if Value::Null sy]] | +| | [[/home/alicia/git/pluralshit/TODO.org::\[ADD\] Add set][{ADD} Add set]] | | | [[/home/alicia/git/pluralshit/TODO.org::Get current front][Get current front]] | +| | [[/home/alicia/git/pluralshit/TODO.org::Add `setgroup` command][Add `setgroup` command]] | | | | +| | [[/home/alicia/git/pluralshit/TODO.org::Get fronters handles the front file][Get fronters handles the front]] | | | | +| | [[/home/alicia/git/pluralshit/TODO.org::Rofi stuff][Rofi stuff]] | | | | +| | | | | | +| | | | | | +| | | | | | #+TBLFM: @1='(kanban-headers $#)::@2$1..@>$>='(kanban-zero @# $# nil (list (buffer-file-name))) diff --git a/src/main.rs b/src/main.rs index cc544e0..7b68da0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,41 +73,7 @@ fn main() { } // Main:1 ends here -// [[file:../README.org::*Main commands][Main commands:1]] -fn set_member(config_path: String, member: String) -> Result<(), &'static str> { - let config = get_config(&config_path); - if config == Value::Null { - return Err("Config not found. Stopping"); - } - - let system: System = get_system(&config_path); - - let mut flag = false; - for mem in &system.members { - if mem.name.to_lowercase() == member.to_lowercase() { - flag = true; - } - } - - if flag { - println!("Member {member} found"); - get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system); - - - Ok(()) - } else { - Err("Member {member} not found") - } - -} - -/* -fn set_empty(config_path: String) { - let config = load_json(format!("{}/config.json", config_path)); - let system = get_system(&config_path); -} -*/ - +// [[file:../README.org::*Sync][Sync:1]] fn sync(config_path: String) -> Result<(), String>{ // Get config let config = get_config(&config_path); @@ -162,9 +128,45 @@ fn sync(config_path: String) -> Result<(), String>{ Ok(()) } -// Main commands:1 ends here +// Sync:1 ends here -// [[file:../README.org::*Pluralkit][Pluralkit:1]] +// [[file:../README.org::*Set member][Set member:1]] +/* +TODO fn set_empty(config_path: String) { +TODO let config = load_json(format!("{}/config.json", config_path)); +TODO let system = get_system(&config_path); +TODO } +*/ + +fn set_member(config_path: String, member: String) -> Result<(), &'static str> { + let config = get_config(&config_path); + if config == Value::Null { + return Err("Config not found. Stopping"); + } + + let system: System = get_system(&config_path); + + let mut flag = false; + for mem in &system.members { + if mem.name.to_lowercase() == member.to_lowercase() { + flag = true; + } + } + + if flag { + println!("Member {member} found"); + get_fronters(&config["pk_key"].as_str().unwrap(), &system.pk_userid, &config["sp_key"].as_str().unwrap(), &system); + + + Ok(()) + } else { + Err("Member {member} not found") + } + +} +// Set member:1 ends here + +// [[file:../README.org::*Get system][Get system:1]] fn pk_get_system(key: &str) -> Value { let url = format!("{}/systems/@me", PK_URL); @@ -180,12 +182,15 @@ fn pk_get_members(key: &str, sysid: &str) -> Vec { return datas; } +// Get system:1 ends here +// [[file:../README.org::*Get fronters][Get fronters:1]] fn pk_get_fronters(key: &str, sysid: &str) -> Vec { let url = format!("{}/systems/{}/fronters", PK_URL, sysid); let res = http_get_request(url,key); let data: Value = serde_json::from_str(&res.unwrap()).unwrap(); + println!("{:?}", data); let memberdata = &data["members"].as_array(); let mut members: Vec = Vec::new(); @@ -198,9 +203,9 @@ fn pk_get_fronters(key: &str, sysid: &str) -> Vec { return members; } -// Pluralkit:1 ends here +// Get fronters:1 ends here -// [[file:../README.org::*Simplyplural][Simplyplural:1]] +// [[file:../README.org::*Get user ID][Get user ID:1]] fn sp_get_userid(key: &str) -> String { let url = format!("{}/me", SP_URL); @@ -208,7 +213,9 @@ fn sp_get_userid(key: &str) -> String { let json_res : Value = serde_json::from_str(&res.unwrap()).unwrap(); return json_res["id"].as_str().unwrap().to_string(); } +// Get user ID:1 ends here +// [[file:../README.org::*Get members ID][Get members ID:1]] fn sp_get_memberids(key: &str, system_id: &str) -> HashMap { let url = format!("{}/members/{}", SP_URL, system_id); @@ -222,7 +229,9 @@ fn sp_get_memberids(key: &str, system_id: &str) -> HashMap { return sp_memberdata; } +// Get members ID:1 ends here +// [[file:../README.org::*Get ID from member][Get ID from member:1]] fn get_sp_id(mem: &Member, ids: &HashMap) -> String { let mut member_id = String::new(); @@ -235,7 +244,9 @@ fn get_sp_id(mem: &Member, ids: &HashMap) -> String { return member_id; } +// Get ID from member:1 ends here +// [[file:../README.org::*Get fronters][Get fronters:1]] fn sp_get_fronters(key: &str, sys: &System) -> Vec { let url = format!("{}/fronters", SP_URL); @@ -256,9 +267,9 @@ fn sp_get_fronters(key: &str, sys: &System) -> Vec { } return members; } -// Simplyplural:1 ends here +// Get fronters:1 ends here -// [[file:../README.org::*Utilities][Utilities:1]] +// [[file:../README.org::*Load JSON][Load JSON:1]] fn load_json(path: String) -> Value { if Path::new(&path).exists() { let config_data = fs::read_to_string(&path).expect("File not found"); @@ -268,7 +279,9 @@ fn load_json(path: String) -> Value { return Value::Null; } } +// Load JSON:1 ends here +// [[file:../README.org::*Get config json][Get config json:1]] fn get_config(config_path: &str) -> Value { let path = format!("{}/config.json", config_path); if Path::new(config_path).exists() { @@ -298,7 +311,9 @@ fn get_config(config_path: &str) -> Value { return Value::Null; } } +// Get config json:1 ends here +// [[file:../README.org::*Get system json][Get system json:1]] fn get_system(config_path: &str) -> System { let path = format!("{}/system.json", config_path); @@ -325,7 +340,7 @@ fn get_fronters(pk_key: &str, pk_sysid: &str, sp_key: &str, sys: &System) -> Has return fronters; } -// Utilities:1 ends here +// Get system json:1 ends here // [[file:../README.org::*Http Request handler][Http Request handler:1]] #[tokio::main]