Crates.io | apply_if |
lib.rs | apply_if |
version | 1.2.1 |
source | src |
created_at | 2023-12-18 01:36:12.855258 |
updated_at | 2024-10-30 04:54:19.87698 |
description | Conditionally apply a closure to an item or return it |
homepage | |
repository | https://github.com/bavalpey/apply_if |
max_upload_size | |
id | 1072873 |
size | 49,736 |
This crate provides the functions apply_if
and apply_if_some
for consuming
builders, as well as apply_if_mut
and apply_if_some_mut
for mutable builder
chains. They allow us to conditionally invoke builder functions while keeping
the nice chaining syntax.
These examples use the derive_builder
crate to create a builder and show how to use the apply_if...
functions to
conditionally invoke builder functions without breaking the chain.
This example uses a mutable builder pattern, which is why apply_if_mut
and apply_if_some_mut
are used.
extern crate derive_builder;
use derive_builder::Builder;
use apply_if::ApplyIf;
#[derive(Debug,PartialEq,Builder)]
struct Value{
#[builder(default = 123)]
first: i32,
#[builder(default = 42.)]
second: f32,
}
fn main () {
// apply_if_mut only applies the function
// if the condition is true
let value = ValueBuilder::default()
.apply_if_mut(true, |builder| builder.first(100))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 100, second: 1.337});
let value = ValueBuilder::default()
.apply_if_mut(false, |builder| builder.first(100))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 123, second: 1.337});
// apply_if_some_mut only applies the function
// if the optional contains a value
let value = ValueBuilder::default()
.apply_if_some_mut(Some(100), |builder,val| builder.first(val))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 100, second: 1.337});
let value = ValueBuilder::default()
.apply_if_some_mut(None, |builder,val| builder.first(val))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 123, second: 1.337});
}
This example uses a consuming builder pattern, which is why apply_if
and apply_if_some
are used.
extern crate derive_builder;
use derive_builder::Builder;
use apply_if::ApplyIf;
#[derive(Debug,PartialEq,Builder)]
#[builder(pattern = "immutable")]
struct Value{
#[builder(default = 123)]
first: i32,
#[builder(default = 42.)]
second: f32,
}
fn main () {
// apply_if only applies the function
// if the condition is true
let value = ValueBuilder::default()
.apply_if(true, |builder| builder.first(100))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 100, second: 1.337});
let value = ValueBuilder::default()
.apply_if(false, |builder| builder.first(100))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 123, second: 1.337});
// apply_if_some only applies the function
// if the optional contains a value
let value = ValueBuilder::default()
.apply_if_some(Some(100), |builder,val| builder.first(val))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 100, second: 1.337});
let value = ValueBuilder::default()
.apply_if_some(None, |builder,val| builder.first(val))
.second(1.337)
.build()
.unwrap();
assert_eq!(value, Value{first: 123, second: 1.337});
}
The code in this repository is licensed under either the MIT License or the Apache License 2.0, at your option. Documentation, specifically the documentation comments and all markdown files, are licensed under the Creative Commons ShareAlike 4.0 license. Generated files are provided under CC0-1.0.