1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
pub mod bytes;
pub use bytes::BytesInput;
pub mod encoded;
pub use encoded::*;
pub mod gramatron;
pub use gramatron::*;
pub mod generalized;
pub use generalized::*;
#[cfg(feature = "nautilus")]
pub mod nautilus;
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use core::{clone::Clone, fmt::Debug, marker::PhantomData};
#[cfg(feature = "std")]
use std::{fs::File, hash::Hash, io::Read, path::Path};
#[cfg(feature = "nautilus")]
pub use nautilus::*;
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use crate::bolts::fs::write_file_atomic;
use crate::{bolts::ownedref::OwnedSlice, Error};
#[cfg(not(feature = "std"))]
pub trait Input: Clone + Serialize + serde::de::DeserializeOwned + Debug {
fn to_file<P>(&self, _path: P) -> Result<(), Error> {
Err(Error::not_implemented("Not supported in no_std"))
}
fn from_file<P>(_path: P) -> Result<Self, Error> {
Err(Error::not_implemented("Not supprted in no_std"))
}
fn generate_name(&self, idx: usize) -> String;
fn wrapped_as_testcase(&mut self) {}
}
#[cfg(feature = "std")]
pub trait Input: Clone + Serialize + serde::de::DeserializeOwned + Debug {
fn to_file<P>(&self, path: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
write_file_atomic(path, &postcard::to_allocvec(self)?)
}
fn from_file<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let mut file = File::open(path)?;
let mut bytes: Vec<u8> = vec![];
file.read_to_end(&mut bytes)?;
Ok(postcard::from_bytes(&bytes)?)
}
fn generate_name(&self, idx: usize) -> String;
fn wrapped_as_testcase(&mut self) {}
}
pub trait InputConverter: Debug {
type From: Input;
type To: Input;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error>;
}
#[macro_export]
macro_rules! none_input_converter {
() => {
None::<$crate::inputs::ClosureInputConverter<_, _>>
};
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug, Hash)]
pub struct NopInput {}
impl Input for NopInput {
fn generate_name(&self, _idx: usize) -> String {
"nop-input".to_string()
}
}
impl HasTargetBytes for NopInput {
fn target_bytes(&self) -> OwnedSlice<u8> {
OwnedSlice::from(vec![0])
}
}
pub trait HasTargetBytes {
fn target_bytes(&self) -> OwnedSlice<u8>;
}
pub trait HasBytesVec {
fn bytes(&self) -> &[u8];
fn bytes_mut(&mut self) -> &mut Vec<u8>;
}
pub trait UsesInput {
type Input: Input;
}
#[derive(Debug)]
pub struct NopInputConverter<I> {
phantom: PhantomData<I>,
}
impl<I> Default for NopInputConverter<I> {
fn default() -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<I> InputConverter for NopInputConverter<I>
where
I: Input,
{
type From = I;
type To = I;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error> {
Ok(input)
}
}
pub struct ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
convert_cb: Box<dyn FnMut(F) -> Result<T, Error>>,
}
impl<F, T> Debug for ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ClosureInputConverter")
.finish_non_exhaustive()
}
}
impl<F, T> ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
#[must_use]
pub fn new(convert_cb: Box<dyn FnMut(F) -> Result<T, Error>>) -> Self {
Self { convert_cb }
}
}
impl<F, T> InputConverter for ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
type From = F;
type To = T;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error> {
(self.convert_cb)(input)
}
}