rumtk_core/
id.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D.
5 * Copyright (C) 2025  MedicalMasses L.L.C.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21use crate::types::RUMID;
22use nanoid::nanoid;
23
24pub const DEFAULT_ID_SIZE: usize = 16;
25pub enum RUMID_TYPE {
26    SHORT,
27    None,
28}
29
30pub fn generate_id(typ: RUMID_TYPE, size: usize) -> String {
31    match typ {
32        RUMID_TYPE::SHORT => {
33            nanoid!(size)
34        }
35        RUMID_TYPE::None => RUMID::new_v4().to_string(),
36    }
37}
38
39#[macro_export]
40macro_rules! rumtk_generate_id {
41    (  ) => {{
42        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
43
44        generate_id(RUMID_TYPE::SHORT, DEFAULT_ID_SIZE)
45    }};
46    ( $size:expr ) => {{
47        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
48
49        generate_id(RUMID_TYPE::SHORT, $size)
50    }};
51    ( $size:expr, $typ:expr ) => {{
52        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
53
54        generate_id($typ, $size)
55    }};
56}