Boilerplate

This commit is contained in:
Antoine Gersant 2024-10-08 22:19:57 -07:00
parent 316f5c0219
commit 7f39d8e8b7
3 changed files with 32 additions and 8 deletions

View file

@ -19,7 +19,7 @@ pub use user::*;
use super::auth;
#[derive(Default)]
#[derive(Debug, Clone, Default)]
pub struct Config {
pub reindex_every_n_seconds: Option<u64>,
pub album_art_pattern: Option<Regex>,
@ -67,6 +67,18 @@ impl TryFrom<storage::Config> for Config {
}
}
impl From<Config> for storage::Config {
fn from(c: Config) -> Self {
Self {
reindex_every_n_seconds: c.reindex_every_n_seconds,
album_art_pattern: c.album_art_pattern.map(|p| p.as_str().to_owned()),
mount_dirs: c.mount_dirs.into_iter().map(|d| d.into()).collect(),
ddns_url: c.ddns_url.map(|u| u.to_string()),
users: c.users.into_iter().map(|(_, u)| u.into()).collect(),
}
}
}
#[derive(Clone)]
pub struct Manager {
config_file_path: PathBuf,
@ -220,6 +232,7 @@ mod test {
users: vec![],
};
ctx.config_manager.apply(new_config.clone()).await.unwrap();
assert_eq!(new_config, ctx.config_manager.config.read().await.clone(),);
let effective_config: Config = ctx.config_manager.config.read().await.clone().into();
assert_eq!(new_config, effective_config,);
}
}

View file

@ -28,6 +28,15 @@ impl TryFrom<storage::MountDir> for MountDir {
}
}
impl From<MountDir> for storage::MountDir {
fn from(m: MountDir) -> Self {
Self {
source: m.source,
name: m.name,
}
}
}
impl Config {
pub fn set_mounts(&mut self, mount_dirs: Vec<storage::MountDir>) {
self.mount_dirs = mount_dirs

View file

@ -11,6 +11,12 @@ pub struct User {
pub hashed_password: String,
}
impl User {
pub fn is_admin(&self) -> bool {
self.admin == Some(true)
}
}
impl TryFrom<storage::User> for User {
type Error = Error;
@ -30,12 +36,6 @@ impl TryFrom<storage::User> for User {
}
}
impl User {
pub fn is_admin(&self) -> bool {
self.admin == Some(true)
}
}
impl From<User> for storage::User {
fn from(user: User) -> Self {
Self {
@ -136,6 +136,7 @@ mod test {
const TEST_USERNAME: &str = "Walter";
const TEST_PASSWORD: &str = "super_secret!";
#[test]
fn adds_password_hashes() {
let user_in = storage::User {
name: TEST_USERNAME.to_owned(),
@ -152,6 +153,7 @@ mod test {
assert!(user_out.hashed_password.is_some());
}
#[test]
fn preserves_password_hashes() {
let user_in = storage::User {
name: TEST_USERNAME.to_owned(),