Skip to main content

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. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20use crate::types::RUMID;
21use nanoid::nanoid;
22use uuid::Uuid;
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
39pub fn id_to_uuid(id: &str) -> RUMID {
40    Uuid::parse_str(id).unwrap()
41}
42
43#[macro_export]
44macro_rules! rumtk_generate_id {
45    (  ) => {{
46        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
47
48        generate_id(RUMID_TYPE::SHORT, DEFAULT_ID_SIZE)
49    }};
50    ( $size:expr ) => {{
51        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
52
53        generate_id(RUMID_TYPE::SHORT, $size)
54    }};
55    ( $size:expr, $typ:expr ) => {{
56        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
57
58        generate_id($typ, $size)
59    }};
60}