refactor & validation
This commit is contained in:
parent
ec03c53208
commit
2d4af8fdbb
7 changed files with 58 additions and 14 deletions
|
@ -1,5 +1,4 @@
|
|||
mod migrate;
|
||||
mod schema;
|
||||
mod validate;
|
||||
|
||||
use clap::{Subcommand, ValueEnum};
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
mod client;
|
||||
mod server;
|
|
@ -1,7 +1,51 @@
|
|||
use crate::config::{client, server};
|
||||
use std::{env, fs, io::Read};
|
||||
use validator::{Validate, ValidationErrors};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[error("failed to validate the config file")]
|
||||
pub(crate) struct ConfigValidateError;
|
||||
pub(crate) enum ConfigValidateError {
|
||||
#[error(transparent)]
|
||||
Read(#[from] ReadConfigError),
|
||||
#[error(transparent)]
|
||||
InvalidConfig(#[from] ValidationErrors),
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub(crate) enum ReadConfigError {
|
||||
#[error(transparent)]
|
||||
ReadFile(#[from] std::io::Error),
|
||||
#[error("config/server.toml is not written in the TOML format")]
|
||||
InvalidServerTomlFormat(#[source] toml::de::Error),
|
||||
#[error("config/client.toml is not written in the TOML format")]
|
||||
InvalidClientTomlFormat(#[source] toml::de::Error),
|
||||
}
|
||||
|
||||
pub(super) fn run() -> Result<(), ConfigValidateError> {
|
||||
unimplemented!()
|
||||
read_server_toml()?.validate()?;
|
||||
read_client_toml()?.validate()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_server_toml() -> Result<server::Config, ReadConfigError> {
|
||||
const FILENAME: &str = "config/server.toml";
|
||||
|
||||
let cwd = env::current_dir()?;
|
||||
let mut file = fs::File::open(cwd.join(FILENAME))?;
|
||||
|
||||
let mut buffer = String::new();
|
||||
file.read_to_string(&mut buffer)?;
|
||||
|
||||
toml::from_str(&buffer).map_err(ReadConfigError::InvalidServerTomlFormat)
|
||||
}
|
||||
|
||||
fn read_client_toml() -> Result<client::Config, ReadConfigError> {
|
||||
const FILENAME: &str = "config/client.toml";
|
||||
|
||||
let cwd = env::current_dir()?;
|
||||
let mut file = fs::File::open(cwd.join(FILENAME))?;
|
||||
|
||||
let mut buffer = String::new();
|
||||
file.read_to_string(&mut buffer)?;
|
||||
|
||||
toml::from_str(&buffer).map_err(ReadConfigError::InvalidClientTomlFormat)
|
||||
}
|
||||
|
|
|
@ -9,12 +9,12 @@ use serde::{Deserialize, Serialize};
|
|||
use validator::{Validate, ValidationError};
|
||||
|
||||
#[derive(Deserialize, Serialize, Validate, Debug)]
|
||||
pub(super) struct Config {
|
||||
pub struct Config {
|
||||
appearance: Appearance,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Validate, Debug)]
|
||||
pub(super) struct Appearance {
|
||||
pub struct Appearance {
|
||||
/// Server-wide default light theme
|
||||
light_theme: Option<String>,
|
||||
/// Server-wide default light theme
|
2
src/config/mod.rs
Normal file
2
src/config/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod client;
|
||||
pub mod server;
|
|
@ -11,7 +11,7 @@ use validator::Validate;
|
|||
type Port = u16;
|
||||
|
||||
#[derive(Deserialize, Serialize, Validate, Debug)]
|
||||
pub(super) struct Config {
|
||||
pub struct Config {
|
||||
network: Network,
|
||||
database: Database,
|
||||
cache_server: CacheServer,
|
||||
|
@ -19,20 +19,20 @@ pub(super) struct Config {
|
|||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Validate, Debug)]
|
||||
pub(super) struct Network {
|
||||
pub struct Network {
|
||||
protocol: Option<HttpProtocol>,
|
||||
host: String,
|
||||
port: Port,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub(super) enum HttpProtocol {
|
||||
pub enum HttpProtocol {
|
||||
Https,
|
||||
Http,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Validate, Debug)]
|
||||
pub(super) struct Database {
|
||||
pub struct Database {
|
||||
host: String,
|
||||
port: Port,
|
||||
user: String,
|
||||
|
@ -41,7 +41,7 @@ pub(super) struct Database {
|
|||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Validate, Debug)]
|
||||
pub(super) struct CacheServer {
|
||||
pub struct CacheServer {
|
||||
host: String,
|
||||
port: Port,
|
||||
user: Option<String>,
|
||||
|
@ -51,7 +51,7 @@ pub(super) struct CacheServer {
|
|||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Validate, Debug)]
|
||||
pub(super) struct Id {
|
||||
pub struct Id {
|
||||
#[validate(range(min = 16, max = 24))]
|
||||
length: Option<u8>,
|
||||
fingerprint: Option<String>,
|
|
@ -1,4 +1,5 @@
|
|||
mod command;
|
||||
mod config;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
|
|
Loading…
Reference in a new issue