From 49027fed9970039b3c50cea7d250ee3a31d453f3 Mon Sep 17 00:00:00 2001 From: Philipp_EndevourOS Date: Mon, 28 Jul 2025 17:25:39 +0200 Subject: [PATCH] redesigned dir structure for rust 2018 style guide. made (untested) rtc funtion --- Cargo.lock | 1 + Cargo.toml | 4 +- src/bin/store/id_mapping.rs | 31 ----- src/bin/store/id_store.rs | 71 ---------- src/bin/store/mod.rs | 8 -- src/drivers.rs | 2 + src/{hardware => drivers}/fram.rs | 0 src/{hardware => drivers}/mock.rs | 0 src/drivers/nfc_reader.rs | 29 ++++ src/drivers/rtc.rs | 1 + src/feedback.rs | 3 +- src/hardware/mod.rs | 26 ---- src/hardware/rtc.rs | 0 src/init.rs | 3 + src/{bin/init/mod.rs => init/hardware.rs} | 59 ++++++-- src/{bin => }/init/network.rs | 0 src/{bin => }/init/wifi.rs | 0 src/{bin => }/main.rs | 47 +++---- src/store/id_mapping.rs | 59 +------- src/store/id_store.rs | 162 +++++++--------------- src/store/mod.rs | 7 +- src/tally_id.rs | 45 ------ src/{bin => }/webserver/mod.rs | 0 23 files changed, 168 insertions(+), 390 deletions(-) delete mode 100644 src/bin/store/id_mapping.rs delete mode 100644 src/bin/store/id_store.rs delete mode 100644 src/bin/store/mod.rs create mode 100644 src/drivers.rs rename src/{hardware => drivers}/fram.rs (100%) rename src/{hardware => drivers}/mock.rs (100%) create mode 100644 src/drivers/nfc_reader.rs create mode 100644 src/drivers/rtc.rs delete mode 100644 src/hardware/mod.rs delete mode 100644 src/hardware/rtc.rs create mode 100644 src/init.rs rename src/{bin/init/mod.rs => init/hardware.rs} (64%) rename src/{bin => }/init/network.rs (100%) rename src/{bin => }/init/wifi.rs (100%) rename src/{bin => }/main.rs (58%) delete mode 100644 src/tally_id.rs rename src/{bin => }/webserver/mod.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index c3eae4d..d45527d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -285,6 +285,7 @@ dependencies = [ "cfg-if", "chrono", "embedded-hal 1.0.0", + "embedded-hal-async", "maybe-async-cfg", "paste", ] diff --git a/Cargo.toml b/Cargo.toml index c68c934..c5b8974 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2024" [[bin]] name = "fw-anwesenheit" -path = "./src/bin/main.rs" +path = "./src/main.rs" test = false doctest = false bench = false @@ -60,7 +60,7 @@ edge-nal-embassy = { version = "0.6.0", features = ["log"] } picoserve = { version = "0.16.0", features = ["embassy", "log"] } embassy-sync = { version = "0.7.0", features = ["log"] } -ds3231 = "0.3.0" +ds3231 = { version = "0.3.0", features = ["async"] } ws2812-spi = "0.5.1" diff --git a/src/bin/store/id_mapping.rs b/src/bin/store/id_mapping.rs deleted file mode 100644 index de25995..0000000 --- a/src/bin/store/id_mapping.rs +++ /dev/null @@ -1,31 +0,0 @@ -extern crate alloc; - -use super::TallyID; -use alloc::collections::BTreeMap; -use alloc::string::String; - -pub struct Name { - pub first: String, - pub last: String, -} - -pub struct IDMapping { - id_map: BTreeMap, -} - -impl IDMapping { - pub fn new() -> Self { - IDMapping { - id_map: BTreeMap::new(), - } - } - - pub fn map(&self, id: &TallyID) -> Option<&Name> { - self.id_map.get(id) - } - - pub fn add_mapping(&mut self, id: TallyID, name: Name) { - self.id_map.insert(id, name); - } -} - diff --git a/src/bin/store/id_store.rs b/src/bin/store/id_store.rs deleted file mode 100644 index bc82b35..0000000 --- a/src/bin/store/id_store.rs +++ /dev/null @@ -1,71 +0,0 @@ -extern crate alloc; - -use super::Date; -use super::IDMapping; -use super::TallyID; -use alloc::collections::BTreeMap; -use alloc::vec::Vec; - -pub struct AttendanceDay { - date: Date, - ids: Vec, -} - -impl AttendanceDay { - fn new(date: Date) -> Self { - Self { - date, - ids: Vec::new(), - } - } - - // Add an ID to the day. - // Returns false if ID was already present - fn add_id(&mut self, id: TallyID) -> bool { - if self.ids.contains(&id) { - return false; - } - self.ids.push(id); - true - } -} - -pub struct IDStore { - days: BTreeMap, - mapping: IDMapping, -} - -impl IDStore { - pub fn new() -> Self { - IDStore { - days: BTreeMap::new(), - mapping: IDMapping::new(), - } - } - - pub fn new_from_storage() -> Self { - // TODO: implement - todo!() - } - - /// Add a new id for the current day - /// Returns false if ID is already present at the current day. - pub fn add_id(&mut self, id: TallyID) -> bool { - self.get_current_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: Date = 1; - - if self.days.contains_key(¤t_day) { - return self.days.get_mut(¤t_day).unwrap(); - } - - self.days - .insert(current_day, AttendanceDay::new(current_day)); - - self.days.get_mut(¤t_day.clone()).unwrap() - } -} diff --git a/src/bin/store/mod.rs b/src/bin/store/mod.rs deleted file mode 100644 index d72bfee..0000000 --- a/src/bin/store/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -mod id_mapping; -mod id_store; - -pub use id_mapping::{IDMapping, Name}; -pub use id_store::IDStore; - -pub type TallyID = [u8; 8]; -pub type Date = u64; diff --git a/src/drivers.rs b/src/drivers.rs new file mode 100644 index 0000000..7fa4aa5 --- /dev/null +++ b/src/drivers.rs @@ -0,0 +1,2 @@ +pub mod nfc_reader; +pub mod rtc; \ No newline at end of file diff --git a/src/hardware/fram.rs b/src/drivers/fram.rs similarity index 100% rename from src/hardware/fram.rs rename to src/drivers/fram.rs diff --git a/src/hardware/mock.rs b/src/drivers/mock.rs similarity index 100% rename from src/hardware/mock.rs rename to src/drivers/mock.rs diff --git a/src/drivers/nfc_reader.rs b/src/drivers/nfc_reader.rs new file mode 100644 index 0000000..0fbeb59 --- /dev/null +++ b/src/drivers/nfc_reader.rs @@ -0,0 +1,29 @@ +use embassy_time::{Duration, Timer}; +use esp_hal::{uart::Uart, Async}; +use log::{ debug, info }; + +use crate::TallyPublisher; + + +#[embassy_executor::task] +pub async fn rfid_reader_task(mut uart_device: Uart<'static, Async>, chan: TallyPublisher) { + let mut uart_buffer = [0u8; 64]; + + loop { + debug!("Looking for NFC..."); + match uart_device.read_async(&mut uart_buffer).await { + Ok(n) => { + let mut hex_str = heapless::String::<64>::new(); + for byte in &uart_buffer[..n] { + core::fmt::Write::write_fmt(&mut hex_str, format_args!("{:02X} ", byte)).ok(); + } + info!("Read {n} bytes from UART: {hex_str}"); + chan.publish([1, 0, 2, 5, 0, 8, 12, 15]).await; + } + Err(e) => { + log::error!("Error reading from UART: {e}"); + } + } + Timer::after(Duration::from_millis(200)).await; + } +} \ No newline at end of file diff --git a/src/drivers/rtc.rs b/src/drivers/rtc.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/drivers/rtc.rs @@ -0,0 +1 @@ + diff --git a/src/feedback.rs b/src/feedback.rs index c381098..4e85256 100644 --- a/src/feedback.rs +++ b/src/feedback.rs @@ -55,8 +55,7 @@ impl Feedback { let (buzzer_result, _) = join!(buzzer_handle, led_handle); - buzzer_result.unwrap_or_else(|err| { - error!("Failed to buzz: {err}"); + buzzer_result.unwrap_or_else(|err| { error!("Failed to buzz: {err}"); }); let _ = self.led_to_status(); diff --git a/src/hardware/mod.rs b/src/hardware/mod.rs deleted file mode 100644 index 698e74c..0000000 --- a/src/hardware/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -use anyhow::Result; -use std::time::Duration; - -mod gpio_buzzer; -mod hotspot; -mod mock; -mod spi_led; - -pub use gpio_buzzer::GPIOBuzzer; -pub use mock::{MockBuzzer, MockHotspot, MockLed}; -pub use spi_led::SpiLed; - -pub trait StatusLed { - fn turn_off(&mut self) -> Result<()>; - - fn turn_on(&mut self, color: rgb::RGB8) -> Result<()>; -} - -pub trait Buzzer { - fn modulated_tone( - &mut self, - frequency_hz: f64, - duration: Duration, - ) -> impl Future> + std::marker::Send; -} - diff --git a/src/hardware/rtc.rs b/src/hardware/rtc.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/init.rs b/src/init.rs new file mode 100644 index 0000000..ef04822 --- /dev/null +++ b/src/init.rs @@ -0,0 +1,3 @@ +pub mod hardware; +pub mod network; +pub mod wifi; diff --git a/src/bin/init/mod.rs b/src/init/hardware.rs similarity index 64% rename from src/bin/init/mod.rs rename to src/init/hardware.rs index 302a36e..06e8c41 100644 --- a/src/bin/init/mod.rs +++ b/src/init/hardware.rs @@ -1,6 +1,13 @@ +use core::slice::RChunks; + +use ds3231::{DS3231, Alarm1Config}; use embassy_executor::Spawner; -use embassy_net::Stack; -use esp_hal::peripherals::{self, GPIO0, GPIO1, GPIO3, GPIO4, GPIO5, GPIO6, GPIO7, GPIO22, GPIO23, I2C0, UART1}; +use embassy_net::{driver, Stack}; +use embassy_sync::mutex::Mutex; +use esp_hal::config; +use esp_hal::gpio::{Input, Pull}; +use esp_hal::i2c::master::Config; +use esp_hal::peripherals::{self, GPIO0, GPIO1, GPIO3, GPIO4, GPIO5, GPIO6, GPIO7, GPIO21, GPIO22, GPIO23, I2C0, UART1}; use esp_hal::time::Rate; use esp_hal::{ Async, @@ -13,8 +20,10 @@ use esp_hal::{ use esp_println::logger::init_logger; use log::error; -mod network; -mod wifi; +use crate::init::wifi; +use crate::init::network; + +const RTC_ADDRESS: u8 = 0x57; #[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { @@ -23,7 +32,7 @@ fn panic(_: &core::panic::PanicInfo) -> ! { esp_bootloader_esp_idf::esp_app_desc!(); -pub async fn hardware_init(spawner: &mut Spawner) -> (Uart<'static, Async>, Stack<'static>) { +pub async fn hardware_init(spawner: &mut Spawner) -> (Uart<'static, Async>, Stack<'static>, I2c<'static, Async>, GPIO21<'static>) { let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); let peripherals = esp_hal::init(config); @@ -48,9 +57,12 @@ pub async fn hardware_init(spawner: &mut Spawner) -> (Uart<'static, Async>, Stac let i2c_device = setup_i2c(peripherals.I2C0, peripherals.GPIO22, peripherals.GPIO23); + //RTC Interrupt pin + let sqw_pin = peripherals.GPIO21; + //TODO change to get I2C device back / maybe init for each protocol - (uart_device, stack) + (uart_device, stack, i2c_device, sqw_pin) } // Initialize the level shifter for the NFC reader and LED (output-enable (OE) input is low, all outputs are placed in the high-impedance (Hi-Z) state) @@ -80,17 +92,40 @@ fn setup_i2c( sda: GPIO22<'static>, scl: GPIO23<'static>, ) -> I2c<'static, Async> { - let config = esp_hal::i2c::master::Config::default().with_frequency(Rate::from_khz(400)); - let i2c_device = I2c::new(i2c0, config); - match i2c_device { - Ok(block) => block.with_sda(sda).with_scl(scl).into_async(), + + let config = Config::default().with_frequency(Rate::from_khz(400)); + let i2c = match I2c::new(i2c0, config) { + Ok(i2c) => i2c.with_sda(sda).with_scl(scl).into_async(), Err(e) => { - error!("Failed to initialize I2C: {e}"); + error!("Failed to initialize I2C: {:?}", e); panic!(); } - } + }; + i2c } +pub async fn rtc_init_iterrupt(sqw_pin: GPIO21<'static>) -> Input<'static> { + let config = esp_hal::gpio::InputConfig::default().with_pull(Pull::Up); + let mut sqw_interrupt = Input::new(sqw_pin, config); + sqw_interrupt +} + +pub async fn rtc_config(i2c: I2c<'static, Async>) -> DS3231> { + let mut rtc: DS3231> = DS3231::new(i2c, RTC_ADDRESS); + let daily_alarm = Alarm1Config::AtTime { + hours: 9, + minutes: 30, + seconds: 0, + is_pm: None, // 24-hour mode + }; + if let Err(e) = rtc.set_alarm1(&daily_alarm).await { + error!("Failed to configure RTC: {:?}", e); + panic!(); + } + rtc +} + + fn setup_spi_led() { } diff --git a/src/bin/init/network.rs b/src/init/network.rs similarity index 100% rename from src/bin/init/network.rs rename to src/init/network.rs diff --git a/src/bin/init/wifi.rs b/src/init/wifi.rs similarity index 100% rename from src/bin/init/wifi.rs rename to src/init/wifi.rs diff --git a/src/bin/main.rs b/src/main.rs similarity index 58% rename from src/bin/main.rs rename to src/main.rs index 40b2ce3..60baffc 100644 --- a/src/bin/main.rs +++ b/src/main.rs @@ -6,15 +6,14 @@ use embassy_executor::Spawner; use embassy_net::Stack; use embassy_sync::{ - blocking_mutex::raw::NoopRawMutex, - pubsub::{ + blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}, mutex::Mutex, pubsub::{ PubSubChannel, Publisher, WaitResult::{Lagged, Message}, - }, + } }; use embassy_time::{Duration, Timer}; use esp_alloc::psram_allocator; -use esp_hal::Async; +use esp_hal::{i2c, peripherals, Async}; use esp_hal::uart::Uart; use log::{debug, info}; use static_cell::make_static; @@ -22,17 +21,18 @@ use static_cell::make_static; use crate::{store::TallyID, webserver::start_webserver}; mod init; +mod drivers; mod store; mod webserver; type TallyChannel = PubSubChannel; type TallyPublisher = Publisher<'static, NoopRawMutex, TallyID, 8, 2, 1>; -static mut UTC_TIME: u64 = 0; +static UTC_TIME: Mutex = Mutex::new(0); #[esp_hal_embassy::main] async fn main(mut spawner: Spawner) { - let (uart_device, stack) = init::hardware_init(&mut spawner).await; + let (uart_device, stack, i2c, sqw_pin) = init::hardware::hardware_init(&mut spawner).await; wait_for_stack_up(stack).await; @@ -42,7 +42,8 @@ async fn main(mut spawner: Spawner) { let publisher = chan.publisher().unwrap(); - spawner.must_spawn(rfid_reader_task(uart_device, publisher)); + spawner.must_spawn(drivers::nfc_reader::rfid_reader_task(uart_device, publisher)); + spawner.must_spawn(rtc_task(i2c, sqw_pin)); let mut sub = chan.subscriber().unwrap(); loop { @@ -68,24 +69,22 @@ async fn wait_for_stack_up(stack: Stack<'static>) { } #[embassy_executor::task] -async fn rfid_reader_task(mut uart_device: Uart<'static, Async>, chan: TallyPublisher) { - let mut uart_buffer = [0u8; 64]; +async fn rtc_task( + mut i2c: i2c::master::I2c<'static, Async>, + sqw_pin: peripherals::GPIO21<'static>, +) { + let mut rtc_interrupt = init::hardware::rtc_init_iterrupt(sqw_pin).await; + let mut rtc = init::hardware::rtc_config(i2c).await; loop { - debug!("Looking for NFC..."); - match uart_device.read_async(&mut uart_buffer).await { - Ok(n) => { - let mut hex_str = heapless::String::<64>::new(); - for byte in &uart_buffer[..n] { - core::fmt::Write::write_fmt(&mut hex_str, format_args!("{:02X} ", byte)).ok(); - } - info!("Read {n} bytes from UART: {hex_str}"); - chan.publish([1, 0, 2, 5, 0, 8, 12, 15]).await; - } - Err(e) => { - log::error!("Error reading from UART: {e}"); - } + rtc_interrupt.wait_for_falling_edge().await; + debug!("RTC interrupt triggered"); + if let Ok(datetime) = rtc.datetime().await { + let mut utc_time = UTC_TIME.lock().await; + *utc_time = datetime.and_utc().timestamp() as u64; + info!("RTC updated UTC_TIME: {}", *utc_time); + } else { + info!("Failed to read RTC datetime"); } - Timer::after(Duration::from_millis(200)).await; } -} +} \ No newline at end of file diff --git a/src/store/id_mapping.rs b/src/store/id_mapping.rs index 3ffd591..de25995 100644 --- a/src/store/id_mapping.rs +++ b/src/store/id_mapping.rs @@ -1,22 +1,22 @@ -use crate::tally_id::TallyID; -use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +extern crate alloc; + +use super::TallyID; +use alloc::collections::BTreeMap; +use alloc::string::String; -#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)] pub struct Name { pub first: String, pub last: String, } -#[derive(Deserialize, Serialize, Clone)] pub struct IDMapping { - id_map: HashMap, + id_map: BTreeMap, } impl IDMapping { pub fn new() -> Self { IDMapping { - id_map: HashMap::new(), + id_map: BTreeMap::new(), } } @@ -29,48 +29,3 @@ impl IDMapping { } } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn basic() { - let mut map = IDMapping::new(); - let id1 = TallyID("A2Fb44".to_owned()); - let name1 = Name { - first: "Max".to_owned(), - last: "Mustermann".to_owned(), - }; - - map.add_mapping(id1.clone(), name1.clone()); - - let res = map.map(&id1); - - assert_eq!(res, Some(&name1)); - } - - #[test] - fn multiple() { - let mut map = IDMapping::new(); - let id1 = TallyID("A2Fb44".to_owned()); - let name1 = Name { - first: "Max".to_owned(), - last: "Mustermann".to_owned(), - }; - - let id2 = TallyID("7D3DF5B5".to_owned()); - let name2 = Name { - first: "First".to_owned(), - last: "Last".to_owned(), - }; - - map.add_mapping(id1.clone(), name1.clone()); - map.add_mapping(id2.clone(), name2.clone()); - - let res = map.map(&id1); - assert_eq!(res, Some(&name1)); - - let res = map.map(&id2); - assert_eq!(res, Some(&name2)); - } -} diff --git a/src/store/id_store.rs b/src/store/id_store.rs index cd5b70a..bc82b35 100644 --- a/src/store/id_store.rs +++ b/src/store/id_store.rs @@ -1,123 +1,20 @@ -use anyhow::{Result, anyhow}; -use serde::{Deserialize, Serialize}; -use std::collections::{HashMap, HashSet}; -use tokio::fs; +extern crate alloc; -use crate::{store::IDMapping, tally_id::TallyID}; +use super::Date; +use super::IDMapping; +use super::TallyID; +use alloc::collections::BTreeMap; +use alloc::vec::Vec; -/// Represents a single day that IDs can attend -#[derive(Deserialize, Serialize)] pub struct AttendanceDay { - date: String, + date: Date, ids: Vec, } -/// Stores all the days -#[derive(Deserialize, Serialize)] -pub struct IDStore { - days: HashMap, - pub mapping: IDMapping, -} - -impl IDStore { - pub fn new() -> Self { - IDStore { - days: HashMap::new(), - mapping: IDMapping::new(), - } - } - - /// Creats a new `IDStore` from a json file - pub async fn new_from_json(filepath: &str) -> Result { - let read_string = fs::read_to_string(filepath).await?; - Ok(serde_json::from_str(&read_string)?) - } - - /// Add a new id for the current day - /// Returns false if ID is already present at the current day. - pub fn add_id(&mut self, id: TallyID) -> bool { - self.get_current_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(); - - if self.days.contains_key(¤t_day) { - return self.days.get_mut(¤t_day).unwrap(); - } - - self.days.insert( - current_day.clone(), - AttendanceDay::new(¤t_day.clone()), - ); - - self.days.get_mut(¤t_day.clone()).unwrap() - } - - /// Writes the store to a json file - pub async fn export_json(&self, filepath: &str) -> Result<()> { - fs::write(filepath, serde_json::to_string(&self)?).await?; - Ok(()) - } - - /// 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 = ";"; - let mut user_ids: HashSet = HashSet::new(); - - for day in self.days.values() { - for id in day.ids.iter() { - user_ids.insert(id.clone()); - } - } - - let mut user_ids: Vec = user_ids.into_iter().collect(); - user_ids.sort(); - - let mut days: Vec = self.days.keys().cloned().collect(); - days.sort(); - - let header = days.join(seperator); - csv.push_str(&format!( - "ID{seperator}Nachname{seperator}Vorname{seperator}{header}\n" - )); - - for user_id in user_ids.iter() { - let id = &user_id.0.to_string(); - let name = self.mapping.map(user_id); - - let firstname = name.map(|e| e.first.clone()).unwrap_or("".to_owned()); - let lastname = name.map(|e| e.last.clone()).unwrap_or("".to_owned()); - - csv.push_str(&format!("{id}{seperator}{lastname}{seperator}{firstname}")); - for day in days.iter() { - let was_there: bool = self - .days - .get(day) - .ok_or(anyhow!("Failed to access day"))? - .ids - .contains(user_id); - - if was_there { - csv.push_str(&format!("{seperator}x")); - } else { - csv.push_str(seperator); - } - } - csv.push('\n'); - } - Ok(csv) - } -} - impl AttendanceDay { - fn new(day: &str) -> Self { + fn new(date: Date) -> Self { Self { - date: day.to_owned(), + date, ids: Vec::new(), } } @@ -133,7 +30,42 @@ impl AttendanceDay { } } -fn get_day_str() -> String { - let now = chrono::offset::Local::now(); - now.format("%Y-%m-%d").to_string() +pub struct IDStore { + days: BTreeMap, + mapping: IDMapping, +} + +impl IDStore { + pub fn new() -> Self { + IDStore { + days: BTreeMap::new(), + mapping: IDMapping::new(), + } + } + + pub fn new_from_storage() -> Self { + // TODO: implement + todo!() + } + + /// Add a new id for the current day + /// Returns false if ID is already present at the current day. + pub fn add_id(&mut self, id: TallyID) -> bool { + self.get_current_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: Date = 1; + + if self.days.contains_key(¤t_day) { + return self.days.get_mut(¤t_day).unwrap(); + } + + self.days + .insert(current_day, AttendanceDay::new(current_day)); + + self.days.get_mut(¤t_day.clone()).unwrap() + } } diff --git a/src/store/mod.rs b/src/store/mod.rs index 89e2394..d72bfee 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -1,5 +1,8 @@ -mod id_store; mod id_mapping; +mod id_store; +pub use id_mapping::{IDMapping, Name}; pub use id_store::IDStore; -pub use id_mapping::{IDMapping,Name}; + +pub type TallyID = [u8; 8]; +pub type Date = u64; diff --git a/src/tally_id.rs b/src/tally_id.rs deleted file mode 100644 index 5ad6a47..0000000 --- a/src/tally_id.rs +++ /dev/null @@ -1,45 +0,0 @@ -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()) - } -} diff --git a/src/bin/webserver/mod.rs b/src/webserver/mod.rs similarity index 100% rename from src/bin/webserver/mod.rs rename to src/webserver/mod.rs