From 78e7d45614ea8e806751d0fc7ce389ec43699d3b Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 16 Dec 2020 13:30:04 +0100 Subject: [PATCH] Users loading, noot noot! --- examples/users.toml | 11 +++++++++++ src/db/user.rs | 20 ++++++++++++++++++-- src/main.rs | 15 ++++++++++----- 3 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 examples/users.toml diff --git a/examples/users.toml b/examples/users.toml new file mode 100644 index 0000000..46c4a67 --- /dev/null +++ b/examples/users.toml @@ -0,0 +1,11 @@ +[Testuser] +# Define them in roles.toml as well +roles = [] + +# If two or more users want to use the same machine at once the higher prio +# wins +priority = 0 + +# You can add whatever random data you want. +# It will get stored in the `kv` field in UserData. +noot = "noot!" diff --git a/src/db/user.rs b/src/db/user.rs index fd78cb3..23914f4 100644 --- a/src/db/user.rs +++ b/src/db/user.rs @@ -3,9 +3,14 @@ //! 2. "I have this here user, what are their roles (and other associated data)" use serde::{Serialize, Deserialize}; use std::fmt; +use std::fs; +use std::iter::FromIterator; +use std::path::Path; use crate::db::access::RoleIdentifier; use std::collections::HashMap; +use crate::error::Result; + mod internal; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -80,8 +85,8 @@ pub struct UserData { pub priority: u64, /// Additional data storage - #[serde(flatten)] - kv: HashMap, Box<[u8]>>, + #[serde(flatten, skip_serializing_if = "HashMap::is_empty")] + kv: HashMap, } impl UserData { @@ -101,6 +106,17 @@ const fn default_priority() -> u64 { 0 } +pub fn load_file>(path: P) -> Result> { + let f = fs::read(path)?; + let mut map: HashMap = toml::from_slice(&f)?; + + Ok(HashMap::from_iter(map.drain().map(|(uid, user_data)| + ( uid.clone() + , User::new(UserId::new(uid, None, None), user_data) + ) + ))) +} + #[cfg(test_DISABLED)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index 3e768cc..d9cf6dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,18 +122,23 @@ fn main() { // Returning a `Result` from `main` allows us to use the `?` shorthand. // In the case of an Err it will be printed using `fmt::Debug` fn maybe(matches: clap::ArgMatches, log: Arc) -> Result<(), Error> { - // If no `config` option is given use a preset default. - let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml"); - let config = config::read(&PathBuf::from_str(configpath).unwrap())?; - debug!(log, "Loaded Config: {:?}", config); if matches.is_present("dump") { error!(log, "Dumping is currently not implemented"); Ok(()) } else if matches.is_present("load") { - error!(log, "Loading is currently not implemented"); + let mut dir = PathBuf::from(matches.value_of_os("load").unwrap()); + dir.push("users.toml"); + let map = db::user::load_file(&dir); + debug!(log, "Loaded users: {:?}", map); + dir.pop(); Ok(()) } else { + // If no `config` option is given use a preset default. + let configpath = matches.value_of("config").unwrap_or("/etc/bffh/config.toml"); + let config = config::read(&PathBuf::from_str(configpath).unwrap())?; + debug!(log, "Loaded Config: {:?}", config); + let ex = Executor::new(); let mqtt = AsyncClient::new(config.mqtt_url.clone())?;