mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2026-04-30 18:49:09 +00:00
redesigned dir structure for rust 2018 style guide. made (untested) rtc funtion
This commit is contained in:
@@ -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<TallyID, Name>,
|
||||
id_map: BTreeMap<TallyID, Name>,
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<TallyID>,
|
||||
}
|
||||
|
||||
/// Stores all the days
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct IDStore {
|
||||
days: HashMap<String, AttendanceDay>,
|
||||
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<Self> {
|
||||
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<String> {
|
||||
let mut csv = String::new();
|
||||
let seperator = ";";
|
||||
let mut user_ids: HashSet<TallyID> = HashSet::new();
|
||||
|
||||
for day in self.days.values() {
|
||||
for id in day.ids.iter() {
|
||||
user_ids.insert(id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let mut user_ids: Vec<TallyID> = user_ids.into_iter().collect();
|
||||
user_ids.sort();
|
||||
|
||||
let mut days: Vec<String> = 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<Date, AttendanceDay>,
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user