wd_run: A Rust library providing a lightweight logging facade

wd_run supports command line parsing and configuration file parsing. It is mainly achieved through the method of registering callbacks.
The following configuration file parsing formats are supported:
Getting Started
[dependencies]
wd_run = "0.1"
Example
cmd
- ExecManager : a global processor
- AsyncEvent : the interface to the callback function
- CmdInfo : command registration tool
Run the following code and the command line
//help
cargo run -- -h
//run
cargo run -- run
use wd_run::{CmdInfo, KE_ARGS, ExecManager,Context};
use std::collections::HashMap;
use std::sync::Arc;
#[tokio::main]
async fn main() {
let mut em = ExecManager::new().await;
let cr = CoreRun{};
em.registered(cr.clone(), CoreRun::get_flag_default());
em.registered(cr, CoreRun::get_flag_exit());
em.run().await.unwrap();
}
#[derive(Clone)]
pub struct CoreRun {
}
impl CoreRun {
pub fn get_flag_default() -> CmdInfo {
let mut ci = CmdInfo::new("run", "", "start run core");
ci.set_flag("-c", "./src/util/conf/config.toml", "config file path");
ci
}
pub fn get_flags(ctx: Arc<Context>) -> HashMap<String, String> {
match ctx.get_value::<&str, HashMap<String, String>>(&KE_ARGS) {
Some(s) => s,
None => HashMap::new(),
}
}
pub fn get_flag_exit() -> CmdInfo {
CmdInfo::new("exit", "", "automatically when exiting the service")
}
async fn run(&self, ctx: Arc<Context>) {
let flags = CoreRun::get_flags(ctx);
let path = flags.get("-c").expect("未获取到配置文件路径").clone();
println!("{}",path);
}
}
#[wd_run::event_trait]
impl wd_run::AsyncEvent for CoreRun {
async fn handle(&self, ctx: Arc<Context>) {
let opt_cmd = ctx.get_msg::<String>();
if let None = opt_cmd {
return;
}
let cmd = String::clone(&opt_cmd.unwrap());
match cmd.as_str() {
"run" => {
self.run(ctx).await;
}
"exit" => {
println!("-----> stop")
}
_ => {
println!("Unprocessed commands:{}", cmd)
}
}
}
}
config
#[derive(Debug,Deserialize)]
pub struct Config {
pub log: i32,
pub server: String,
pub stream: String,
}
#[test]
fn test_config () {
let conf:Config = wd_run::load_config("./src/util/conf/config.toml").unwrap();
println!("{:?}",conf)
}
License
Licensed under either of
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.