byte_mutator/mutators/mod.rs
1//! This module contains the available mutators.
2//!
3//! # Example
4//! ```
5//! use byte_mutator::mutators::Mutation;
6//! use byte_mutator::mutators::MutationType::BitFlipper;
7//!
8//! let bitflipper = Mutation{
9//! range: None,
10//! mutation: BitFlipper {width: 1}
11//! };
12//!
13//! let mut bytes = b"foo".to_vec();
14//!
15//! bitflipper.mutate(&mut bytes, 0);
16//!
17//! assert_eq!(&bytes, b"goo");
18//! ```
19
20use serde_derive::Deserialize;
21
22use crate::mutators::bitflipper::BitFlipper;
23
24pub mod bitflipper;
25
26#[derive(Debug, Deserialize, Clone)]
27pub enum MutationType {
28 /// Flips `width` number of bits.
29 BitFlipper { width: u8 },
30}
31
32#[derive(Debug, Deserialize, Clone)]
33/// A single mutation, optionally scoped to only operate on a subslice
34pub struct Mutation {
35 /// Optional subslice range (e.g. Some((0, 3)) only mutates the first three bytes)
36 pub range: Option<(usize, usize)>,
37 /// Type of mutator (e.g. MutatorType::BitFlipper)
38 pub mutation: MutationType,
39}
40
41impl Mutation {
42 /// Create a new `Mutation`, optionally scoped to operate only on a subslice
43 pub const fn new(mutator_type: MutationType, range: Option<(usize, usize)>) -> Self {
44 Self {
45 range,
46 mutation: mutator_type,
47 }
48 }
49
50 /// Execute the mutation
51 pub fn mutate(&self, bytes: &mut [u8], i: usize) {
52 match self.mutation {
53 MutationType::BitFlipper { width } => BitFlipper::mutate(bytes, i, width),
54 // todo: Closure type bitflipper?
55 }
56 }
57}