Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pvshvp-oss committed May 6, 2024
1 parent f3a8a9c commit dcd64e0
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 107 deletions.
42 changes: 19 additions & 23 deletions paxy-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod cli_template {
)]
pub struct CliTemplate {
#[command(flatten)]
pub global_arguments: ui::cli_template::GlobalArgs<clap_verbosity_flag::InfoLevel>,
pub global_args: ui::cli_template::GlobalArgs<clap_verbosity_flag::InfoLevel>,

#[command(subcommand)]
pub entity: Option<EntitySubcommand>,
Expand All @@ -67,43 +67,39 @@ mod cli_template {
/// Implement a trait that can extract standard global arguments from our
/// own CLI template
impl ui::GlobalArguments for CliTemplate {
type L = clap_verbosity_flag::InfoLevel;

fn config_file(&self) -> &Option<PathBuf> {
&self
.global_arguments
.config_file
fn config_filepath(&self) -> &Option<PathBuf> {
self.global_args
.config_filepath()
}

fn is_json(&self) -> bool {
self.global_arguments
.json_flag
self.global_args
.is_json()
}

fn is_plain(&self) -> bool {
self.global_arguments
.plain_flag
self.global_args
.is_plain()
}

fn is_debug(&self) -> bool {
self.global_arguments
.debug_flag
self.global_args
.is_debug()
}

fn is_no_color(&self) -> bool {
self.global_arguments
.no_color_flag
fn is_test(&self) -> bool {
self.global_args
.is_test()
}

fn is_test(&self) -> bool {
self.global_arguments
.test_flag
fn is_no_color(&self) -> bool {
self.global_args
.is_no_color()
}

fn verbosity(&self) -> &clap_verbosity_flag::Verbosity<Self::L> {
&self
.global_arguments
.verbose
fn verbosity_filter(&self) -> &log::LevelFilter {
self.global_args
.verbosity_filter()
}
}

Expand Down
30 changes: 13 additions & 17 deletions paxy-gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,39 @@ mod gui_cli_template {
/// Implement a trait that can extract standard global arguments from our
/// own CLI template
impl ui::GlobalArguments for CliTemplate {
type L = clap_verbosity_flag::InfoLevel;

fn config_file(&self) -> &Option<PathBuf> {
&self
.global_args
.config_file
fn config_filepath(&self) -> &Option<PathBuf> {
self.global_args
.config_filepath()
}

fn is_json(&self) -> bool {
self.global_args
.json_flag
.is_json()
}

fn is_plain(&self) -> bool {
self.global_args
.plain_flag
.is_plain()
}

fn is_debug(&self) -> bool {
self.global_args
.debug_flag
.is_debug()
}

fn is_no_color(&self) -> bool {
fn is_test(&self) -> bool {
self.global_args
.no_color_flag
.is_test()
}

fn is_test(&self) -> bool {
fn is_no_color(&self) -> bool {
self.global_args
.test_flag
.is_no_color()
}

fn verbosity(&self) -> &clap_verbosity_flag::Verbosity<Self::L> {
&self
.global_args
.verbose
fn verbosity_filter(&self) -> &log::LevelFilter {
self.global_args
.verbosity_filter()
}
}

Expand Down
106 changes: 83 additions & 23 deletions paxy/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,102 @@ fn admerge_from_stub(candidate_config_filepath_stub: &PathBuf, mut figment: Figm
figment
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub config_dirpaths: Vec<PathBuf>,

pub log_dirpath: PathBuf,

pub cli_output_format: ui::CliOutputFormat,
pub figment: Figment,
}

impl Config {
pub fn new() -> Self {
Self::default()
Self {
figment: Figment::from(ConfigTemplate::default()),
}
}
}

impl Default for Config {
fn default() -> Self {
Self {
config_dirpaths: None,
log_dirpath: None,
log_directory: todo!(),
cli_output_format: todo!(),
pub fn with_overriding_file<P: AsRef<Path>>(&mut self, filepath: P) -> &mut Self {
let filepath: &Path = filepath.as_ref();
if let Some(file_extension) = filepath.extension() {
file_extension = file_extension
.to_string_lossy()
.to_lowercase();
match (file_extension) {
"toml" => {
self.figment = self
.figment
.admerge(Toml::file(filepath));
}
"json" => {
self.figment = self
.figment
.admerge(Json::file(filepath));
}
"yaml" | "yml" => {
self.figment = self
.figment
.admerge(Yaml::file(filepath));
}
}
}

self
}
}

impl Provider for Config {
fn metadata(&self) -> Metadata {
Metadata::named("Library Config")
pub fn with_overriding_files<P, I>(&mut self, filepaths: I) -> &mut Self
where
P: AsRef<Path>,
I: Iterator<Item = P>,
{
filepaths.for_each(|filepath| self.with_overriding_file(filepath));

self
}

pub fn with_overriding_env<S: AsRef<str>>(prefix: S) -> &mut Self {
let prefix = prefix.as_ref();
self.figment = self
.figment
.admerge(Env::prefixed(prefix));

self
}

fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
figment::providers::Serialized::defaults(Config::default()).data()
pub fn with_overriding_env_var<S: AsRef<str>>(env_var_name: S) -> &mut Self {
let env_var_name = env_var_name.as_ref();
self.figment = self
.figment
.admerge(Env::raw().only(&[env_var_name]));

self
}

pub fn with_overriding_args<A: ui::GlobalArguments>(&mut self, arguments: A) -> &mut Self {
if let Some(path) = arguments.config_filepath() {
self.figment = self.figment.admerge(("config_filepaths", path));
}

self
}

fn profile(&self) -> Option<Profile> {
None
pub fn object(&self) -> Result<ConfigTemplate, Error> {
self.figment
.extract()
.context(ExtractConfigSnafu {})?
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConfigTemplate {
pub config_filepaths: Vec<PathBuf>,
pub log_filepath_stub: PathBuf,
pub console_output_format: ConsoleOutputFormat,
}

impl Default for ConfigTemplate {
fn default() -> Self {
Self {
config_filepaths: Vec::new(),
log_filepath_stub: PathBuf::default(),
console_output_format: ConsoleOutputFormat::default(),
}
}
}

Expand All @@ -145,6 +204,7 @@ use log::LevelFilter;
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};

use super::ui::GlobalArguments;
use crate::app;
use crate::app::ui;

Expand Down
Loading

0 comments on commit dcd64e0

Please sign in to comment.