liteboxfs 0.1.0

A modern POSIX filesystem in a SQLite database
Documentation
use std::iter;

use rand::{Rng, SeedableRng, rngs::SmallRng};

#[derive(Debug, Clone, Copy)]
pub enum Case {
    Lower,
    #[allow(dead_code)]
    Upper,
    #[allow(dead_code)]
    Mixed,
}

impl Case {
    fn charset(&self) -> &[u8] {
        match self {
            Case::Lower => b"abcdefghijklmnopqrstuvwxyz",
            Case::Upper => b"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
            Case::Mixed => b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        }
    }
}

pub fn random_string(len: usize, case: Case) -> String {
    let charset = case.charset();
    let mut rng = SmallRng::from_os_rng();

    iter::repeat_with(|| charset[rng.random_range(0..charset.len())] as char)
        .take(len)
        .collect::<String>()
}

#[allow(dead_code)]
pub fn random_buf(len: usize, case: Case) -> Vec<u8> {
    random_string(len, case).into_bytes()
}