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 <ralf.zerres@networkx.de>
This commit is contained in:
2019-11-03 05:55:37 +01:00
parent 49740091bf
commit b21309e0e5
2 changed files with 47 additions and 34 deletions

View File

@@ -5,6 +5,7 @@ authors = ["Ralf Zerres <ralf.zerres@networkx.de>"]
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"

View File

@@ -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::<Environment>() {
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);