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.
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;
23use uuid::Uuid;
24
25pub const DEFAULT_ID_SIZE: usize = 16;
26pub enum RUMID_TYPE {
27    SHORT,
28    None,
29}
30
31pub fn generate_id(typ: RUMID_TYPE, size: usize) -> String {
32    match typ {
33        RUMID_TYPE::SHORT => {
34            nanoid!(size)
35        }
36        RUMID_TYPE::None => RUMID::new_v4().to_string(),
37    }
38}
39
40pub fn id_to_uuid(id: &str) -> RUMID {
41    Uuid::parse_str(id).unwrap()
42}
43
44#[macro_export]
45macro_rules! rumtk_generate_id {
46    (  ) => {{
47        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
48
49        generate_id(RUMID_TYPE::SHORT, DEFAULT_ID_SIZE)
50    }};
51    ( $size:expr ) => {{
52        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
53
54        generate_id(RUMID_TYPE::SHORT, $size)
55    }};
56    ( $size:expr, $typ:expr ) => {{
57        use $crate::id::{generate_id, DEFAULT_ID_SIZE, RUMID_TYPE};
58
59        generate_id($typ, $size)
60    }};
61}