diff --git a/Cargo.lock b/Cargo.lock index 4123ffd..333acac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "enve" -version = "0.2.0" +version = "0.3.0" dependencies = [ "estring", ] diff --git a/Cargo.toml b/Cargo.toml index 63c4254..508b5e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "enve" -version = "0.2.0" +version = "0.3.0" authors = ["Dmitriy Pleshevskiy "] description = "it helps you work with environment variables and convert it to any type using only type annotations" categories = ["config"] diff --git a/README.md b/README.md index 5f0193a..37a3cef 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ```toml [dependencies] -enve = "0.2" +enve = "0.3" ``` `enve` helps you work with environment variables and convert it to **any type** @@ -25,28 +25,60 @@ Look at the [examples] to see the power! ## Usage +Basic + +```rust +fn main() -> Result<(), enve::Error> { + enve::sset("E", "10"); + + let res: f32 = enve::get("E")?; + + println!("result: {}", res); + + Ok(()) +} +``` + +You can use predefined structs like `SepVec` if you enable `structs` feature. + +Note: You can use custom types as annotations! Just implement `ParseFragment`. + ```rust use enve::SepVec; -type MinusVec = SepVec; type PlusVec = SepVec; type MulVec = SepVec; fn main() -> Result<(), enve::Error> { - enve::sset("E", "10+5*2-3"); + enve::sset("E", "10+5*2+3"); - let res: f32 = enve::get::>>>("E") + let res: f32 = enve::get::>>("E") .unwrap() .iter() - .map(|p| { - p.iter() - .map(|m| m.iter().product::()) - .reduce(|acc, v| acc - v) - .unwrap_or_default() - }) + .map(|m| m.iter().product::()) .sum::(); - println!("result: {}", res); + assert_eq!(res, 23.0); + + Ok(()) +} +``` + +You can also use predefined aggregators if you enable `aggs` feature. + +```rust +use enve::{SepVec, Product, Sum, estring::Aggregate}; + +type PlusVec = SepVec; +type MulVec = SepVec; + +fn main() -> Result<(), enve::Error> { + enve::sset("E", "10+5*2+3"); + + let res: f32 = enve::get::>>>>("E")? + .agg(); + + assert_eq!(res, 23.0); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 8f22378..a900177 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,41 +3,75 @@ //! `enve` helps you work with environment variables and convert it to **any type** //! using only **type annotations**. //! -//! Look at the [examples](https://github.com/pleshevskiy/enve/tree/main/examples) -//! to see the power! -//! //! All standard environment variable types are included, but `enve` under the hood //! uses [estring](https://github.com/pleshevskiy/estring), so you can easily create //! your own type. //! -//! ## Getting started +//! ## Usage +//! +//! Basic //! //! ```rust -//! use enve::SepVec; -//! -//! type MinusVec = SepVec; -//! type PlusVec = SepVec; -//! type MulVec = SepVec; -//! //! fn main() -> Result<(), enve::Error> { -//! enve::sset("E", "10+5*2-3"); +//! enve::sset("E", "10"); //! -//! let res: f32 = enve::get::>>>("E") -//! .unwrap() -//! .iter() -//! .map(|p| { -//! p.iter() -//! .map(|m| m.iter().product::()) -//! .reduce(|acc, v| acc - v) -//! .unwrap_or_default() -//! }) -//! .sum::(); +//! let res: f32 = enve::get("E")?; //! //! println!("result: {}", res); //! //! Ok(()) //! } //! ``` +//! +//! You can use predefined structs like `SepVec` if you enable `structs` feature. +//! +//! Note: You can use custom types as annotations! Just implement `ParseFragment`. +//! +//! ```rust +//! use enve::SepVec; +//! +//! type PlusVec = SepVec; +//! type MulVec = SepVec; +//! +//! fn main() -> Result<(), enve::Error> { +//! enve::sset("E", "10+5*2+3"); +//! +//! let res = enve::get::>>("E")? +//! .iter() +//! .map(|m| m.iter().product::()) +//! .sum::(); +//! +//! assert_eq!(res, 23.0); +//! +//! Ok(()) +//! } +//! ``` +//! +//! You can also use predefined aggregators if you enable `aggs` feature. +//! +//! ```rust +//! use enve::{SepVec, Product, Sum, estring::Aggregate}; +//! +//! type PlusVec = SepVec; +//! type MulVec = SepVec; +//! +//! fn main() -> Result<(), enve::Error> { +//! enve::sset("E", "10+5*2+3"); +//! +//! let res = enve::get::>>>>("E")?.agg(); +//! +//! assert_eq!(res, 23.0); +//! +//! Ok(()) +//! } +//! ``` +//! +//! --- +//! +//! Look at the [examples] to see the power! +//! +//! [examples]: https://github.com/pleshevskiy/enve/tree/main/examples +//! // Rustc lints. #![forbid(unsafe_code)]