mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2025-10-13 15:06:39 +00:00
moved TallyID str function to right module
This commit is contained in:
parent
4a9ff47dcc
commit
b13ae76bc5
@ -2,8 +2,46 @@ mod id_mapping;
|
|||||||
pub mod persistence;
|
pub mod persistence;
|
||||||
mod id_store;
|
mod id_store;
|
||||||
|
|
||||||
|
use heapless::String;
|
||||||
pub use id_mapping::{IDMapping, Name};
|
pub use id_mapping::{IDMapping, Name};
|
||||||
pub use id_store::{IDStore,AttendanceDay};
|
pub use id_store::{IDStore,AttendanceDay};
|
||||||
|
|
||||||
pub type TallyID = [u8; 6];
|
pub type TallyID = [u8; 6];
|
||||||
pub type Date = [u8; 10];
|
pub type Date = [u8; 10];
|
||||||
|
|
||||||
|
pub fn hex_string_to_tally_id(s: &str) -> Option<TallyID> {
|
||||||
|
let bytes = s.as_bytes();
|
||||||
|
if bytes.len() != 12 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut out: TallyID = [0;6];
|
||||||
|
for i in 0..6 {
|
||||||
|
let hi = hex_val(bytes[2 * i])?;
|
||||||
|
let lo = hex_val(bytes[2 * i + 1])?;
|
||||||
|
out[i] = (hi << 4) | lo;
|
||||||
|
}
|
||||||
|
Some(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tally_id_to_hex_string(bytes: TallyID) -> Option<String<12>> {
|
||||||
|
const HEX_CHARS: &[u8; 16] = b"0123456789abcdef";
|
||||||
|
let mut s: String<12> = String::new();
|
||||||
|
|
||||||
|
for &b in &bytes {
|
||||||
|
s.push(HEX_CHARS[(b >> 4) as usize] as char).ok()?;
|
||||||
|
s.push(HEX_CHARS[(b & 0x0F) as usize] as char).ok()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hex_val(b: u8) -> Option<u8> {
|
||||||
|
match b {
|
||||||
|
b'0'..=b'9' => Some(b - b'0'),
|
||||||
|
b'a'..=b'f' => Some(b - b'a' + 10),
|
||||||
|
b'A'..=b'F' => Some(b - b'A' + 10),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ use picoserve::{
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
store::{Name, TallyID},
|
store::{Name, hex_string_to_tally_id},
|
||||||
webserver::app::AppState,
|
webserver::{app::AppState, sse::IDEvents},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -16,30 +16,6 @@ pub struct NewMapping {
|
|||||||
name: Name,
|
name: Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hex_string_to_tally_id(s: &str) -> Option<TallyID> {
|
|
||||||
let bytes = s.as_bytes();
|
|
||||||
if bytes.len() != 24 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut out = [0u8; 12];
|
|
||||||
for i in 0..12 {
|
|
||||||
let hi = hex_val(bytes[2 * i])?;
|
|
||||||
let lo = hex_val(bytes[2 * i + 1])?;
|
|
||||||
out[i] = (hi << 4) | lo;
|
|
||||||
}
|
|
||||||
Some(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hex_val(b: u8) -> Option<u8> {
|
|
||||||
match b {
|
|
||||||
b'0'..=b'9' => Some(b - b'0'),
|
|
||||||
b'a'..=b'f' => Some(b - b'a' + 10),
|
|
||||||
b'A'..=b'F' => Some(b - b'A' + 10),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* #[get("/api/idevent")]
|
* #[get("/api/idevent")]
|
||||||
* #[get("/api/csv")]
|
* #[get("/api/csv")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user