From b21309e0e5cc30dd5b9a78102160150737d4cae3 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Sun, 3 Nov 2019 05:55:37 +0100 Subject: [PATCH] functional update * convert from Rust 2015 to Rust 2018 * convert from slog to tracing new module-system handling (macros and crates) Signed-off-by: Ralf Zerres --- Cargo.toml | 2 +- src/main.rs | 79 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 51433db..5fb359e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Ralf Zerres "] description = "Supports lawyers to capture relevant data encountered during an online legal advice\n" readme = "README.md" license = "MIT" +edition = "2018" [profile.release] # optimize to max speed @@ -25,4 +26,3 @@ features = [ "postgres", "sqlite" ] version = "0.10.0" [dependencies.locales] -version = "0.1.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9421288..6cd2b86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,43 +4,23 @@ * SPDX-License-Identifier: 0BSD */ -#[macro_use] -extern crate clap; -extern crate locales; +// Rust nightly supports procedural macros! +//#![feature(proc_macro)] +//#![deny(rust_2018_idioms)] -use clap::App; +//use clap::{ArgMatches, Arg, App, SubCommand}; +use clap::ArgMatches; +use serde::Deserialize; +use std::{env, process}; +use dotenv::dotenv; use locales::t; +use tracing::{debug, info, instrument, span, Level}; -fn main() { - // set locale - let lang= "de"; +#[derive(Deserialize, Debug)] +struct Environment { + test_lang: String, +} - // handle commandline arguments with clap (relative path to cli.yml) - let res = t!("parse.arguments", lang); - println!("{}", &res); - - let yaml = load_yaml!("cli.yml"); - let matches = App::from_yaml(yaml) - .name(crate_name!()) - .version(crate_version!()) - .author(crate_authors!()) - .about(crate_description!()) - .get_matches(); - - // Gets a options value if supplied by user. otherwise set defaults - let config = matches.value_of("config").unwrap_or("default.conf"); - println!("Value for config: {}", config); - - let dbdriver = matches.value_of("dbdriver").unwrap_or("sqlite3"); - println!("Value for database driver: {}", dbdriver); - - // Vary the output based on how many times the user used the "verbose" flag - // (i.e. ' -v -v -v' or 'myprog -vvv' vs 'myprog -v' - match matches.occurrences_of("v") { - 0 => println!("No verbose info"), - 1 => println!("Some verbose info"), - 2 => println!("Tons of verbose info"), - 3 | _ => println!("Don't be crazy"), } // You can handle information about subcommands by requesting their matches by name @@ -52,6 +32,39 @@ fn main() { println!("Printing normally..."); } } +fn main() { + //initialize tracing eco-system + use tracing_subscriber::fmt; + + let span = span!(Level::TRACE, "advotracker_main"); + let _enter = span.enter(); + let subscriber = fmt::Subscriber::builder() + .with_env_filter("advotracker=trace") + .finish(); + + tracing::subscriber::with_default(subscriber, || { + // get testing environment (.env) + debug!( + message = "Reading Environment ...", + finished = "no!" + ); + + dotenv().ok(); + let mut lang = env::var("LANG").unwrap_or("en".to_string()); + match envy::from_env::() { + Ok(environment) => { + if environment.test_lang != lang { lang = environment.test_lang; } + }, + Err(e) => println!("Couldn't read LANG ({:#?})", e), + }; + let res = t!("parse.environment", lang); + info!(target: "advotracker_event", "{}", res); + info!(target: "advotracker_event", "{}", lang); + + // handle commandline arguments with clap (relative path to cli.yml) + let res = t!("parse.arguments", lang); + info!(target: "advotracker_event", "{}", res); + // Starting the program logic let res = t!("main.start", lang);