added some documentation

This commit is contained in:
Niklas Kapelle 2025-04-26 19:22:13 +02:00
parent 6496f671d9
commit 0195bb3666
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
4 changed files with 26 additions and 1 deletions

View File

@ -6,6 +6,8 @@ const CON_NAME: &str = "fwa-hotspot";
const PASSWORD: &str = "hunter22"; const PASSWORD: &str = "hunter22";
const IPV4_ADDRES: &str = "192.168.4.1/24"; 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<dyn Error>> { async fn create_hotspot() -> Result<(), Box<dyn Error>> {
let mut cmd = Command::new("nmcli") let mut cmd = Command::new("nmcli")
.args(["device", "wifi", "hotspot"]) .args(["device", "wifi", "hotspot"])
@ -42,6 +44,7 @@ async fn create_hotspot() -> Result<(), Box<dyn Error>> {
Ok(()) Ok(())
} }
/// Checks if the connection already exists
async fn exists() -> Result<bool, Box<dyn Error>> { async fn exists() -> Result<bool, Box<dyn Error>> {
let mut cmd = Command::new("nmcli") let mut cmd = Command::new("nmcli")
.args(["connection", "show"]) .args(["connection", "show"])

View File

@ -7,6 +7,7 @@ use std::{
fs, fs,
}; };
/// Represents the ID that is stored on the Tally
#[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)] #[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)]
pub struct TallyID(pub String); pub struct TallyID(pub String);
@ -16,12 +17,14 @@ impl Display for TallyID {
} }
} }
/// Represents a single day that IDs can attend
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct AttendanceDay { pub struct AttendanceDay {
date: String, date: String,
ids: Vec<TallyID>, ids: Vec<TallyID>,
} }
/// Stores all the days
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct IDStore { pub struct IDStore {
days: HashMap<String, AttendanceDay>, days: HashMap<String, AttendanceDay>,
@ -34,17 +37,22 @@ impl IDStore {
} }
} }
/// Creats a new `IDStore` from a json file
pub fn new_from_json(filepath: &str) -> Result<Self, Box<dyn Error>> { pub fn new_from_json(filepath: &str) -> Result<Self, Box<dyn Error>> {
let readed_string = fs::read_to_string(filepath)?; let readed_string = fs::read_to_string(filepath)?;
Ok(serde_json::from_str(&readed_string)?) 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) { pub fn add_id(&mut self, id: TallyID) {
let day = self.get_current_day(); let day = self.get_current_day();
day.add_id(id); 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 { pub fn get_current_day(&mut self) -> &mut AttendanceDay {
let current_day = get_day_str(); let current_day = get_day_str();
@ -60,11 +68,14 @@ impl IDStore {
self.days.get_mut(&current_day.clone()).unwrap() self.days.get_mut(&current_day.clone()).unwrap()
} }
/// Writes the store to a json file
pub fn export_json(&self, filepath: &str) -> Result<(), Box<dyn Error>> { pub fn export_json(&self, filepath: &str) -> Result<(), Box<dyn Error>> {
// Serialize it to a JSON string and safe it in filepath file // Serialize it to a JSON string and safe it in filepath file
Ok(fs::write(filepath, serde_json::to_string(&self)?)?) 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<String, Box<dyn Error>> { pub fn export_csv(&self) -> Result<String, Box<dyn Error>> {
let mut csv = String::new(); let mut csv = String::new();
let seperator = ";"; let seperator = ";";
@ -88,7 +99,12 @@ impl IDStore {
for user_id in user_ids.iter() { for user_id in user_ids.iter() {
csv.push_str(&user_id.0.to_string()); csv.push_str(&user_id.0.to_string());
for day in days.iter() { 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 { if was_there {
csv.push_str(&format!("{}x", seperator)); csv.push_str(&format!("{}x", seperator));

View File

@ -1,5 +1,7 @@
use regex::Regex; use regex::Regex;
/// Parses the output of PM3 finds the read IDs
/// Example input: `[+] UID.... 3112B710`
pub fn parse_line(line: &str) -> Option<String> { pub fn parse_line(line: &str) -> Option<String> {
let regex = Regex::new(r"(?m)^\[\+\] UID.... (.*)$").unwrap(); let regex = Regex::new(r"(?m)^\[\+\] UID.... (.*)$").unwrap();
let result = regex.captures(line); let result = regex.captures(line);

View File

@ -6,6 +6,9 @@ use std::process::{Command, Stdio};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::time::{Duration, sleep}; 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<String>) -> Result<(), Box<dyn Error>> { pub async fn run_pm3(tx: mpsc::Sender<String>) -> Result<(), Box<dyn Error>> {
let pm3_path = match env::var("PM3_BIN") { let pm3_path = match env::var("PM3_BIN") {
Ok(path) => path, Ok(path) => path,
@ -56,6 +59,7 @@ pub async fn run_pm3(tx: mpsc::Sender<String>) -> Result<(), Box<dyn Error>> {
} }
} }
/// Mocks the `run_pm3` command. Outputs the same ID every second.
pub async fn pm3_mock(tx: mpsc::Sender<String>) -> Result<(), Box<dyn Error>> { pub async fn pm3_mock(tx: mpsc::Sender<String>) -> Result<(), Box<dyn Error>> {
#![allow(while_true)] #![allow(while_true)]
while true { while true {