refactor & validation

This commit is contained in:
naskya 2024-06-20 05:08:33 +09:00
parent ec03c53208
commit 2d4af8fdbb
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
7 changed files with 58 additions and 14 deletions

View file

@ -1,5 +1,4 @@
mod migrate; mod migrate;
mod schema;
mod validate; mod validate;
use clap::{Subcommand, ValueEnum}; use clap::{Subcommand, ValueEnum};

View file

@ -1,2 +0,0 @@
mod client;
mod server;

View file

@ -1,7 +1,51 @@
use crate::config::{client, server};
use std::{env, fs, io::Read};
use validator::{Validate, ValidationErrors};
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
#[error("failed to validate the config file")] pub(crate) enum ConfigValidateError {
pub(crate) struct 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> { 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)
} }

View file

@ -9,12 +9,12 @@ use serde::{Deserialize, Serialize};
use validator::{Validate, ValidationError}; use validator::{Validate, ValidationError};
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub(super) struct Config { pub struct Config {
appearance: Appearance, appearance: Appearance,
} }
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub(super) struct Appearance { pub struct Appearance {
/// Server-wide default light theme /// Server-wide default light theme
light_theme: Option<String>, light_theme: Option<String>,
/// Server-wide default light theme /// Server-wide default light theme

2
src/config/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod client;
pub mod server;

View file

@ -11,7 +11,7 @@ use validator::Validate;
type Port = u16; type Port = u16;
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub(super) struct Config { pub struct Config {
network: Network, network: Network,
database: Database, database: Database,
cache_server: CacheServer, cache_server: CacheServer,
@ -19,20 +19,20 @@ pub(super) struct Config {
} }
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub(super) struct Network { pub struct Network {
protocol: Option<HttpProtocol>, protocol: Option<HttpProtocol>,
host: String, host: String,
port: Port, port: Port,
} }
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
pub(super) enum HttpProtocol { pub enum HttpProtocol {
Https, Https,
Http, Http,
} }
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub(super) struct Database { pub struct Database {
host: String, host: String,
port: Port, port: Port,
user: String, user: String,
@ -41,7 +41,7 @@ pub(super) struct Database {
} }
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub(super) struct CacheServer { pub struct CacheServer {
host: String, host: String,
port: Port, port: Port,
user: Option<String>, user: Option<String>,
@ -51,7 +51,7 @@ pub(super) struct CacheServer {
} }
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub(super) struct Id { pub struct Id {
#[validate(range(min = 16, max = 24))] #[validate(range(min = 16, max = 24))]
length: Option<u8>, length: Option<u8>,
fingerprint: Option<String>, fingerprint: Option<String>,

View file

@ -1,4 +1,5 @@
mod command; mod command;
mod config;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};