diff --git a/src/command/config.rs b/src/command/config.rs index 942ec9b..6ed0e91 100644 --- a/src/command/config.rs +++ b/src/command/config.rs @@ -1,5 +1,4 @@ mod migrate; -mod schema; mod validate; use clap::{Subcommand, ValueEnum}; diff --git a/src/command/config/schema.rs b/src/command/config/schema.rs deleted file mode 100644 index ea1769a..0000000 --- a/src/command/config/schema.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod client; -mod server; diff --git a/src/command/config/validate.rs b/src/command/config/validate.rs index be96c6e..7bc4668 100644 --- a/src/command/config/validate.rs +++ b/src/command/config/validate.rs @@ -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 { + 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 { + 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) } diff --git a/src/command/config/schema/client.rs b/src/config/client.rs similarity index 97% rename from src/command/config/schema/client.rs rename to src/config/client.rs index 856bb5f..8d9a088 100644 --- a/src/command/config/schema/client.rs +++ b/src/config/client.rs @@ -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, /// Server-wide default light theme diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..c07f47e --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,2 @@ +pub mod client; +pub mod server; diff --git a/src/command/config/schema/server.rs b/src/config/server.rs similarity index 87% rename from src/command/config/schema/server.rs rename to src/config/server.rs index a7c1244..6356a89 100644 --- a/src/command/config/schema/server.rs +++ b/src/config/server.rs @@ -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, 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, @@ -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, fingerprint: Option, diff --git a/src/main.rs b/src/main.rs index e0b82be..150a143 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod command; +mod config; use clap::{Parser, Subcommand};