-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy code from zola_chrono and split across crates to allow for reuse
- Loading branch information
Showing
9 changed files
with
626 additions
and
15 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use clap::ValueEnum; | ||
use log::LevelFilter; | ||
|
||
/// Exists to provide better help messages variants copied from LevelFilter as | ||
/// that's the type that is actually needed | ||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug, Default)] | ||
#[allow(missing_docs)] | ||
pub enum LogLevel { | ||
/// Nothing emitted in this mode | ||
#[default] | ||
Off, | ||
Error, | ||
Warn, | ||
Info, | ||
Debug, | ||
Trace, | ||
} | ||
|
||
impl From<LogLevel> for LevelFilter { | ||
fn from(value: LogLevel) -> Self { | ||
match value { | ||
LogLevel::Off => LevelFilter::Off, | ||
LogLevel::Error => LevelFilter::Error, | ||
LogLevel::Warn => LevelFilter::Warn, | ||
LogLevel::Info => LevelFilter::Info, | ||
LogLevel::Debug => LevelFilter::Debug, | ||
LogLevel::Trace => LevelFilter::Trace, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
use log::LevelFilter; | ||
|
||
use log4rs::{ | ||
append::console::ConsoleAppender, | ||
config::{Appender, Root}, | ||
encode::pattern::PatternEncoder, | ||
}; | ||
|
||
mod cli_annotated; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
pub use cli_annotated::LogLevel; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
/// Initializes logging for the application (Should be called at most once) | ||
pub fn init_logging(log_level: LevelFilter) -> anyhow::Result<()> { | ||
let stdout = ConsoleAppender::builder() | ||
.encoder(Box::new(PatternEncoder::new( | ||
"{h({d(%Y-%m-%d %H:%M:%S)} {l} {t} - {m})}{n}", | ||
))) | ||
.build(); | ||
let config = log4rs::Config::builder() | ||
.appender(Appender::builder().build("stdout", Box::new(stdout))) | ||
.build(Root::builder().appender("stdout").build(log_level))?; | ||
log4rs::init_config(config)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//! Stores Command Line Interface (cli) configuration | ||
use clap::Parser; | ||
use one_log::LogLevel; | ||
|
||
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)] | ||
#[command(author, version, about)] | ||
/// Stores the configurations acquired via the command line | ||
pub struct Cli { | ||
/// Set logging level to use | ||
#[arg(long, short, value_enum, default_value_t = LogLevel::Info)] | ||
pub log_level: LogLevel, | ||
|
||
#[arg(value_name = "Original Text")] | ||
/// The text to be converted to snake case | ||
pub org_str: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![deny(missing_docs)] | ||
#![deny(missing_debug_implementations)] | ||
// For documenting optional features. See more at <https://c-git.github.io/rust/documentation/> | ||
#![cfg_attr(docsrs, feature(doc_cfg))] | ||
#![forbid(unsafe_code)] | ||
//! Converts text into snake case | ||
|
||
mod cli; | ||
|
||
pub use cli::Cli; | ||
|
||
/// Runs the body of the logic | ||
pub fn run(cli: &Cli) -> anyhow::Result<String> { | ||
// TODO: Conversion | ||
Ok(cli.org_str.clone()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use anyhow::Context; | ||
use clap::Parser; | ||
use log::debug; | ||
use one_log::init_logging; | ||
use snake_case::{self, run, Cli}; | ||
fn main() -> anyhow::Result<()> { | ||
let cli: Cli = Cli::parse(); | ||
init_logging(cli.log_level.into())?; | ||
debug!("Cli: {cli:#?}"); | ||
let converted_text = run(&cli).context("Failed to convert input")?; | ||
println!("{converted_text}"); | ||
Ok(()) | ||
} |