require
only.Expand description
Ensure each requirement was met
Note: This module is experimental, other parts of this crate for the most part should have a stable api, this is an exception.
The idea behind this part of the crate is you can define requirements once and reuse them
across your project. A requirement is a higher-level concept than a condition in the context
of this crate. Unlike the condition combinatorics swift-check
exposes, requirements must have
been fulfilled at least once in the input, or they raise the error associated with them. This is
valuable for things like specifying password rules in a highly maintainable fashion.
§Example
use swift_check::{require::{Requirement, check}, requirement, requirements, eq};
enum MyError {
Space,
Seven
}
requirement!(
/// The input must include a space
pub space => eq(b' ') =>! MyError::Space
);
requirement!(
/// The input must include a seven
pub seven => eq(b'7') =>! MyError::Seven
);
// (you can use any condition exposed by swift-check, not just eq)
// now check if all the requirements were fulfilled, and that each byte in the input fell into
// at least one requirement (for validation)
let (valid, res) = check(
b"example input 7",
// as long as each requirement's error implements `Into<MyError>` you can use it.
requirements!(MyError, [space, seven])
).result(); // fails fast, there's also `results()` which allows you to iterate over the results
// not all the example input was 7 or space, so valid is false
assert!(!valid);
// there was a space and 7 in the input so the res is Ok
assert!(res.is_ok())
§Performance
This is the slowest part of the api due to the added complexity to facilitate the desired
functionality. It is not slow, but it is not swift, there’s a lot of room for optimization so if
require
remains a part of the api it will overtime become more and more performant.
Structs§
- ErrMsg
- The default error type meant for when you’re feeling lazy.
- Requires
Requires
is the final representation of a requirement, usable in therequirements!
macro.
Traits§
- Condition
- A
Condition
is used internally by theRequirement
trait.requirement!
will expand into a constant function which returns a type that implements this trait. - Requirement
- A trait representing a collection of conditions which must be met at least once.
Functions§
- check
- Check that all
requirement!
s are fulfilled