implemented IDStore

This commit is contained in:
2025-04-12 14:36:33 +02:00
parent 32f3124374
commit c7dbbc1b43
6 changed files with 387 additions and 5 deletions

View File

@@ -1 +1,67 @@
use std::collections::HashMap;
#[derive(PartialEq, Eq)]
struct TellyID (String);
struct AttendanceDay {
date: String,
ids: Vec<TellyID>,
}
struct IDStore {
days: HashMap<String,AttendanceDay>
}
impl IDStore {
fn new() -> Self {
IDStore{
days: HashMap::new(),
}
}
fn add_id(&mut self, id: TellyID){
let day = self.get_current_day();
day.add_id(id);
self.clean_map();
}
fn get_current_day(&mut self) -> &mut AttendanceDay {
let current_day = get_day_str();
if self.days.contains_key(&current_day) {
return self.days.get_mut(&current_day).unwrap();
}
self.days.insert(current_day.clone(), AttendanceDay::new(&current_day.clone()));
self.days.get_mut(&current_day.clone()).unwrap()
}
fn clean_map(&mut self){
todo!()
}
}
impl AttendanceDay {
fn new(day: &str) -> Self{
Self{
date: day.to_owned(),
ids: Vec::new(),
}
}
fn add_id(&mut self, id: TellyID){
if self.ids.contains(&id) {
return
}
self.ids.push(id);
}
}
fn get_day_str() -> String {
let now = chrono::offset::Local::now();
now.format("%Y-%m-%d").to_string()
}

View File

@@ -1,7 +1,9 @@
use pm3::run_pm3;
mod parser;
mod pm3;
mod id_store;
fn main() {
pm3::RunPm3().unwrap();
run_pm3().unwrap();
}

View File

@@ -1,6 +1,6 @@
use regex::Regex;
pub fn ParseLine(line: &str) -> Option<String> {
pub fn parse_line(line: &str) -> Option<String> {
let regex = Regex::new(r"(?m)^\[\+\] UID.... (.*)$").unwrap();
let result = regex.captures(line);

View File

@@ -2,7 +2,7 @@ use std::error::Error;
use std::process::{Command, Stdio};
use std::io::{self, BufRead};
pub fn RunPm3() -> Result<(), Box<dyn Error>> {
pub fn run_pm3() -> Result<(), Box<dyn Error>> {
let mut cmd = Command::new("stdbuf")
.arg("-oL")
.arg("pm3")
@@ -17,7 +17,7 @@ pub fn RunPm3() -> Result<(), Box<dyn Error>> {
for line_result in reader.lines() {
match line_result {
Ok(line) => {
let parse_result = super::parser::ParseLine(&line);
let parse_result = super::parser::parse_line(&line);
if let Some(uid) = parse_result {
println!("UID: {}",uid);
}
@@ -30,5 +30,9 @@ pub fn RunPm3() -> Result<(), Box<dyn Error>> {
let status = cmd.wait().expect("Failed to wait on child");
Ok(())
if status.success() {
Ok(())
}else {
Err("pm3 had non zero exit code".into())
}
}