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