Enum generator_combinator::Generator
source · [−]pub enum Generator {
Show 15 variants
AlphaLower,
AlphaUpper,
Digit,
AlphaNumLower,
AlphaNumUpper,
HexUpper,
HexLower,
Char(char),
Str(String),
OneOf {
v: Vec<Generator>,
is_optional: bool,
},
RepeatedN(Box<Generator>, usize),
RepeatedMN(Box<Generator>, usize, usize),
Sequence(Vec<Generator>),
Transform {
inner: Box<Generator>,
transform_fn: TransformFn,
},
Empty,
}
Expand description
The building block of generator-combinators.
A Generator
can be constructed from strings, chars, and slices:
use generator_combinator::Generator;
let foo = Generator::from("foo"); // generates the string `foo`
let dot = Generator::from('.'); // generates the string `.`
let countries = Generator::from(&["US", "FR", "NZ", "CH"][..]); // generates strings `US`, `FR`, `NZ`, and `CH`.
Individual Generator
s can be combined as sequences with +
, as variants with |
, and with repetition with * usize
and * (usize, usize)
:
use generator_combinator::Generator;
let foo = Generator::from("foo");
let bar = Generator::from("bar");
let foobar = foo.clone() + bar.clone(); // generates `foobar`
let foo_or_bar = foo.clone() | bar.clone(); // generates `foo`, `bar`
let foo_or_bar_x2 = foo_or_bar.clone() * 2; // generates `foofoo`, `foobar`, `barfoo`, `barbar`
let foo_x2_to_x4 = foo.clone() * (2, 4); // generates `foofoo`, `foofoofoo`, `foofoofoofoo`
Variants
AlphaLower
Lowercase ASCII letters (a-z)
AlphaUpper
Uppercase ASCII letters (A-Z)
Digit
Base-10 digits (0-9)
AlphaNumLower
Lowercase ASCII letters and digits (a-z0-9)
AlphaNumUpper
Uppercase ASCII letters and digits (A-Z0-9)
HexUpper
Uppercase hexadecimal values (0-9A-F)
HexLower
Lowercase hexadecimal values (0-9a-f)
Char(char)
Generates a char
literal.
Str(String)
Generates a String
literal.
Note that this is not a character class.
Str("foo".into())
generates the exact string "foo"
OneOf
A choice between two or more patterns
As a regex, this would be, eg, (a|b|c)?
(depending on is_optional
)
RepeatedN(Box<Generator>, usize)
A pattern repeated exactly n times. This is the same as RepeatedMN
(a, n, n)
As a regex, this would be a{n}
RepeatedMN(Box<Generator>, usize, usize)
A pattern repeated at least m times, as many as n times.
As a regex, this would be a{m,n}
Sequence(Vec<Generator>)
Two or more sequential patterns.
As a regex, this would be, eg, abc
Transform
Wrap the current generator in a user-defined transformation.
Empty
Doesn’t generate anything
Implementations
sourceimpl Generator
impl Generator
sourcepub fn regex(&self) -> String
pub fn regex(&self) -> String
Create a regular expression that represents the patterns generated.
The result here is currently best-guess. It’s not guaranteed valid, correct, idiomatic, etc.
sourcepub fn generate_one(&self, num: u128) -> String
pub fn generate_one(&self, num: u128) -> String
Generates the String
encoded by the specified num
.
Panics if num
exceeds the length given by Generator::len
sourcepub fn optional(self) -> Self
pub fn optional(self) -> Self
Makes this Generator
optional.
As a regex, this is the ?
operator.
sourcepub fn generate_all(&self) -> StringIter<'_>ⓘNotable traits for StringIter<'a>impl<'a> Iterator for StringIter<'a> type Item = String;
pub fn generate_all(&self) -> StringIter<'_>ⓘNotable traits for StringIter<'a>impl<'a> Iterator for StringIter<'a> type Item = String;
Provides an iterator across all possible values for this Generator
.
sourcepub fn transform(self, f: fn(_: String) -> String) -> Self
pub fn transform(self, f: fn(_: String) -> String) -> Self
Includes a user-defined transformation when generating values.
sourcepub fn visit_one<F>(&self, num: u128, cb: F) where
F: FnMut(String),
pub fn visit_one<F>(&self, num: u128, cb: F) where
F: FnMut(String),
For a value specified by num
, applies the callback cb
for each of the component values
for this Generator.
This may be preferable to [generate_one
] if you want to see the individual components
comprising the final string and/or if you want to avoid the memory allocation and freeing
by creating the values.
Trait Implementations
sourceimpl Add<Generator> for Generator
impl Add<Generator> for Generator
Add operator for exact repetitions.
The following expressions are equivalent:
use generator_combinator::Generator;
let foostr = Generator::from("foofoo");
let foomul = Generator::from("foo") * 2;
let fooadd = Generator::from("foo") + Generator::from("foo");
sourceimpl AddAssign<&'_ str> for Generator
impl AddAssign<&'_ str> for Generator
sourcefn add_assign(&mut self, rhs: &str)
fn add_assign(&mut self, rhs: &str)
Performs the +=
operation. Read more
sourceimpl AddAssign<Generator> for Generator
impl AddAssign<Generator> for Generator
sourcefn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the +=
operation. Read more
sourceimpl AddAssign<String> for Generator
impl AddAssign<String> for Generator
sourcefn add_assign(&mut self, rhs: String)
fn add_assign(&mut self, rhs: String)
Performs the +=
operation. Read more
sourceimpl AddAssign<char> for Generator
impl AddAssign<char> for Generator
sourcefn add_assign(&mut self, rhs: char)
fn add_assign(&mut self, rhs: char)
Performs the +=
operation. Read more
sourceimpl BitOrAssign<&'_ str> for Generator
impl BitOrAssign<&'_ str> for Generator
sourcefn bitor_assign(&mut self, rhs: &str)
fn bitor_assign(&mut self, rhs: &str)
Performs the |=
operation. Read more
sourceimpl BitOrAssign<Generator> for Generator
impl BitOrAssign<Generator> for Generator
sourcefn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
Performs the |=
operation. Read more
sourceimpl BitOrAssign<String> for Generator
impl BitOrAssign<String> for Generator
sourcefn bitor_assign(&mut self, rhs: String)
fn bitor_assign(&mut self, rhs: String)
Performs the |=
operation. Read more
sourceimpl BitOrAssign<char> for Generator
impl BitOrAssign<char> for Generator
sourcefn bitor_assign(&mut self, rhs: char)
fn bitor_assign(&mut self, rhs: char)
Performs the |=
operation. Read more
sourceimpl<'a> From<&'a Generator> for StringIter<'a>
impl<'a> From<&'a Generator> for StringIter<'a>
sourceimpl Mul<(usize, usize)> for Generator
impl Mul<(usize, usize)> for Generator
Mul operator for repetitions between m
and n
(inclusive)
use generator_combinator::Generator;
let foo_two_to_five_times = Generator::from("foo") * (2,5);
sourceimpl Mul<usize> for Generator
impl Mul<usize> for Generator
Mul operator for exact repetitions.
The following expressions are equivalent:
use generator_combinator::Generator;
let foostr = Generator::from("foofoo");
let foomul = Generator::from("foo") * 2;
let fooadd = Generator::from("foo") + Generator::from("foo");
sourceimpl MulAssign<usize> for Generator
impl MulAssign<usize> for Generator
sourcefn mul_assign(&mut self, rhs: usize)
fn mul_assign(&mut self, rhs: usize)
Performs the *=
operation. Read more
impl StructuralPartialEq for Generator
Auto Trait Implementations
impl RefUnwindSafe for Generator
impl Send for Generator
impl Sync for Generator
impl Unpin for Generator
impl UnwindSafe for Generator
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more