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
pub trait ToCharset {
    fn to_chars(&self) -> Vec<char>;
}

impl ToCharset for String {
    fn to_chars(&self) -> Vec<char> {
        self.chars().into_iter().collect()
    }
}

impl ToCharset for &str {
    fn to_chars(&self) -> Vec<char> {
        self.chars().into_iter().collect()
    }
}


/// A set of specific chars to use in generation
#[derive(Debug)]
pub struct Charset {
    length: usize,
    body: Vec<char>,
}

impl Charset {
    /// Returns a charset with a given body as &str or String
    /// Will return None only if the given body is empty
    ///
    /// # Arguments
    ///
    /// * `charset` - Body of the Charset
    ///
    /// # Examples
    ///
    /// ```
    /// use random_string::Charset;
    /// let charset = Charset::new("name");
    /// ```
    pub fn new<T>(charset: T) -> Option<Self>
    where T: ToCharset {
        let chars = charset.to_chars();
        let len = chars.len();

        // let mut buf = BytesMut::with_capacity(chars.len());
        // buf.put(chars.as_bytes());

        if len == 0 {
            return None
        }

        Some( Self {
            length: len,
            body: chars,
        })
    }

    pub fn length(&self) -> usize {
        self.length
    }

    pub fn borrow_body(&self) -> &Vec<char> {
        &self.body
    }

    pub fn clone_body(&self) -> Vec<char> {
        self.body.clone()
    }

    pub fn to_string(&self) -> String {
        let mut result = String::new();
        for ch in self.body.iter() {
            result.push(*ch)
        }
        result
    }
}