bitbelay_providers/
lib.rs

1//! Data providers for `bitbelay`.
2
3use clap::ValueEnum;
4
5pub mod ascii;
6pub mod numeric;
7
8/// The number of bits for a _short_ length data provider.
9const SHORT_BITS: usize = 3;
10
11/// The number of bits for a _medium_ length data provider.
12const MEDIUM_BITS: usize = 6;
13
14/// The number of bits for a _long_ length data provider.
15const LONG_BITS: usize = 12;
16
17/// A data provider for a hash function.
18pub trait Provider: std::fmt::Debug {
19    /// The name of the provider.
20    fn name(&self) -> &str;
21
22    /// Provides data by specifying the number of desired results (not bytes).
23    fn provide(&mut self, n: usize) -> Vec<&[u8]>;
24
25    /// The number of bytes per data provided.
26    fn bytes_per_input(&mut self) -> usize;
27}
28
29/// A list of possible providers.
30#[derive(Clone, Debug, Default, ValueEnum)]
31pub enum AvailableProviders {
32    /// A medium ASCII alphanumeric string.
33    #[clap(name = "ascii-alphanumeric")]
34    #[default]
35    ASCIIAlphanumeric,
36
37    /// A long ASCII alphanumeric string.
38    #[clap(name = "ascii-alphanumeric-long")]
39    ASCIIAlphanumericLong,
40
41    /// A short ASCII alphanumeric string.
42    #[clap(name = "ascii-alphanumeric-short")]
43    ASCIIAlphanumericShort,
44
45    /// A medium array of `u64`s.
46    #[clap(name = "u64")]
47    U64,
48
49    /// A long array of `u64`s.
50    #[clap(name = "u64-long")]
51    U64Long,
52
53    /// A short array of `u64`s.
54    #[clap(name = "u64-short")]
55    U64Short,
56}
57
58impl std::fmt::Display for AvailableProviders {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            AvailableProviders::ASCIIAlphanumeric => write!(f, "ascii-alphanumeric"),
62            AvailableProviders::ASCIIAlphanumericLong => write!(f, "ascii-alphanumeric-long"),
63            AvailableProviders::ASCIIAlphanumericShort => write!(f, "ascii-alphanumeric-short"),
64            AvailableProviders::U64 => write!(f, "u64"),
65            AvailableProviders::U64Long => write!(f, "u64-long"),
66            AvailableProviders::U64Short => write!(f, "u64-short"),
67        }
68    }
69}
70
71impl From<AvailableProviders> for Box<dyn Provider> {
72    fn from(provider: AvailableProviders) -> Self {
73        match provider {
74            // ASCII alphanumeric-based providers.
75            AvailableProviders::ASCIIAlphanumeric => {
76                Box::new(ascii::AlphanumericProvider::new(1 << MEDIUM_BITS))
77            }
78            AvailableProviders::ASCIIAlphanumericLong => {
79                Box::new(ascii::AlphanumericProvider::new(1 << LONG_BITS))
80            }
81            AvailableProviders::ASCIIAlphanumericShort => {
82                Box::new(ascii::AlphanumericProvider::new(1 << SHORT_BITS))
83            }
84
85            // `u64`-based providers.
86            AvailableProviders::U64 => {
87                Box::new(numeric::Unsigned64BitProvider::new(1 << MEDIUM_BITS))
88            }
89            AvailableProviders::U64Long => {
90                Box::new(numeric::Unsigned64BitProvider::new(1 << LONG_BITS))
91            }
92            AvailableProviders::U64Short => {
93                Box::new(numeric::Unsigned64BitProvider::new(1 << SHORT_BITS))
94            }
95        }
96    }
97}