Expand description
Semigroup trait is useful for combining multiple elements.
For example:
This crate enables you to derive Semigroup
and provides many practical implementations.
§Usage
cargo add semigroup --features derive,monoid§Examples
A CLI example of clap and serde integration, see https://github.com/hayas1/semigroup/blob/master/semigroup/examples/clap_serde.rs
§Simple coalesce
use semigroup::Semigroup;
#[derive(Debug, Clone, PartialEq, Semigroup)]
#[semigroup(with = "semigroup::op::Coalesce")]
pub struct Config<'a> {
pub num: Option<u32>,
pub str: Option<&'a str>,
#[semigroup(with = "semigroup::op::Overwrite")]
pub boolean: bool,
}
let cli = Config { num: Some(1), str: None, boolean: true };
let file = Config { num: None, str: Some("ten"), boolean: false };
let env = Config { num: Some(100), str: None, boolean: false };
let config = cli.semigroup(file).semigroup(env);
assert_eq!(config, Config { num: Some(1), str: Some("ten"), boolean: false });§Coalesce with rich enum annotation and lazy evaluation
More detail is in Annotate and Lazy.
use semigroup::{Annotate, Lazy, Semigroup};
#[derive(Debug, Clone, PartialEq, Semigroup)]
#[semigroup(annotated, with = "semigroup::op::Coalesce")]
pub struct Config<'a> {
pub num: Option<u32>,
pub str: Option<&'a str>,
#[semigroup(with = "semigroup::op::Overwrite")]
pub boolean: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Source {
File,
Env,
Cli,
}
let cli = Config { num: Some(1), str: None, boolean: true }.annotated(Source::Cli);
let file = Config { num: None, str: Some("ten"), boolean: false }.annotated(Source::File);
let env = Config { num: Some(100), str: None, boolean: false }.annotated(Source::Env);
let lazy = Lazy::from(cli).semigroup(file.into()).semigroup(env.into());
assert_eq!(lazy.first().value(), &Config { num: Some(1), str: None, boolean: true });
assert_eq!(lazy.last().value(), &Config { num: Some(100), str: None, boolean: false });
let config = lazy.combine();
assert_eq!(config.value(), &Config { num: Some(1), str: Some("ten"), boolean: false });
assert_eq!(config.annotation().num, Source::Cli);
assert_eq!(config.annotation().str, Source::File);
assert_eq!(config.annotation().boolean, Source::Env);§Highlights
#[derive(Semigroup)]and#[derive(Construction)]- derive
Semigroupimplements semigroup for a struct by field level semantics. - derive
Constructiondefines a new semigroup operation (Some operations are already defined incrate::op).
- derive
- Practical annotation support
- Some semigroup operations such as
op::Coalescecan have an annotation that is represented byAnnotatetrait.
- Some semigroup operations such as
- Combine multiple elements
CombineIteratorprovides fold and combine operations for iterators.Lazyprovides lazy evaluation.segment_tree::SegmentTreeis useful for fast range queries onMonoid.
Semigroup | Annotate | Monoid | Commutative | |
|---|---|---|---|---|
| property | associativity | annotation | identity element | commutativity |
#[derive(Semigroup)] #[semigroup(...)] | annotated | monoid | commutative | |
#[derive(Construction)] #[construction(...)] | annotated | monoid | commutative | |
| testing | assert_semigroup! | assert_monoid! | assert_commutative! | |
| typical combiner | CombineIterator | Lazy | SegmentTree | CombineStream |
§Links
- GitHub: https://github.com/hayas1/semigroup
- GitHub Pages: https://hayas1.github.io/semigroup/semigroup
- Release Notes: https://github.com/hayas1/semigroup/releases
- Crates.io: https://crates.io/crates/semigroup
- Docs.rs: https://docs.rs/semigroup
§Testing
§Benchmarks
// TODO
§Coverage
https://hayas1.github.io/semigroup/semigroup/tarpaulin-report.html
Modules§
- op
- segment_
tree monoid - test_
combine test - test_
commutative test - test_
lazy test - test_
monoid monoidandtest - test_
semigroup test
Macros§
- assert_
commutative - Assert that the given type satisfies the commutative property.
- assert_
monoid - Assert that the given type satisfies the monoid property.
- assert_
semigroup - Assert that the given type satisfies the semigroup property.
Structs§
- Annotated
Annotatedrepresents aSemigroupvalue with an annotation. The value will be annotated byAnnotatetrait.- Lazy
- A lazy evaluated
Semigroupwith nonempty buffer that is implemented byVec. - Option
Monoid monoid - Construct
Monoidfrom optionalSemigroup. SomeSemigrouplack a suitable identity element for extension to aMonoid. - Reverse
- A
Semigroupconstruction that flips the order of operands:op(Reverse(a), Reverse(b)) = Reverse(op(b, a)).
Traits§
- Annotate
- Some
Semigroupsuch ascrate::op::Coalescecan have an annotation. The annotated operation is represented byAnnotatedSemigroup, and the annotated value is represented by a typeAnnotated. - Annotated
Semigroup AnnotatedSemigroupis aSemigroupthat has an annotation, such ascrate::Annotate.- Async
Commutative async - Async version of
Commutative. - Async
Semigroup async - Async version of
Semigroup. - Combine
Iterator - Extensions for
Iterators that items implementSemigroup. - Combine
Stream async - Extensions for
Streams that items implementAsyncCommutative. Likecrate::CombineIterator. - Commutative
Commutativerepresents a binary operation that satisfies the following property- Construction
Constructionrepresentscrate::Semigroupas a new type struct.- Construction
Annotated ConstructionAnnotatedrepresentscrate::AnnotatedSemigroupas a new type struct likeConstruction.- Construction
Monoid monoid ConstructionMonoidrepresentscrate::Monoidas a new type struct. likeConstruction.- Monoid
monoid Monoidrepresents a binary operation that satisfies the following properties- Semigroup
Semigrouprepresents a binary operation that satisfies the following properties
Attribute Macros§
- properties
derive
Derive Macros§
- Construction
derive - Semigroup
derive