Skip to content

Commit

Permalink
Lint, cleanup, fmt, clippy, 2021 edition
Browse files Browse the repository at this point in the history
A few minor cleanups to make it easier to submit PRs:
* migrate to 2021 edition
* run `cargo fmt`
* bump some dependencies to their latest
  * Note that Clap is locked to 2.x until clap-rs/clap#1206 is fixed in v4 (?)
* spelling fixes

TODO for another PR:
* fix many clippy [from_over_into](https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into) warnings in the src/map/map_value.rs file.
* README.md still has a few bugs. Uncomment a section in the lib.rs to show them during the test run.
  • Loading branch information
nyurik committed May 29, 2022
1 parent 8271d3b commit 30d4479
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 170 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
/target
**/*.rs.bk
Cargo.lock
/nogit
/nogit
.idea/
56 changes: 28 additions & 28 deletions Cargo.toml
Expand Up @@ -2,13 +2,13 @@
name = "viperus"
version = "0.1.11"
authors = ["mauro cordioli <coma@cordioli.it>"]
edition = "2018"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/maurocordioli/viperus"
description="Viperus is an (in)complete configuration solution for rust applications heavly inspired by the GO package Viper.It supports defaults reading from JSON, TOML, YAML, envfile,java properties, environment variables reading from Clap command line flags setting explicit values"
description = "Viperus is an (in)complete configuration solution for rust applications heavly inspired by the GO package Viper.It supports defaults reading from JSON, TOML, YAML, envfile,java properties, environment variables reading from Clap command line flags setting explicit values"
exclude = ["tarpaulin-report.html"]
keywords= ["config", "yaml", "toml", "json", "dotenv"]
readme="README.md"
keywords = ["config", "yaml", "toml", "json", "dotenv"]
readme = "README.md"

[badges]

Expand Down Expand Up @@ -44,22 +44,22 @@ maintenance = { status = "experimental" }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log="0.4"
serde={ version="1.0.0", optional=true}
serde_yaml={ version="0.8.11", optional=true}
serde_json={ version="1.0.44", optional=true}
clap={ version="2.33.0", optional=true}

dotenv={ version="0.15.0", optional=true}
toml= { version="0.5.5", optional=true}
java-properties = { version="1.2.0", optional=true}
lazy_static= { version="1.4.0", optional=true}
notify = { version="4.0.0", optional=true}

[dev-dependencies]
env_logger = "0.7.1"
tempfile = "3.1.0"
log = "0.4"
serde = { version = "1.0.0", optional = true }
serde_yaml = { version = "0.8.11", optional = true }
serde_json = { version = "1.0.44", optional = true }
clap = { version = "2.34.0", optional = true }

dotenv = { version = "0.15.0", optional = true }
toml = { version = "0.5.5", optional = true }
java-properties = { version = "1.4.0", optional = true }

lazy_static = { version = "1.4.0", optional = true }
notify = { version = "4.0.0", optional = true }

[dev-dependencies]
env_logger = "0.9"
tempfile = "3.3.0"
criterion = "0.3.0"

[[bench]]
Expand All @@ -75,15 +75,15 @@ default = ["cache", "global", "fmt-clap", "fmt-env", "fmt-javaproperties", "fmt-
global = ["lazy_static"]

# cache queries
cache =[]
cache = []

# triggers parameter changes in config files
watch=["global", "notify"]
watch = ["global", "notify"]

# supported format parsers
fmt-clap=["clap"]
fmt-env=["dotenv"]
fmt-javaproperties=["java-properties"]
fmt-json=["serde","serde_json"]
fmt-toml=[ "toml"]
fmt-yaml=["serde","serde_yaml"]
fmt-clap = ["clap"]
fmt-env = ["dotenv"]
fmt-javaproperties = ["java-properties"]
fmt-json = ["serde", "serde_json"]
fmt-toml = ["toml"]
fmt-yaml = ["serde", "serde_yaml"]
51 changes: 27 additions & 24 deletions README.md
Expand Up @@ -2,7 +2,7 @@
~~go~~ rust configuration with fangs!

`viperus` is an (in)complete commandline configuration solution for Rust applications.
It is heavly inspired by the wonderful go package <https://github.com/spf13/viper>
It is heavily inspired by the wonderful go package <https://github.com/spf13/viper>
Use it at your own risk. ;-)

no Go projects ~~has been harmed~~ are built consuming `viperus` :-)
Expand All @@ -14,14 +14,14 @@ no Go projects ~~has been harmed~~ are built consuming `viperus` :-)
* 0.1.5 add watch_all files with autoreload
* 0.1.4 add format: java properties files
* 0.1.3 better clap args: default values
* 0.1.2 relaod config from files
* 0.1.2 reload config from files
* 0.1.1 fixes dcs
* 0.1.0 first release

## What is viperus?
Viperus is a package, that enables program configuration via an extendable
types system. Parameters can be incoprorated via cli parameters, environment
viariables. Parameters may be declared in configuration files as well.
types system. Parameters can be incorporated via cli parameters, environment
variables. Parameters may be declared in configuration files as well.

Given implementation can handle:

Expand All @@ -37,7 +37,7 @@ Given implementation can handle:

## Why viperus?

beacuse I was migrating some go apps ... and there was missing a rust tool that supplies `Viper` ease of use :-)
because I was migrating some go apps ... and there was missing a rust tool that supplies `Viper` ease of use :-)

When config parameters are applied, Viperus uses the following decreasing precedence order:

Expand All @@ -49,7 +49,7 @@ When config parameters are applied, Viperus uses the following decreasing preced

`viperus` merges configuration parameters from

* clap optoins
* clap options
* dotenv
* json files
* toml files
Expand All @@ -58,8 +58,8 @@ When config parameters are applied, Viperus uses the following decreasing preced
in a single typed hash structure. The structure can be preset with default. Values are type checked.

You can create a standalone `viperus` object or "enjoy" a global
instance. The global instance is guarded with a Mutex to garantee
thread safty.
instance. The global instance is guarded with a Mutex to guarantee
thread safety.

Structure elements of a static instance may be manipulated via the shadow functions

Expand All @@ -69,7 +69,7 @@ Structure elements of a static instance may be manipulated via the shadow functi
* load_file

```rust
// add a dotenv config file. keys defautls to the env variables
// add a dotenv config file. keys defaults to the env variables
viperus::load_file(".env", viperus::Format::ENV).unwrap();

// add another file
Expand All @@ -88,7 +88,7 @@ viperus::cache(true);
let ok=viperus::get::<bool>("TEST_BOOL").unwrap();
```

** Sidenote: Yes I konw globals are evil. Inspiration was taken form the go package `viper` that is taking this route ....
** Side note: Yes I know globals are evil. Inspiration was taken form the go package `viper` that is taking this route ....
If you dislike globals, or other preset defaults, go ahead and opt-out like this:

```cargo
Expand All @@ -104,7 +104,7 @@ Cache is only thread safe if you use a global instance that is guarded with an a
```

The caching is invalidated, if you
* reload any file with an excplicit ``` viperus::reload() ```
* reload any file with an explicit ``` viperus::reload() ```
* parameter changes inside a file, that is observed with the `watch` feature

## logging/debug
Expand All @@ -122,7 +122,7 @@ The crate may be adopted using cargo's "feature" semantic. The default enables a
* feature = "watch" enabling the automatic file reload ( prerequisite: feature=global)
* feature = "cache" enabling caching

single featues could be activated in a selective way via cargo.toml
single feature could be activated in a selective way via cargo.toml

```toml
[dependencies.viperus]
Expand All @@ -132,14 +132,14 @@ version = "0.1.8"
default-features = false

# cherry-pick individual features
features = ["global", "cache","watch","fmt-yaml"]
features = ["global", "cache", "watch", "fmt-yaml"]
```

## Tests
All available integration tests are put into the subdirectory `tests`.
Reading the source code may help to get into the inner logic.

```
```bash
cargo test
```

Expand All @@ -149,7 +149,7 @@ Inside the example subdirectory there is a reference implementation that consume

Compile and execute it like this:

```
```bash
cargo run --example cli-clap-yaml --
```

Expand All @@ -158,14 +158,17 @@ arguments, the revealed parameters inside the config file will be parsed out.

In a second run, we call this example providing commandline parameters:

```
```bash
cargo run --example cli-clap-yaml -- -c ./example.conf -u http://nowhere/api/v1
```

Since the `cli-clap-yaml` does call **with** commandline
arguments, the revealed parameters inside the config file are overwritten.

```rust
use clap::{App, Arg};
use log::debug;
use viperus::{path, Format, Viperus};

let matches = App::new("My Super Program")
.arg(Arg::with_name("v")
Expand All @@ -178,24 +181,24 @@ let mut v = Viperus::new();
//enable clap
v.load_clap(matches);
//enable a yaml json toml file
v.load_file(&path!(".","assets","test.yaml"), Format::YAML).unwrap();
v.load_file(&path!(".","assets","test.json"), Format::JSON).unwrap();
v.load_file(&path!(".","assets","test.toml"), Format::TOML).unwrap();
v.load_file(&path!(".","assets","test.properties"), Format::JAVAPROPERTIES).unwrap();
v.load_file(&path!(".", "assets", "test.yaml"), Format::YAML).unwrap();
v.load_file(&path!(".", "assets", "test.json"), Format::JSON).unwrap();
v.load_file(&path!(".", "assets", "test.toml"), Format::TOML).unwrap();
v.load_file(&path!(".", "assets", "test.properties"), Format::JAVAPROPERTIES).unwrap();
//link the "v" clap option to the key "verbose"
v.bond_clap("v","verbose");
v.bond_clap("v", "verbose");


//add an explicit overload
v.add("service.url", String::from("http://example.com"));
v.add("service.url", String::from("https://example.com"));
debug!("final {:?}", v);

//get a typed key
let s: &str = v.get("service.url").unwrap();
assert_eq!("http://example.com", s);
assert_eq!("https://example.com", s);

//get a bool from configs or app args
let fVerbose=v.get::<bool>("verbose").unwrap();
let fVerbose = v.get::<bool>("verbose").unwrap();
assert_eq!(true, fVerbose);
```

Expand Down
13 changes: 6 additions & 7 deletions examples/cli-clap-yaml.rs
Expand Up @@ -39,19 +39,18 @@ fn main() {
#[cfg(feature = "fmt-yaml")]
{
println!("Feature: `fmt-yml`, parsing ...");
viperus::load_file(&path!("examples", "example.yaml"), viperus::Format::YAML).unwrap();
viperus::load_file(&path!("examples", "example.yaml"), viperus::Format::YAML).unwrap();

println!("Parsed parameters:");
println!(" - config: {}",
viperus::get::<String>("config.file").unwrap()
println!(
" - config: {}",
viperus::get::<String>("config.file").unwrap()
);
println!(" - url: {}",
viperus::get::<String>("service.url").unwrap()
);
println!(" - url: {}", viperus::get::<String>("service.url").unwrap());
}
}

#[cfg(not(all(feature = "global", feature = "fmt-clap")))]
fn main() {
println!("Viperus: opted out feature `global`, `fmt-clap`" );
println!("Viperus: opted out feature `global`, `fmt-clap`");
}
4 changes: 2 additions & 2 deletions src/adapter.rs
Expand Up @@ -24,9 +24,9 @@ pub use ayaml::*;

/// ConfigAdapter mediates from varius config format and Viperus
pub trait ConfigAdapter {
/// parse create he interna rappresentation of the config file/mode
/// parse create he internal presentation of the config file/mode
fn parse(&mut self) -> AdapterResult<()>;
/// get_map returns a key value map rappresentation of the actaul config
/// get_map returns a key value map presentation of the actual config
fn get_map(&self) -> crate::map::Map;
}

Expand Down
1 change: 1 addition & 0 deletions src/adapter/aenv.rs
Expand Up @@ -29,6 +29,7 @@ impl EnvAdapter {
&self.real_path
}
}

impl ConfigAdapter for EnvAdapter {
fn parse(&mut self) -> AdapterResult<()> {
self.data = dotenv::vars().collect();
Expand Down
1 change: 1 addition & 0 deletions src/adapter/ajson.rs
@@ -1,4 +1,5 @@
use super::*;
use log::debug;

/// JsonAdapter map a Json file in a linear multilevel key/value array
///
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/aprop.rs
Expand Up @@ -4,7 +4,7 @@ use java_properties::PropertiesIter;

use std::io::BufReader;

/// JPropertiesAdapter map a ajava properties file in a linear multilevel key/value array
/// JPropertiesAdapter map a java properties file in a linear multilevel key/value array
///
/// the adaptor could be consumed by Viperous
/// internally uses java_properties crate
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/atoml.rs
@@ -1,5 +1,5 @@
use super::*;
use toml;
use log::debug;

/// TomlAdapter map a Toml file in a linear multilevel key/value array
///
Expand Down
6 changes: 3 additions & 3 deletions src/adapter/ayaml.rs
@@ -1,9 +1,9 @@
use super::*;
use serde_yaml;
use log::debug;

/// YamlAdapter map a Yaml file in a linear multilevel key/value array
///
/// the adaptor could be consumed by Viperous
/// the adaptor could be consumed by Viperous
pub struct YamlAdapter {
source: String,
data: serde_yaml::Mapping,
Expand Down Expand Up @@ -50,7 +50,7 @@ impl ConfigAdapter for YamlAdapter {
if let serde_yaml::Value::String(s) = k {
let kpath = s.to_owned();

rec_yaml(&mut res, &kpath, &v);
rec_yaml(&mut res, &kpath, v);
}
}

Expand Down

0 comments on commit 30d4479

Please sign in to comment.