Enum 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 Generators 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)

Fields

§is_optional: bool
§

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.

Fields

§transform_fn: TransformFn
§

Empty

Doesn’t generate anything

Implementations§

Source§

impl Generator

Source

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.

Source

pub fn len(&self) -> u128

The number of possible patterns represented.

Source

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

Source

pub fn optional(self) -> Self

Makes this Generator optional.

As a regex, this is the ? operator.

Source

pub fn generate_all(&self) -> StringIter<'_>

Provides an iterator across all possible values for this Generator.

Source

pub fn transform(self, f: fn(String) -> String) -> Self

Includes a user-defined transformation when generating values.

Source

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§

Source§

impl Add<&str> for Generator

Source§

type Output = Generator

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &str) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<String> for Generator

Source§

type Output = Generator

The resulting type after applying the + operator.
Source§

fn add(self, rhs: String) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<char> for Generator

Source§

type Output = Generator

The resulting type after applying the + operator.
Source§

fn add(self, rhs: char) -> Self::Output

Performs the + operation. Read more
Source§

impl Add 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");
Source§

type Output = Generator

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<&str> for Generator

Source§

fn add_assign(&mut self, rhs: &str)

Performs the += operation. Read more
Source§

impl AddAssign<String> for Generator

Source§

fn add_assign(&mut self, rhs: String)

Performs the += operation. Read more
Source§

impl AddAssign<char> for Generator

Source§

fn add_assign(&mut self, rhs: char)

Performs the += operation. Read more
Source§

impl AddAssign for Generator

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl BitOr<&str> for Generator

Source§

type Output = Generator

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &str) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<String> for Generator

Source§

type Output = Generator

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: String) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<char> for Generator

Source§

type Output = Generator

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: char) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr for Generator

Source§

type Output = Generator

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOrAssign<&str> for Generator

Source§

fn bitor_assign(&mut self, rhs: &str)

Performs the |= operation. Read more
Source§

impl BitOrAssign<String> for Generator

Source§

fn bitor_assign(&mut self, rhs: String)

Performs the |= operation. Read more
Source§

impl BitOrAssign<char> for Generator

Source§

fn bitor_assign(&mut self, rhs: char)

Performs the |= operation. Read more
Source§

impl BitOrAssign for Generator

Source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
Source§

impl Clone for Generator

Source§

fn clone(&self) -> Generator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Generator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Generator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> From<&[T]> for Generator
where T: AsRef<str> + Display,

Source§

fn from(values: &[T]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Generator> for StringIter<'a>

Source§

fn from(c: &'a Generator) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Generator

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Generator

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<char> for Generator

Source§

fn from(c: char) -> Self

Converts to this type from the input type.
Source§

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);
Source§

type Output = Generator

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: (usize, usize)) -> Self::Output

Performs the * operation. Read more
Source§

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");
Source§

type Output = Generator

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: usize) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<(usize, usize)> for Generator

Source§

fn mul_assign(&mut self, rhs: (usize, usize))

Performs the *= operation. Read more
Source§

impl MulAssign<usize> for Generator

Source§

fn mul_assign(&mut self, rhs: usize)

Performs the *= operation. Read more
Source§

impl PartialEq for Generator

Source§

fn eq(&self, other: &Generator) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Generator

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.