You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
videl/src/config.rs

81 lines
1.4 KiB

//! Videl configuration
use std::{
path::{
PathBuf,
},
error,
fmt,
io,
num::NonZeroUsize,
};
use tokio::fs;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Config
{
/// The base dir of the database
pub base_dir: PathBuf,
/// Maximum number of concurrent operations
pub limit: Option<NonZeroUsize>,
}
impl Default for Config
{
#[inline]
fn default() -> Self
{
Self {
base_dir: PathBuf::from("/tmp/videl"),
limit: Some(unsafe{NonZeroUsize::new_unchecked(4096)}),
}
}
}
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<io::Error> for Error
{
#[inline] fn from(from: io::Error) -> Self
{
Self::IO(from)
}
}