diff --git a/src/hotspot.rs b/src/hotspot.rs index df1b1ff..24e0707 100644 --- a/src/hotspot.rs +++ b/src/hotspot.rs @@ -6,6 +6,8 @@ const CON_NAME: &str = "fwa-hotspot"; const PASSWORD: &str = "hunter22"; const IPV4_ADDRES: &str = "192.168.4.1/24"; +/// Create the connection in NM +/// Will fail if already exists async fn create_hotspot() -> Result<(), Box> { let mut cmd = Command::new("nmcli") .args(["device", "wifi", "hotspot"]) @@ -42,6 +44,7 @@ async fn create_hotspot() -> Result<(), Box> { Ok(()) } +/// Checks if the connection already exists async fn exists() -> Result> { let mut cmd = Command::new("nmcli") .args(["connection", "show"]) diff --git a/src/id_store.rs b/src/id_store.rs index b53452a..2ce4bf9 100644 --- a/src/id_store.rs +++ b/src/id_store.rs @@ -7,6 +7,7 @@ use std::{ fs, }; +/// Represents the ID that is stored on the Tally #[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)] pub struct TallyID(pub String); @@ -16,12 +17,14 @@ impl Display for TallyID { } } +/// Represents a single day that IDs can attend #[derive(Deserialize, Serialize)] pub struct AttendanceDay { date: String, ids: Vec, } +/// Stores all the days #[derive(Deserialize, Serialize)] pub struct IDStore { days: HashMap, @@ -34,17 +37,22 @@ impl IDStore { } } + /// Creats a new `IDStore` from a json file pub fn new_from_json(filepath: &str) -> Result> { let readed_string = fs::read_to_string(filepath)?; Ok(serde_json::from_str(&readed_string)?) } + /// Add a new id for the current day + /// Duplicates will get ignored pub fn add_id(&mut self, id: TallyID) { let day = self.get_current_day(); day.add_id(id); } + /// Get the `AttendanceDay` of the current day + /// Creates a new if not exists pub fn get_current_day(&mut self) -> &mut AttendanceDay { let current_day = get_day_str(); @@ -60,11 +68,14 @@ impl IDStore { self.days.get_mut(¤t_day.clone()).unwrap() } + /// Writes the store to a json file pub fn export_json(&self, filepath: &str) -> Result<(), Box> { // Serialize it to a JSON string and safe it in filepath file Ok(fs::write(filepath, serde_json::to_string(&self)?)?) } + /// Export the store to a csv file. + /// With days in the rows and IDs in the collum. pub fn export_csv(&self) -> Result> { let mut csv = String::new(); let seperator = ";"; @@ -88,7 +99,12 @@ impl IDStore { for user_id in user_ids.iter() { csv.push_str(&user_id.0.to_string()); for day in days.iter() { - let was_there: bool = self.days.get(day).ok_or("Failed to access day")?.ids.contains(user_id); + let was_there: bool = self + .days + .get(day) + .ok_or("Failed to access day")? + .ids + .contains(user_id); if was_there { csv.push_str(&format!("{}x", seperator)); diff --git a/src/parser.rs b/src/parser.rs index 43483db..e0a7b3c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,7 @@ use regex::Regex; +/// Parses the output of PM3 finds the read IDs +/// Example input: `[+] UID.... 3112B710` pub fn parse_line(line: &str) -> Option { let regex = Regex::new(r"(?m)^\[\+\] UID.... (.*)$").unwrap(); let result = regex.captures(line); diff --git a/src/pm3.rs b/src/pm3.rs index 052134a..cf9f62d 100644 --- a/src/pm3.rs +++ b/src/pm3.rs @@ -6,6 +6,9 @@ use std::process::{Command, Stdio}; use tokio::sync::mpsc; use tokio::time::{Duration, sleep}; +/// Runs the pm3 binary and monitors it's output +/// The pm3 binary is ether set in the env var PM3_BIN or found in the path +/// The ouput is parsed and send via the `tx` channel pub async fn run_pm3(tx: mpsc::Sender) -> Result<(), Box> { let pm3_path = match env::var("PM3_BIN") { Ok(path) => path, @@ -56,6 +59,7 @@ pub async fn run_pm3(tx: mpsc::Sender) -> Result<(), Box> { } } +/// Mocks the `run_pm3` command. Outputs the same ID every second. pub async fn pm3_mock(tx: mpsc::Sender) -> Result<(), Box> { #![allow(while_true)] while true {