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
use std::{
    mem,
    time::{UNIX_EPOCH, SystemTime},
};
#[cfg(unix)]
use std::{
    fs::OpenOptions,
    io::{Read, Write},
    path::PathBuf,
};
const RANDOM_BYTE_COUNT: usize = mem::size_of::<u64>();
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub enum Level {
    
    Low,
    
    Normal,
    
    Medium,
    
    High,
    
    Critical,
}
impl Level {
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    pub fn hash(&self) -> String {
        let bytes = rand_bytes();
        let limit: usize = match *self {
            Level::Low => 1,
            Level::Normal => 2,
            Level::Medium => 4,
            Level::High => 6,
            Level::Critical => 8,
        };
        let mut result = String::with_capacity({
            let separators = (limit / 2).saturating_sub(1);
            limit.saturating_mul(2).saturating_add(separators)
        });
        for (index, byte) in bytes[..bytes.len().min(limit)].iter().enumerate() {
            if index > 0 && index % 2 == 0 {
                result.push('-');
            }
            result.push_str(&format!("{:02x}", byte));
        }
        result
    }
}
fn rand_bytes_from_current_time() -> [u8; RANDOM_BYTE_COUNT] {
    let value = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| {
        let mut result = 1_u128;
        for v in [d.subsec_millis(), d.subsec_micros(), d.subsec_nanos()].iter() {
            if let Some(v) = result.checked_mul(*v as u128) {
                result = v;
            }
        }
        result.checked_mul(d.as_secs() as u128).unwrap_or_else(|| result.checked_add(d.as_secs() as u128).unwrap_or(result))
    })
        .unwrap_or_else(|_| u128::max_value());
    
    let value = ((value % u64::max_value() as u128) as u64).to_le();
    value.to_ne_bytes()
}
#[cfg(not(unix))]
fn rand_bytes() -> [u8; RANDOM_BYTE_COUNT] {
    rand_bytes_from_current_time()
}
#[cfg(unix)]
fn rand_bytes() -> [u8; RANDOM_BYTE_COUNT] {
    use ::std::os::unix::fs::FileTypeExt;
    const PATH: &'static str = "/dev/urandom";
    let some_bytes = rand_bytes_from_current_time();
    match PathBuf::from(PATH).metadata().map(|m| m.file_type()) {
        Ok(file_type) => match
            file_type.is_dir() == false && file_type.is_file() == false && file_type.is_char_device() && file_type.is_socket() == false
        {
            true => match OpenOptions::new().read(true).write(true).open(PATH) {
                Ok(mut file) => {
                    let mut result = [0_u8; RANDOM_BYTE_COUNT];
                    match file.read_exact(&mut result) {
                        Ok(()) => {
                            
                            match file.write_all(&some_bytes) {
                                Ok(()) => if cfg!(test) {
                                    __p!("Sent some bytes to {:?}", PATH);
                                },
                                Err(err) => __e!("Couldn't write to {:?} -> {}", PATH, &err),
                            };
                            result
                        },
                        Err(_) => some_bytes,
                    }
                },
                Err(_) => some_bytes,
            },
            false => {
                __e!("Malformed {:?}", PATH);
                some_bytes
            },
        },
        Err(_) => some_bytes,
    }
}
#[test]
fn test_levels() {
    assert!(u128::max_value() as f64 > u64::max_value() as f64 * 1e3 * 1e6 * 1e9);
    assert_eq!(rand_bytes().len(), RANDOM_BYTE_COUNT);
}