crypto_random_map/
dense.rs

1//! doc me
2use convert_base::Convert;
3use std::{ops::Index, str};
4
5/// doc me
6#[derive(Debug)]
7pub struct SecretDense {
8    order: u64,
9    set: String,
10}
11
12impl SecretDense {
13    /// doc me
14    pub fn new(codes: &str) -> Self {
15        let order = codes.chars().count() as u64;
16        SecretDense { set: codes.to_string(), order }
17    }
18    /// doc me
19    pub fn encode(&self, v: &[u8]) -> Vec<char> {
20        let mut base = Convert::new(256, self.order);
21        let output = base.convert::<u8, u64>(&v.to_vec());
22        self.index_vec(output)
23    }
24    /// doc me
25    pub fn decode(&self, s: &str) -> Vec<u8> {
26        let c = s.chars().collect::<Vec<char>>();
27        let v = self.index_str(c);
28        let mut base = Convert::new(self.order, 256);
29        base.convert::<u64, u8>(&v.to_vec())
30    }
31}
32#[allow(unused_variables)]
33#[allow(unreachable_code)]
34impl Index<usize> for SecretDense {
35    type Output = Option<char>;
36    fn index(&self, index: usize) -> &Self::Output {
37        unimplemented!()
38    }
39}
40#[allow(unused_variables)]
41#[allow(unreachable_code)]
42impl Index<char> for SecretDense {
43    type Output = Option<usize>;
44    fn index(&self, index: char) -> &Self::Output {
45        unimplemented!()
46    }
47}
48
49impl SecretDense {
50    fn index_vec(&self, index: Vec<u64>) -> Vec<char> {
51        let mut result = vec![];
52        for i in index {
53            let c = self.set.chars().nth(i as usize).unwrap();
54            result.push(c)
55        }
56        return result;
57    }
58    fn index_str(&self, index: Vec<char>) -> Vec<u64> {
59        let mut result = vec![];
60        for i in index {
61            let u = self.set.chars().position(|c| c == i).unwrap();
62            result.push(u as u64)
63        }
64        return result;
65    }
66}