Skip to content

Commit

Permalink
Fixed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pvshvp-oss committed May 12, 2024
1 parent ff9ea0f commit 2f2f698
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 52 deletions.
1 change: 1 addition & 0 deletions paxy-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ paxy = { workspace = true }

# Logging
tracing = { workspace = true }
log = { workspace = true, features = ["serde"] }

# Error handling
snafu = {workspace = true}
Expand Down
31 changes: 13 additions & 18 deletions paxy-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ pub enum Error {
}

// region: IMPORTS

use paxy::app::ui;

use owo_colors::OwoColorize;
use paxy::ui;
use snafu::Snafu;

// endregion: IMPORTS
Expand Down Expand Up @@ -66,47 +68,39 @@ mod cli_template {

/// Implement a trait that can extract standard global arguments from our
/// own CLI template
impl ui::GlobalArguments for CliTemplate
{
impl ui::GlobalArguments for CliTemplate {
fn config_filepath(&self) -> &Option<PathBuf> {
self
.global_args
self.global_args
.config_filepath()
}

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

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

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

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

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

fn verbosity_filter(&self) -> log::LevelFilter {
self
.global_args
self.global_args
.verbosity_filter()
}
}
Expand Down Expand Up @@ -410,7 +404,8 @@ mod cli_template {
use std::path::PathBuf;

use clap::{Args, Parser, Subcommand};
use paxy::ui;

use paxy::app::ui;

// endregion: IMPORTS
}
Expand Down
1 change: 1 addition & 0 deletions paxy-gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ paxy = { workspace = true }

# Logging
tracing = { workspace = true }
log = { workspace = true, features = ["serde"] }

# Error handling
snafu = {workspace = true}
Expand Down
9 changes: 5 additions & 4 deletions paxy-gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub enum Error {
// region: IMPORTS

use owo_colors::OwoColorize;
use paxy::ui;

use paxy::app::ui;
use snafu::Snafu;

// endregion: IMPORTS
Expand All @@ -49,8 +50,7 @@ mod gui_cli_template {

/// Implement a trait that can extract standard global arguments from our
/// own CLI template
impl ui::GlobalArguments for CliTemplate
{
impl ui::GlobalArguments for CliTemplate {
fn config_filepath(&self) -> &Option<PathBuf> {
self.global_args
.config_filepath()
Expand Down Expand Up @@ -92,7 +92,8 @@ mod gui_cli_template {
use std::path::PathBuf;

use clap::Parser;
use paxy::ui;

use paxy::app::ui;

// endregion: IMPORTS
}
Expand Down
54 changes: 30 additions & 24 deletions paxy/src/app/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
lazy_static! {
pub static ref CONFIG_FILE_EXTENSIONS: [&'static str] = ["toml", "json", "yaml", "yml"];
pub static ref CONFIG_FILE_EXTENSIONS: &'static [&'static str] = &["toml", "json", "yaml", "yml"];
}

/// Initializes a layered configuration deriving values from the app-wide
/// defaults, overridden by values from global paths, overridden by values from
/// local paths. overridden by environment variables starting with `PAXY_`,
/// overridden by the configuration file specified by the commandline.
/// Values from only files with supported file extensions would be merged.
pub fn init_config<G: GlobalArguments>(console_global_arguments: G) -> Result<(ConfigTemplate, Vec<PathBuf>), Error>
{
pub fn init_config<G: GlobalArguments>(
console_global_arguments: G,
) -> Result<(ConfigTemplate, Vec<PathBuf>), Error> {
let mut candidate_config_filepaths: Vec<PathBuf> = Vec::new();

// Global directories
Expand All @@ -30,14 +31,18 @@ pub fn init_config<G: GlobalArguments>(console_global_arguments: G) -> Result<(C
.iter_mut()
.for_each(|f| f.push(*app::APP_NAME));

let lowercase_config_file_extensions = *CONFIG_FILE_EXTENSIONS.iter();
let uppercase_config_file_extensions = lowercase_config_file_extensions.map(str::to_uppercase);
let lowercase_config_file_extensions = CONFIG_FILE_EXTENSIONS.iter().copied().map(str::to_string);
let uppercase_config_file_extensions = CONFIG_FILE_EXTENSIONS.iter()
.map(|extension| {
extension
.to_uppercase()
});

candidate_config_filepaths = candidate_config_filepaths
.iter()
.cartesian_product(
lowercase_config_file_extensions.chain(uppercase_config_file_extensions),
);
.into_iter()
.cartesian_product(lowercase_config_file_extensions.chain(uppercase_config_file_extensions))
.map(|(filepath_stub, extension)| filepath_stub.with_extension(extension))
.collect();

// Initialize configuration with app-wide defaults
let mut config = Config::new();
Expand Down Expand Up @@ -85,10 +90,10 @@ impl Config {
pub fn with_overriding_file<P: AsRef<Path>>(mut self, filepath: P) -> Self {
let filepath: &Path = filepath.as_ref();
if let Some(file_extension) = filepath.extension() {
file_extension = file_extension
let file_extension = file_extension
.to_string_lossy()
.to_lowercase();
match file_extension {
match file_extension.as_str() {
"toml" => {
self.figment = self
.figment
Expand All @@ -104,6 +109,7 @@ impl Config {
.figment
.admerge(Yaml::file(filepath));
}
&_ => {}
}
}

Expand All @@ -129,22 +135,21 @@ impl Config {
) -> Self
where
I1: IntoIterator<Item = S>,
S: AsRef<str>,
S: AsRef<str> + Clone,
I2: IntoIterator<Item = P>,
P: Into<PathBuf>,
<I2 as IntoIterator>::IntoIter: Clone,
P: Into<PathBuf> + Clone,
{
let filepath_stubs = filepath_stubs
.into_iter();
self = file_extensions
.into_iter()
.map(Into::into);
file_extensions
.into_iter()
.map(|file_extension| file_extension.as_ref())
.cartesian_product(&filepath_stubs)
.map(|(file_extension, filepath_stub)| {
let filepath = filepath_stub;
filepath.set_extension(file_extension);
.cartesian_product(filepath_stubs)
.fold(self, |config, (file_extension, filepath_stub)| {
let mut filepath: PathBuf = filepath_stub.into();
filepath.set_extension(file_extension.as_ref());

self = self.with_overriding_file(filepath);
config.with_overriding_file(filepath)
});

self
Expand All @@ -157,8 +162,8 @@ impl Config {
) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
P: Into<PathBuf>,
S: AsRef<str> + Clone,
P: Into<PathBuf> + Clone,
{
self.with_overriding_filepath_stubs(file_extensions, iter::once(filepath_stub))
}
Expand Down Expand Up @@ -253,6 +258,7 @@ impl Config {
use std::{
env,
iter,
clone::Clone,
path::{Path, PathBuf},
};

Expand Down
11 changes: 5 additions & 6 deletions paxy/src/app/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ where
.context(crate::AppSnafu {})?;

// Adjust output formatting if requested
adjust_output_formatting(&config.console_output_format, &mut logging_handle);
adjust_output_formatting(&config.console_output_format, &mut logging_handle)?;

emit_welcome_messages();

Expand Down Expand Up @@ -268,7 +268,7 @@ pub trait GlobalArguments {
}
}

impl<T:GlobalArguments> GlobalArguments for &T {
impl<T: GlobalArguments> GlobalArguments for &T {
fn config_filepath(&self) -> &Option<PathBuf> {
(**self).config_filepath()
}
Expand Down Expand Up @@ -386,11 +386,10 @@ pub mod cli_template {

impl<L> GlobalArguments for GlobalArgs<L>
where
L: clap_verbosity_flag::LogLevel
L: clap_verbosity_flag::LogLevel,
{
fn config_filepath(&self) -> &Option<PathBuf> {
&self
.config_file
&self.config_file
}

fn is_json(&self) -> bool {
Expand Down Expand Up @@ -421,7 +420,7 @@ pub mod cli_template {

// region: IMPORTS

use std::{path::PathBuf};
use std::path::PathBuf;

use clap::Args;

Expand Down

0 comments on commit 2f2f698

Please sign in to comment.