doc: add examples to readme
This commit is contained in:
parent
aecd3f9543
commit
039a0dd630
7 changed files with 82 additions and 3 deletions
36
README.md
36
README.md
|
@ -24,6 +24,23 @@ For more details, see [examples].
|
||||||
|
|
||||||
## Usage
|
## 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
|
```rust
|
||||||
use estring::{SepVec, EString};
|
use estring::{SepVec, EString};
|
||||||
|
|
||||||
|
@ -42,8 +59,23 @@ fn main() -> estring::Result<()> {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can use custom types as annotations! Just implement
|
You can also use predefined aggregators if you enable the `aggs` feature.
|
||||||
`estring::ParseFragment`!
|
|
||||||
|
```rust
|
||||||
|
use estring::{Aggregate, EString, Product, SepVec, Sum};
|
||||||
|
|
||||||
|
type PlusVec<T> = SepVec<T, '+'>;
|
||||||
|
type MulVec<T> = SepVec<T, '*'>;
|
||||||
|
|
||||||
|
fn main() -> estring::Result<()> {
|
||||||
|
let res = EString::from("10+5*2+3")
|
||||||
|
.parse::<Sum<PlusVec<Product<MulVec<f32>>>>>()?
|
||||||
|
.agg();
|
||||||
|
|
||||||
|
assert_eq!(res, 23.0);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Contact Us
|
## Contact Us
|
||||||
|
|
||||||
|
|
|
@ -240,6 +240,7 @@ impl ParseFragment for EString {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "aggs")]
|
||||||
impl Aggregatable for EString {
|
impl Aggregatable for EString {
|
||||||
type Item = Self;
|
type Item = Self;
|
||||||
|
|
||||||
|
@ -263,6 +264,7 @@ impl ToEString for String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "aggs")]
|
||||||
impl Aggregatable for String {
|
impl Aggregatable for String {
|
||||||
type Item = Self;
|
type Item = Self;
|
||||||
|
|
||||||
|
@ -286,6 +288,7 @@ impl<'a> ToEString for &'a str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "aggs")]
|
||||||
impl<'a> Aggregatable for &'a str {
|
impl<'a> Aggregatable for &'a str {
|
||||||
type Item = Self;
|
type Item = Self;
|
||||||
|
|
||||||
|
|
42
src/lib.rs
42
src/lib.rs
|
@ -7,7 +7,23 @@
|
||||||
//!
|
//!
|
||||||
//! [enve]: https://github.com/pleshevskiy/enve
|
//! [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
|
//! ```rust
|
||||||
//! use estring::{SepVec, EString};
|
//! 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<T> = SepVec<T, '+'>;
|
||||||
|
//! type MulVec<T> = SepVec<T, '*'>;
|
||||||
|
//!
|
||||||
|
//! fn main() -> estring::Result<()> {
|
||||||
|
//! let res = EString::from("10+5*2+3")
|
||||||
|
//! .parse::<Sum<PlusVec<Product<MulVec<f32>>>>>()?
|
||||||
|
//! .agg();
|
||||||
|
//!
|
||||||
|
//! assert_eq!(res, 23.0);
|
||||||
|
//! Ok(())
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ---
|
||||||
|
//!
|
||||||
|
//! For more details, see [examples].
|
||||||
|
//!
|
||||||
|
//! [examples]: https://github.com/pleshevskiy/estring/tree/main/examples
|
||||||
|
//!
|
||||||
#![deny(clippy::pedantic)]
|
#![deny(clippy::pedantic)]
|
||||||
#![allow(clippy::module_name_repetitions)]
|
#![allow(clippy::module_name_repetitions)]
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
|
@ -19,6 +19,7 @@ impl ToEString for bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "aggs")]
|
||||||
impl Aggregatable for bool {
|
impl Aggregatable for bool {
|
||||||
type Item = Self;
|
type Item = Self;
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ macro_rules! from_env_string_numbers_impl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "aggs")]
|
||||||
impl Aggregatable for $ty {
|
impl Aggregatable for $ty {
|
||||||
type Item = Self;
|
type Item = Self;
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "aggs")]
|
||||||
impl<T> Aggregatable for Option<T>
|
impl<T> Aggregatable for Option<T>
|
||||||
where
|
where
|
||||||
T: Aggregatable,
|
T: Aggregatable,
|
||||||
|
|
|
@ -92,6 +92,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "aggs")]
|
||||||
impl<T, const SEP: char> Aggregatable for SepVec<T, SEP>
|
impl<T, const SEP: char> Aggregatable for SepVec<T, SEP>
|
||||||
where
|
where
|
||||||
T: Aggregatable,
|
T: Aggregatable,
|
||||||
|
|
Reference in a new issue