Skip to main content

hegel/generators/
misc.rs

1use super::{BasicGenerator, Generator};
2use crate::cbor_utils::cbor_map;
3use ciborium::Value;
4
5/// Generate the unit value `()`.
6// nocov start
7pub fn unit() -> JustGenerator<()> {
8    just(())
9    // nocov end
10}
11
12/// Generator that always produces the same value. Created by [`just()`].
13pub struct JustGenerator<T> {
14    value: T,
15}
16
17impl<T: Clone + Send + Sync> Generator<T> for JustGenerator<T> {
18    fn as_basic(&self) -> Option<BasicGenerator<'_, T>> {
19        let value = self.value.clone();
20        Some(BasicGenerator::new(
21            cbor_map! {"type" => "constant", "value" => Value::Null},
22            move |_| value.clone(),
23        ))
24    }
25}
26
27/// Generate a constant value.
28pub fn just<T: Clone + Send + Sync>(value: T) -> JustGenerator<T> {
29    JustGenerator { value }
30}
31
32/// Generator for boolean values. Created by [`booleans()`].
33pub struct BoolGenerator;
34
35impl Generator<bool> for BoolGenerator {
36    fn as_basic(&self) -> Option<BasicGenerator<'_, bool>> {
37        Some(BasicGenerator::new(
38            cbor_map! {"type" => "boolean"},
39            super::deserialize_value,
40        ))
41    }
42}
43
44/// Generate boolean values.
45pub fn booleans() -> BoolGenerator {
46    BoolGenerator
47}