Browse Source

WIP: refactor

develop-refactor
chodak166 4 months ago
parent
commit
21f3b09591
  1. 17
      app/src/app.rs
  2. 44
      app/src/config.rs
  3. 39
      lib/src/presentation/cli/defaults.rs

17
app/src/app.rs

@ -38,22 +38,23 @@ impl Application {
pub async fn run(self) -> Result<()> { pub async fn run(self) -> Result<()> {
match self.command { match self.command {
Command::Server(_) => { Command::Server(_) => {
commands::server::run(self.config.server, Self::wait_for_shutdown_signal()).await; let config = self.config.server.expect("Server config not set");
commands::server::run(config, Self::wait_for_shutdown_signal()).await;
} }
Command::Decode(_) => { Command::Decode(_) => {
commands::decode::run(self.config.decoder).await; let config = self.config.decoder.expect("Decoder config not set");
commands::decode::run(config).await;
} }
Command::Encode(_) => { Command::Encode(_) => {
let config = self.config.encoder.expect("Encoder config not set");
let repo = self.container.create_dict_repo("demo_pl").await?; let repo = self.container.create_dict_repo("demo_pl").await?;
commands::encode::run(self.config.encoder, repo.as_ref()).await; commands::encode::run(config, repo.as_ref()).await;
} }
Command::ImportDict(_) => { Command::ImportDict(_) => {
let importer = self let config = self.config.import_dict.expect("ImportDict config not set");
.container let importer = self.container.create_dict_importer(&config.name).await?;
.create_dict_importer(&self.config.import_dict.name) commands::import_dict::run(config, importer).await?;
.await?;
commands::import_dict::run(self.config.import_dict, importer).await?;
} }
} }
Ok(()) Ok(())

44
app/src/config.rs

@ -2,15 +2,19 @@ use anyhow::{Context, Result};
use config::{Config, Environment, File}; use config::{Config, Environment, File};
use serde::Deserialize; use serde::Deserialize;
use applib::cli::{Command, GlobalArgs, defaults::set_defaults}; use applib::cli::{Command, GlobalArgs, defaults::set_command_defaults};
use applib::config::*; use applib::config::*;
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct AppConfig { pub struct AppConfig {
pub server: ServerConfig, #[serde(default)]
pub decoder: DecoderConfig, pub server: Option<ServerConfig>,
pub encoder: EncoderConfig, #[serde(default)]
pub import_dict: ImportDictConfig, pub decoder: Option<DecoderConfig>,
#[serde(default)]
pub encoder: Option<EncoderConfig>,
#[serde(default)]
pub import_dict: Option<ImportDictConfig>,
pub log_level: String, pub log_level: String,
} }
@ -18,8 +22,8 @@ impl AppConfig {
pub fn build(args: &GlobalArgs, command: &Command) -> Result<Self> { pub fn build(args: &GlobalArgs, command: &Command) -> Result<Self> {
let mut builder = Config::builder(); let mut builder = Config::builder();
// Defaults // Command-specific defaults
builder = set_defaults(builder)?; builder = set_command_defaults(builder, command)?;
// File Layer // File Layer
let config_path = &args.config; let config_path = &args.config;
@ -31,10 +35,29 @@ impl AppConfig {
builder = builder.add_source(Environment::with_prefix("APP").separator("_")); builder = builder.add_source(Environment::with_prefix("APP").separator("_"));
// CLI Overrides Layer // CLI Overrides Layer
builder = apply_cli_overrides(builder, args, command)?;
builder
.build()
.context("Failed to build configuration layers")?
.try_deserialize()
.context("Failed to deserialize Config")
}
}
fn apply_cli_overrides(
builder: config::ConfigBuilder<config::builder::DefaultState>,
args: &GlobalArgs,
command: &Command,
) -> Result<config::ConfigBuilder<config::builder::DefaultState>> {
let mut builder = builder;
// Global log level override
if let Some(ref level) = args.log_level { if let Some(ref level) = args.log_level {
builder = builder.set_override("log_level", level.clone())?; builder = builder.set_override("log_level", level.clone())?;
} }
// Command-specific overrides
match command { match command {
Command::Server(cmd_args) => { Command::Server(cmd_args) => {
if let Some(port) = cmd_args.port { if let Some(port) = cmd_args.port {
@ -59,10 +82,5 @@ impl AppConfig {
} }
} }
builder Ok(builder)
.build()
.context("Failed to build configuration layers")?
.try_deserialize()
.context("Failed to deserialize Config")
}
} }

39
lib/src/presentation/cli/defaults.rs

@ -3,6 +3,8 @@ pub use config::ConfigBuilder;
use const_format::formatcp; use const_format::formatcp;
use crate::cli::Command;
pub const HOST: &str = "127.0.0.1"; pub const HOST: &str = "127.0.0.1";
pub const PORT: u16 = 8080; pub const PORT: u16 = 8080;
pub const LOG_LEVEL: &str = "info"; pub const LOG_LEVEL: &str = "info";
@ -19,23 +21,34 @@ pub const HELP_ENC_INPUT: &str = formatcp!("Number to encode");
pub const HELP_IMPORT_DICT_NAME: &str = formatcp!("Dictionary name"); pub const HELP_IMPORT_DICT_NAME: &str = formatcp!("Dictionary name");
pub const HELP_IMPORT_DICT_INPUT: &str = formatcp!("Dictionary file path"); pub const HELP_IMPORT_DICT_INPUT: &str = formatcp!("Dictionary file path");
pub fn set_defaults( pub fn set_command_defaults(
builder: ConfigBuilder<config::builder::DefaultState>, builder: ConfigBuilder<config::builder::DefaultState>,
command: &Command,
) -> Result<ConfigBuilder<config::builder::DefaultState>> { ) -> Result<ConfigBuilder<config::builder::DefaultState>> {
builder let mut builder = builder.set_default("log_level", LOG_LEVEL)?;
.set_default("log_level", LOG_LEVEL)?
// Server match command {
Command::Server(_) => {
builder = builder
.set_default("server.host", HOST)? .set_default("server.host", HOST)?
.set_default("server.port", PORT)? .set_default("server.port", PORT)?;
// Decoder }
Command::Decode(_) => {
builder = builder
.set_default("decoder.system", SYSTEM_NAME)? .set_default("decoder.system", SYSTEM_NAME)?
.set_default("decoder.input", "")? .set_default("decoder.input", "")?;
// Encoder }
Command::Encode(_) => {
builder = builder
.set_default("encoder.system", SYSTEM_NAME)? .set_default("encoder.system", SYSTEM_NAME)?
.set_default("encoder.input", "")? .set_default("encoder.input", "")?;
// Import Dict }
Command::ImportDict(_) => {
builder = builder
.set_default("import_dict.name", IMPORT_DICT_NAME)? .set_default("import_dict.name", IMPORT_DICT_NAME)?
.set_default("import_dict.path", IMPORT_DICT_PATH) .set_default("import_dict.path", IMPORT_DICT_PATH)?;
// Wrapping in Result }
.map_err(|e| e.into()) }
Ok(builder)
} }

Loading…
Cancel
Save