hoicko_lib 0.1.16

Hoicko library
Documentation
use nanoid::nanoid;
use serde::{Deserialize, Serialize};

pub struct RedisKeys;

impl RedisKeys {
    pub const USER_KEY: &'static str = "hoicko:alpha_users:";
    pub const BOARD_KEY: &'static str = "hoicko:alpha_board:";
    pub const BOARD_META_KEY: &'static str = "hoicko:alpha_board_meta:";
}
const ID_CHARS: &[char] = &[
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
    'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 
    'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
    'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
    'Y', 'Z'
];
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IDPrefix {
    Folder,
    WorkSpace,
    Board,
    None,
    Row,
    View,
    Column,
    Option,
    WebHook,
    OBAC,
    ObacNode
}

impl IDPrefix {
    #[inline]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::WebHook => "wbh",
            Self::ObacNode => "obno",
            Self::OBAC => "obsc",
            Self::WorkSpace => "wrk",
            Self::Folder => "fod",
            Self::Column => "fld",
            Self::Board => "brd",
            Self::None => "",
            Self::Row => "row",
            Self::Option => "opt",
            Self::View => "viw",
        }
    }

    pub fn generate_id(&self, length: Option<usize>) -> String {
        let id_length = length.unwrap_or(12);
        format!("{}{}", self.as_str(), nanoid!(id_length,ID_CHARS))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_id_generation() {
        let folder = IDPrefix::Folder;
        let id = folder.generate_id(Some(10));
        assert!(id.starts_with("fod"));
        assert_eq!(id.len(), 13); // "fod" + 10 chars
    }

    #[test]
    fn test_different_prefixes() {
        let test_cases = vec![
            (IDPrefix::Board, "brd"),
            (IDPrefix::Column, "fld"),
            (IDPrefix::Option, "opt"),
        ];

        for (prefix, expected) in test_cases {
            let id = prefix.generate_id(Some(5));
            assert!(id.starts_with(expected));
            assert_eq!(id.len(), expected.len() + 5);
        }
    }
}