Skip to content

Commit fc4aa2e

Browse files
authored
Merge pull request #35 from teymour-aldridge/docs
Add some more documentation.
2 parents 85af1db + c1b526e commit fc4aa2e

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

fuzzcheck/src/mutators/filter.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@ use std::any::Any;
22

33
use crate::Mutator;
44

5+
/// A [`FilterMutator`] provides a way to filter values outputted by a mutator.
6+
/// Given any [`Mutator<Value=T>`] and a function [`Fn(&T) -> bool`] it creates
7+
/// a new mutator which can generate all the values of `T` the underlying
8+
/// mutator can, except those for which the filtering function returns false.
59
pub struct FilterMutator<M, F> {
6-
pub mutator: M,
7-
pub filter: F,
10+
mutator: M,
11+
filter: F,
812
}
13+
14+
impl<M, F> FilterMutator<M, F> {
15+
/// Creates a new [`FilterMutator`].
16+
///
17+
/// Note that the mutator will filter all values for which the filtering
18+
/// function returns _false_.
19+
pub fn new<T>(mutator: M, filter: F) -> FilterMutator<M, F>
20+
where
21+
M: Mutator<T>,
22+
T: Clone + 'static,
23+
F: Fn(&T) -> bool,
24+
Self: 'static,
25+
{
26+
FilterMutator { mutator, filter }
27+
}
28+
}
29+
930
impl<T, M, F> Mutator<T> for FilterMutator<M, F>
1031
where
1132
M: Mutator<T>,

fuzzcheck/src/mutators/grammar/grammar.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@ pub enum Grammar {
2323
pub fn regex(s: &str) -> Rc<Grammar> {
2424
grammar_from_regex(s)
2525
}
26+
2627
#[no_coverage]
28+
/// Creates an [`Rc<Grammar>`] which outputs characters in the given range.
29+
///
30+
/// For example, to generate characters in the range 'a' to 'z' (inclusive), one
31+
/// could use this code
32+
///
33+
/// ```
34+
/// let a_to_z = literal_ranges('a'..='z');
35+
/// ```
2736
pub fn literal_ranges(ranges: Vec<RangeInclusive<char>>) -> Rc<Grammar> {
2837
Rc::new(Grammar::Literal(ranges))
2938
}
39+
3040
#[no_coverage]
41+
/// Creates an [`Rc<Grammar>`] which matches a single character literal.
42+
///
43+
/// ```
44+
/// let l = literal('l');
45+
/// ```
3146
pub fn literal(l: char) -> Rc<Grammar> {
3247
Rc::new(Grammar::Literal(vec![l..=l]))
3348
}
@@ -48,15 +63,31 @@ where
4863
};
4964
Rc::new(Grammar::Literal(vec![start..=end]))
5065
}
66+
67+
/// Produces a grammar which will choose between the provided grammars.
5168
#[no_coverage]
5269
pub fn alternation(gs: impl IntoIterator<Item = Rc<Grammar>>) -> Rc<Grammar> {
5370
Rc::new(Grammar::Alternation(gs.into_iter().collect()))
5471
}
72+
73+
/// Produces a grammar which will concatenate the output of all the provided
74+
/// grammars together, in order.
75+
///
76+
/// For example, the grammar
77+
/// ```
78+
/// concatenation([
79+
/// regex("fuzz"),
80+
/// regex("check")
81+
/// ])
82+
/// ```
83+
/// would output "fuzzcheck".
5584
#[no_coverage]
5685
pub fn concatenation(gs: impl IntoIterator<Item = Rc<Grammar>>) -> Rc<Grammar> {
5786
Rc::new(Grammar::Concatenation(gs.into_iter().collect()))
5887
}
88+
5989
#[no_coverage]
90+
/// Repeats the provided grammar some number of times in the given range.
6091
pub fn repetition<R>(gs: Rc<Grammar>, range: R) -> Rc<Grammar>
6192
where
6293
R: RangeBounds<usize>,
@@ -75,10 +106,19 @@ where
75106
}
76107

77108
#[no_coverage]
109+
/// Used to indicate a point of recursion to Fuzzcheck. Should be combined with
110+
/// [`recursive`].
111+
///
112+
/// See the module documentation ([`super`]) for an example on how to use it.
78113
pub fn recurse(g: &Weak<Grammar>) -> Rc<Grammar> {
79114
Rc::new(Grammar::Recurse(g.clone()))
80115
}
116+
81117
#[no_coverage]
118+
/// Creates a recursive grammar. This function should be combined with
119+
/// [`recurse`] to make recursive calls.
120+
///
121+
/// See the module documentation ([`super`]) for an example on how to use it.
82122
pub fn recursive(data_fn: impl Fn(&Weak<Grammar>) -> Rc<Grammar>) -> Rc<Grammar> {
83123
Rc::new(Grammar::Recursive(Rc::new_cyclic(|g| {
84124
Rc::try_unwrap(data_fn(g)).unwrap()

fuzzcheck/src/mutators/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ where
140140
#[no_coverage]
141141
fn filter<F>(self, filter: F) -> FilterMutator<Self, F>
142142
where
143-
F: Fn(&T) -> bool,
143+
F: Fn(&T) -> bool + 'static,
144144
{
145-
FilterMutator { mutator: self, filter }
145+
FilterMutator::new(self, filter)
146146
}
147147
/// Create a mutator which wraps `self` and transforms the values generated by `self`
148148
/// using the `map` closure. The second closure, `parse`, should apply the opposite

0 commit comments

Comments
 (0)