intial commit
This commit is contained in:
64
src/conf.rs
Normal file
64
src/conf.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
fs::File,
|
||||
io::{self, Read, Seek},
|
||||
};
|
||||
|
||||
use libc::gid_t;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Either<T, U> {
|
||||
Left(T),
|
||||
Right(U),
|
||||
}
|
||||
|
||||
impl<T: Debug, U: Debug> Debug for Either<T, U> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Left(l) => l.fmt(f),
|
||||
Self::Right(r) => r.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Group = Either<String, gid_t>;
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Unix {
|
||||
groups: Vec<Group>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Config {
|
||||
unix: Unix,
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum ConfigParseError {
|
||||
#[error(transparent)]
|
||||
Io(#[from] io::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
Parse(#[from] toml::de::Error),
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// # Errors
|
||||
///
|
||||
/// For IO errors reading the file or parsing errors.
|
||||
pub fn from_toml_file(f: &mut File) -> Result<Self, ConfigParseError> {
|
||||
let mut buf = Vec::new();
|
||||
if let Ok(len) = f.stream_len() {
|
||||
// There's no way a config file passes machine's memory limitations
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
buf.reserve_exact(len as usize);
|
||||
}
|
||||
f.read_to_end(&mut buf)?;
|
||||
|
||||
Ok(toml::from_slice(&buf)?)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user