Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvshvp-oss committed Apr 12, 2024
1 parent 5b37333 commit 35dfc5f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ paxy = { path = "paxy" }
# Logging
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["serde", "tracing-serde"] }
tracing-subscriber = "0.3"

# Configuration
figment = "0.10"
Expand Down
45 changes: 26 additions & 19 deletions paxy/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ fn admerge_from_stub(candidate_config_filepath_stub: &PathBuf, mut figment: Figm

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub paths: app::Paths,

pub log_directory: Option<String>,

pub log_level_filter: Option<LevelFilter>,
Expand Down Expand Up @@ -128,6 +130,29 @@ impl Provider for Config {
}
}

// region: IMPORTS

use std::path::PathBuf;

use figment::{
providers::{Env, Format, Json, Toml, Yaml},
value::{Dict, Map},
Figment,
Metadata,
Profile,
Provider,
};
use log::LevelFilter;
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};

use crate::app;
use crate::app::ui;

// endregion: IMPORTS

// region: ERRORS

#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum Error {
Expand All @@ -146,22 +171,4 @@ pub enum Error {
ExtractConfig { source: figment::Error },
}

// region: IMPORTS

use std::path::PathBuf;

use figment::{
providers::{Env, Format, Json, Toml, Yaml},
value::{Dict, Map},
Figment,
Metadata,
Profile,
Provider,
};
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};
use log::LevelFilter;

use crate::{app, ui};

// endregion: IMPORTS
// endregion: ERRORS
16 changes: 11 additions & 5 deletions paxy/src/app/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// TODO: The module code goes here

// region: IMPORTS

use snafu::Snafu;

// endregion: IMPORTS

// region: ERRORS

#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum Error {
Expand All @@ -6,8 +16,4 @@ pub enum Error {
I18nDummy {},
}

// region: IMPORTS

use snafu::Snafu;

// endregion: IMPORTS
// endregion: ERRORS
47 changes: 29 additions & 18 deletions paxy/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ lazy_static! {
pub static ref APP_NAME: &'static str = "paxy";
}

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

// region: IMPORTS

use std::path::PathBuf;

use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use snafu::Snafu;

// endregion: IMPORTS

// region: ERRORS

#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum Error {
Expand All @@ -19,6 +37,13 @@ pub enum Error {
source: config::Error,
},

#[non_exhaustive]
#[snafu(display("in the UI: {source}"), visibility(pub))]
Ui {
#[snafu(backtrace)]
source: ui::Error,
},

#[non_exhaustive]
#[snafu(display("in internationalization: {source}"), visibility(pub))]
Internationalization {
Expand All @@ -27,27 +52,13 @@ pub enum Error {
},
}

// region: IMPORTS
use lazy_static::lazy_static;
use snafu::Snafu;
// endregion: ERRORS

// endregion: IMPORTS

// region: MODULES
// region: EXTERNAL-SUBMODULES

pub mod config;
pub mod i18n;
pub mod logging;
pub mod ui;

// endregion: MODULES

// region: RE-EXPORTS

#[allow(unused_imports)]
pub use config::*;
#[allow(unused_imports)]
pub use i18n::*;
#[allow(unused_imports)]
pub use logging::*;

// endregion: RE-EXPORTS
// endregion: EXTERNAL-SUBMODULES
75 changes: 61 additions & 14 deletions paxy/src/ui/mod.rs → paxy/src/app/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ where
.parse()
.ok()
});
let (mut handle, log_filepath) = logging::init_log(config_log_dirpath, config_verbosity_filter.into())
.context(app::LoggingSnafu {})
.context(crate::AppSnafu {})?;
let (mut handle, log_filepath) =
logging::init_log(config_log_dirpath, config_verbosity_filter.into())
.context(app::LoggingSnafu {})
.context(crate::AppSnafu {})?;

// Modify logging behavior if Plain or Json output is desired
if cli_input.is_json() {
Expand Down Expand Up @@ -153,6 +154,51 @@ fn emit_test_messages() {
tracing::info!(target:"PLAIN", "{} Testing: Plain Target", console::Emoji("🧪", ""));
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliFormat {
pub output_mode: Option<CliOutputMode>,
verbosity_level_filter: log::LevelFilter,
pub is_colored: Option<bool>,
}

impl CliFormat {
pub fn try_set_max_verbosity_level(suggested_max_verbosity_level: log::LevelFilter) -> Result<(),> {
return match self.output_mode.unwrap_or_default() {
CliOutputMode::Regular => suggested_max_verbosity_level.as_str().parse().unwrap(),
CliOutputMode::Plain => todo!(),
CliOutputMode::Json => todo!(),
CliOutputMode::Test => todo!(),
};
}
pub fn max_verbosity_level(&self) -> log::LevelFilter {
return verbosity_level_filter;
}
}

impl Default for CliFormat {
fn default() -> Self {
Self {
output_mode: Default::default(),
verbosity_level_filter: log::LevelFilter::Info,
is_colored: Some(true),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CliOutputMode {
Regular,
Plain,
Json,
Test,
}

impl Default for CliOutputMode {
fn default() -> Self {
CliOutputMode::Regular
}
}

impl<T> CliModifier for T
where
T: GlobalArguments,
Expand All @@ -165,22 +211,21 @@ where
<Self as GlobalArguments>::L: LogLevel,
{
fn verbosity_filter(&self) -> Option<LevelFilter> {
if self.is_plain() || self.is_json() {
return Some(LevelFilter::Info);
}

let verbosity_flag_filter = self
.verbosity()
.log_level_filter();

if verbosity_flag_filter < clap_verbosity_flag::LevelFilter::Debug && self.is_debug() {
if self.is_plain() || self.is_json() {
return Some(LevelFilter::Info);
} else if verbosity_flag_filter < clap_verbosity_flag::LevelFilter::Debug && self.is_debug()
{
return Some(LevelFilter::Debug);
} else {
return verbosity_flag_filter
.as_str()
.parse()
.ok();
}

verbosity_flag_filter
.as_str()
.parse()
.ok()
}

fn is_uncolored(&self) -> bool {
Expand Down Expand Up @@ -233,10 +278,12 @@ use core::fmt;
use std::{env, path::PathBuf};

use clap_verbosity_flag::LogLevel;
use log::LevelFilter;
use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use tracing::Level;
use tracing_appender::non_blocking::WorkerGuard;
use log::LevelFilter;

use crate::app::{self, config, logging};

Expand Down
30 changes: 13 additions & 17 deletions paxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ pub fn type_of<T>(_: &T) -> &str {
any::type_name::<T>()
}

// region: IMPORTS

use std::any;

use snafu::Snafu;

// endregion: IMPORTS

// region: ERRORS

#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum Error {
Expand All @@ -12,13 +22,6 @@ pub enum Error {
source: app::Error,
},

#[non_exhaustive]
#[snafu(display("in the UI: {source}"), visibility(pub))]
Ui {
#[snafu(backtrace)]
source: ui::Error,
},

#[non_exhaustive]
#[snafu(display("in an action:{source}"), visibility(pub))]
Actions {
Expand All @@ -27,19 +30,12 @@ pub enum Error {
},
}

// region: IMPORTS

use std::any;

use snafu::Snafu;

// endregion: IMPORTS
// endregion: ERRORS

// region: MODULES
// region: EXTERNAL-SUBMODULES

pub mod actions;
pub mod app;
pub mod data;
pub mod ui;

// endregion: MODULES
// endregion: EXTERNAL-SUBMODULES
Empty file removed requirements.txt
Empty file.

0 comments on commit 35dfc5f

Please sign in to comment.