diff --git a/src/id_store.rs b/src/id_store.rs index 33c875b..4a2359a 100644 --- a/src/id_store.rs +++ b/src/id_store.rs @@ -2,19 +2,10 @@ use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, HashSet}, error::Error, - fmt::Display, }; use tokio::fs; -/// Represents the ID that is stored on the Tally -#[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)] -pub struct TallyID(pub String); - -impl Display for TallyID { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} +use crate::tally_id::TallyID; /// Represents a single day that IDs can attend #[derive(Deserialize, Serialize)] diff --git a/src/main.rs b/src/main.rs index ad953ec..efca073 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,12 @@ use buzzer::GPIOBuzzer; -use id_store::{IDStore, TallyID}; +use id_store::IDStore; use led::Led; use log::{LevelFilter, debug, error, info, warn}; use pm3::run_pm3; use rppal::pwm::Channel; use simplelog::{ConfigBuilder, SimpleLogger}; use std::{env, error::Error, sync::Arc}; +use tally_id::TallyID; use tokio::{ fs, sync::{Mutex, mpsc}, @@ -19,6 +20,7 @@ mod id_store; mod led; mod parser; mod pm3; +mod tally_id; mod webserver; const STORE_PATH: &str = "./data.json"; @@ -92,7 +94,7 @@ async fn main() -> Result<(), Box> { let channel_store = store.clone(); tokio::spawn(async move { while let Some(tally_id_string) = rx.recv().await { - let tally_id = id_store::TallyID(tally_id_string); + let tally_id = TallyID(tally_id_string); if hotspot_ids.contains(&tally_id) { info!("Enableing hotspot"); diff --git a/src/tally_id.rs b/src/tally_id.rs new file mode 100644 index 0000000..5ad6a47 --- /dev/null +++ b/src/tally_id.rs @@ -0,0 +1,45 @@ +use std::{ + cmp::Ordering, + fmt::Display, + hash::{Hash, Hasher}, +}; + +use serde::{Deserialize, Serialize}; + +/// Represents the ID that is stored on the Tally +/// Is case-insensitive. +/// While any string can be a ID, most IDs are going to be a hex string. +#[derive(Deserialize, Serialize, Clone)] +pub struct TallyID(pub String); + +impl PartialEq for TallyID { + fn eq(&self, other: &Self) -> bool { + self.0.eq_ignore_ascii_case(&other.0) + } +} + +impl Hash for TallyID { + fn hash(&self, state: &mut H) { + self.0.to_uppercase().hash(state); + } +} + +impl Eq for TallyID {} + +impl Ord for TallyID { + fn cmp(&self, other: &Self) -> Ordering { + self.0.to_uppercase().cmp(&other.0.to_uppercase()) + } +} + +impl PartialOrd for TallyID { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Display for TallyID { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0.to_uppercase()) + } +}