//! Videl configuration use std::{ path::{ PathBuf, }, error, fmt, io }; use tokio::fs; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Config { pub base_dir: PathBuf, } impl Default for Config { #[inline] fn default() -> Self { Self { base_dir: PathBuf::from("/tmp/videl"), } } } impl Config { /// Validate this config pub async fn validate(&self) -> Result<(), Error> { let exists = self.base_dir.exists(); if exists && !self.base_dir.is_dir() { return Err(Error::InvalidDestination); } else if !exists { fs::create_dir_all(&self.base_dir).await?; } Ok(()) } } #[derive(Debug)] pub enum Error { IO(io::Error), InvalidDestination, } impl error::Error for Error { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match &self { Self::IO(io) => Some(io), _ => None, } } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::IO(_) => write!(f, "i/o error"), Self::InvalidDestination => write!(f, "invalid base destination for videl databases"), } } } impl From for Error { #[inline] fn from(from: io::Error) -> Self { Self::IO(from) } }