refactor: split cli and logging to separate crates
All checks were successful
Test the running changes / Test (push) Successful in 42s
All checks were successful
Test the running changes / Test (push) Successful in 42s
This commit is contained in:
14
gravel_cli/Cargo.toml
Normal file
14
gravel_cli/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "gravel_cli"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "gravel"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
cracked_md = { path = "../cracked_md" }
|
||||
stdsrv = { path = "../stdsrv" }
|
||||
slogger = { path = "../slogger" }
|
||||
84
gravel_cli/src/args.rs
Normal file
84
gravel_cli/src/args.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
//! Simple and program specific command line argument parsing solution.
|
||||
|
||||
// todo: refactor to <command> <subcommand> [<options>]
|
||||
|
||||
use slogger::{LOG_LEVEL, Level};
|
||||
|
||||
use crate::error::Error;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub struct ProgramArgs {
|
||||
pub outdir: PathBuf,
|
||||
pub indir: PathBuf,
|
||||
pub generate: bool,
|
||||
pub force: bool,
|
||||
pub addr: Ipv4Addr,
|
||||
pub port: u16,
|
||||
pub verbose: bool,
|
||||
}
|
||||
|
||||
impl Default for ProgramArgs {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
indir: PathBuf::from("./web"),
|
||||
outdir: PathBuf::from("./html"),
|
||||
generate: false,
|
||||
force: false,
|
||||
addr: Ipv4Addr::UNSPECIFIED,
|
||||
port: 8080,
|
||||
verbose: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<std::env::Args> for ProgramArgs {
|
||||
type Error = crate::error::Error;
|
||||
fn try_from(mut value: std::env::Args) -> Result<Self, Self::Error> {
|
||||
let mut a = Self::default();
|
||||
let _ = value.next(); // ignore executable path
|
||||
while let Some(v) = value.next() {
|
||||
match v.as_str() {
|
||||
"-i" => {
|
||||
a.indir = value
|
||||
.next()
|
||||
.ok_or(Error::CommandLineArgsParse(
|
||||
"Expected input directory after option `-i`".to_string(),
|
||||
))?
|
||||
.into();
|
||||
}
|
||||
"-a" => {
|
||||
let addr_string = value.next().ok_or(Error::CommandLineArgsParse(
|
||||
"Expected listener IPv4 address after option `-a`".to_string(),
|
||||
))?;
|
||||
a.addr = Ipv4Addr::parse_ascii(addr_string.as_bytes()).map_err(|_e| {
|
||||
Error::CommandLineArgsParse(
|
||||
"Invalid IPv4 address after option `-a`".to_string(),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
"-p" => {
|
||||
let port_string = value.next().ok_or(Error::CommandLineArgsParse(
|
||||
"Expected listener port after option `-p`".to_string(),
|
||||
))?;
|
||||
a.port = port_string.parse().map_err(|_e| {
|
||||
Error::CommandLineArgsParse(
|
||||
"Invalid 16-bit port number after option `-p`".to_string(),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
"-g" => a.generate = true,
|
||||
"-f" => a.force = true,
|
||||
"-v" => {
|
||||
a.verbose = true;
|
||||
LOG_LEVEL.get_or_init(|| Level::Debug);
|
||||
}
|
||||
_ => {
|
||||
a.outdir = v.into();
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG_LEVEL.get_or_init(|| Level::Info);
|
||||
Ok(a)
|
||||
}
|
||||
}
|
||||
32
gravel_cli/src/error.rs
Normal file
32
gravel_cli/src/error.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Server(stdsrv::error::Error),
|
||||
MdParse(cracked_md::Error),
|
||||
CommandLineArgsParse(String),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::Server(e) => e.fmt(f),
|
||||
Error::MdParse(e) => e.fmt(f),
|
||||
Error::CommandLineArgsParse(s) => write!(f, "{s}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl From<cracked_md::Error> for Error {
|
||||
fn from(value: cracked_md::Error) -> Self {
|
||||
Self::MdParse(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<stdsrv::error::Error> for Error {
|
||||
fn from(value: stdsrv::error::Error) -> Self {
|
||||
Self::Server(value)
|
||||
}
|
||||
}
|
||||
17
gravel_cli/src/main.rs
Normal file
17
gravel_cli/src/main.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
#![feature(addr_parse_ascii, never_type)]
|
||||
|
||||
use args::ProgramArgs;
|
||||
use cracked_md::generate;
|
||||
use error::Error;
|
||||
//use slogger::{LOG_LEVEL, Level};
|
||||
use stdsrv::serve;
|
||||
|
||||
mod args;
|
||||
mod error;
|
||||
|
||||
fn main() -> Result<!, Error> {
|
||||
let args = ProgramArgs::try_from(std::env::args())?;
|
||||
|
||||
generate(&args.indir, &args.outdir, args.force)?;
|
||||
serve(args.addr, args.port, args.outdir)?;
|
||||
}
|
||||
Reference in New Issue
Block a user