From 039a0dd63092a62cc30086b7669d96d318040c81 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 28 Jul 2022 14:28:09 +0300 Subject: [PATCH] doc: add examples to readme --- README.md | 36 ++++++++++++++++++++++++++++++++++-- src/core.rs | 3 +++ src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++++++- src/std/bool.rs | 1 + src/std/number.rs | 1 + src/std/option.rs | 1 + src/structs/sep_vec.rs | 1 + 7 files changed, 82 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ecd68e1..c0bec7e 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,23 @@ For more details, see [examples]. ## Usage +Basic + +```rust +use estring::EString; + +fn main() -> estring::Result<()> { + let res: i32 = EString::from("10").parse()?; + assert_eq!(res, 10); + Ok(()) +} +``` + +You can use predefined structs like `SepVec` if you enable the `structs` +feature. + +Note: You can use custom types as annotations! Just implement `ParseFragment`! + ```rust use estring::{SepVec, EString}; @@ -42,8 +59,23 @@ fn main() -> estring::Result<()> { } ``` -You can use custom types as annotations! Just implement -`estring::ParseFragment`! +You can also use predefined aggregators if you enable the `aggs` feature. + +```rust +use estring::{Aggregate, EString, Product, SepVec, Sum}; + +type PlusVec = SepVec; +type MulVec = SepVec; + +fn main() -> estring::Result<()> { + let res = EString::from("10+5*2+3") + .parse::>>>>()? + .agg(); + + assert_eq!(res, 23.0); + Ok(()) +} +``` ## Contact Us diff --git a/src/core.rs b/src/core.rs index 2d6bd74..054ac5f 100644 --- a/src/core.rs +++ b/src/core.rs @@ -240,6 +240,7 @@ impl ParseFragment for EString { } } +#[cfg(feature = "aggs")] impl Aggregatable for EString { type Item = Self; @@ -263,6 +264,7 @@ impl ToEString for String { } } +#[cfg(feature = "aggs")] impl Aggregatable for String { type Item = Self; @@ -286,6 +288,7 @@ impl<'a> ToEString for &'a str { } } +#[cfg(feature = "aggs")] impl<'a> Aggregatable for &'a str { type Item = Self; diff --git a/src/lib.rs b/src/lib.rs index 73d8637..bb90cbe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,23 @@ //! //! [enve]: https://github.com/pleshevskiy/enve //! -//! ## Getting started +//! ## Usage +//! +//! Basic +//! +//! ```rust +//! use estring::EString; +//! +//! fn main() -> estring::Result<()> { +//! let res: i32 = EString::from("10").parse()?; +//! assert_eq!(res, 10); +//! 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 estring::{SepVec, EString}; @@ -27,6 +43,30 @@ //! } //! ``` //! +//! You can also use predefined aggregators if you enable `aggs` feature. +//! +//! ```rust +//! use estring::{Aggregate, EString, Product, SepVec, Sum}; +//! +//! type PlusVec = SepVec; +//! type MulVec = SepVec; +//! +//! fn main() -> estring::Result<()> { +//! let res = EString::from("10+5*2+3") +//! .parse::>>>>()? +//! .agg(); +//! +//! assert_eq!(res, 23.0); +//! Ok(()) +//! } +//! ``` +//! +//! --- +//! +//! For more details, see [examples]. +//! +//! [examples]: https://github.com/pleshevskiy/estring/tree/main/examples +//! #![deny(clippy::pedantic)] #![allow(clippy::module_name_repetitions)] #![warn(missing_docs)] diff --git a/src/std/bool.rs b/src/std/bool.rs index 876f29b..bf01286 100644 --- a/src/std/bool.rs +++ b/src/std/bool.rs @@ -19,6 +19,7 @@ impl ToEString for bool { } } +#[cfg(feature = "aggs")] impl Aggregatable for bool { type Item = Self; diff --git a/src/std/number.rs b/src/std/number.rs index 2afae8c..63f8f66 100644 --- a/src/std/number.rs +++ b/src/std/number.rs @@ -19,6 +19,7 @@ macro_rules! from_env_string_numbers_impl { } } + #[cfg(feature = "aggs")] impl Aggregatable for $ty { type Item = Self; diff --git a/src/std/option.rs b/src/std/option.rs index be2e648..931ab66 100644 --- a/src/std/option.rs +++ b/src/std/option.rs @@ -25,6 +25,7 @@ where } } +#[cfg(feature = "aggs")] impl Aggregatable for Option where T: Aggregatable, diff --git a/src/structs/sep_vec.rs b/src/structs/sep_vec.rs index 166fc89..9413ad2 100644 --- a/src/structs/sep_vec.rs +++ b/src/structs/sep_vec.rs @@ -92,6 +92,7 @@ where } } +#[cfg(feature = "aggs")] impl Aggregatable for SepVec where T: Aggregatable,