Skip to content

Commit

Permalink
Copy code from zola_chrono and split across crates to allow for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Dec 16, 2023
1 parent 4efe513 commit 4b688cf
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 15 deletions.
518 changes: 516 additions & 2 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ resolver = "2"
members = ["crates/snake_case", "crates/one_log"]

[workspace.dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.11", features = [
"cargo",
"derive",
"env",
"wrap_help",
] }
log = "0.4.20"
log4rs = "1.2.0"
one_log = { version = "*", path = "crates/one_log" }
6 changes: 6 additions & 0 deletions crates/one_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
name = "one_log"
version = "0.1.0"
edition = "2021"
publish = false
description = "Shares logging setup code between crates"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log4rs.workspace = true
anyhow.workspace = true
log.workspace = true
clap.workspace = true
30 changes: 30 additions & 0 deletions crates/one_log/src/cli_annotated.rs
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,
}
}
}
33 changes: 22 additions & 11 deletions crates/one_log/src/lib.rs
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(())
}
4 changes: 4 additions & 0 deletions crates/snake_case/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow.workspace = true
clap.workspace = true
log.workspace = true
log4rs.workspace = true
one_log.workspace = true
16 changes: 16 additions & 0 deletions crates/snake_case/src/cli.rs
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,
}
16 changes: 16 additions & 0 deletions crates/snake_case/src/lib.rs
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())
}
14 changes: 12 additions & 2 deletions crates/snake_case/src/main.rs
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(())
}

0 comments on commit 4b688cf

Please sign in to comment.