rumtk_core/
core.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::strings::rumtk_format;
22use crate::strings::RUMString;
23pub use smallvec::{smallvec, SmallVec};
24
25///
26/// Type used for propagating error messages.
27///
28pub type RUMResult<T> = Result<T, RUMString>;
29
30pub type RUMVec<T> = Vec<T>;
31
32pub fn is_unique<T: std::cmp::Eq + std::hash::Hash>(data: &Vec<T>) -> bool {
33    let mut keys = ahash::AHashSet::with_capacity(data.len());
34    for itm in data {
35        if !keys.insert(itm) {
36            return false;
37        }
38    }
39    true
40}
41
42///
43/// Take a requested index and the maximum size of the item container.
44/// Check if the index is valid and return an error if it is.
45/// The purpose of this function is to enable handling of out of bounds without triggering a panic.
46/// Also, add negative indices like Python does when doing a reverse search!
47///
48/// * If the index is 0, return Error
49/// * If the index is below 0, return the max - index iff max - index > 0
50/// * If the index is bigger than the defined max, return Error.
51/// * Otherwise, return the given index.
52///
53/// # Examples
54///
55/// ## Min
56/// ```
57/// use ::rumtk_core::core::clamp_index;
58/// use ::rumtk_core::strings::rumtk_format;
59/// let max: isize = 5;
60/// let i: isize = 1;
61/// let result = clamp_index(&i, &max).unwrap();
62/// assert_eq!(&1, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
63/// ```
64///
65/// ## Max
66/// ```
67/// use ::rumtk_core::core::clamp_index;
68/// use ::rumtk_core::strings::rumtk_format;
69/// let max: isize = 5;
70/// let i: isize = 5;
71/// let result = clamp_index(&i, &max).unwrap();
72/// assert_eq!(&5, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
73/// ```
74///
75/// ## Valid
76/// ```
77/// use ::rumtk_core::core::clamp_index;
78/// use ::rumtk_core::strings::rumtk_format;
79/// let max: isize = 5;
80/// let i: isize = 5;
81/// let result = clamp_index(&i, &max).unwrap();
82/// assert_eq!(&5, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
83/// ```
84///
85/// ## Valid Negative Index (reverse lookup)
86/// ```
87/// use ::rumtk_core::core::clamp_index;
88/// use ::rumtk_core::strings::rumtk_format;
89/// let max: isize = 5;
90/// let i: isize = -1;
91/// let result = clamp_index(&i, &max).unwrap();
92/// assert_eq!(&5, &result, "{}", rumtk_format!("Expected to receive 0 but got {}", &result))
93/// ```
94#[inline(always)]
95pub fn clamp_index(given_indx: &isize, max_size: &isize) -> RUMResult<usize> {
96    let neg_max_indx = *max_size * -1;
97    if *given_indx == 0 {
98        return Err(rumtk_format!(
99            "Index {} is invalid! Use 1-indexed values if using positive indices.",
100            given_indx
101        ));
102    }
103
104    if *given_indx >= neg_max_indx && *given_indx < 0 {
105        return Ok((max_size + given_indx + 1) as usize);
106    }
107
108    if *given_indx > 0 && given_indx <= max_size {
109        return Ok(*given_indx as usize);
110    }
111
112    Err(rumtk_format!(
113        "Index {} is outside {} < x < {} boundary!",
114        given_indx,
115        neg_max_indx,
116        max_size
117    ))
118}