rumtk-core 0.20.2

Core library for providing general functionality to support the other RUMTK crates. See rumtk-hl7-v2 crate as example
Documentation
/*
 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
 *     Copyright (C) 2024  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
 *     Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
use crate::base::RUMResult;
use crate::regex::*;
use crate::rumtk_cache_fetch;
use crate::strings::RUMString;
use rumtk_arena::base::RUMVec;
use std::str::FromStr;
/**************************** Globals **************************************/
    /**************************** Constants**************************************/
    /**************************** Types *****************************************/

    /**************************** Traits ****************************************/

    /**************************** Helpers ***************************************/

    ///
    /// Finds all of the named regex captures and generates a hash table with the results assorted
    /// into key-value pairs. The keys are the names found in the regex expression. The value is
    /// the match corresponding to the named capture.
    ///
    /// This function returns an instance of SearchGroup which is the hash map.
    ///
    pub fn string_search_named_captures(input: &str, expr: &str) -> RUMResult<SearchGroups> {
        let key = hash_regex_expression(expr);
        let re: RUMRegex = rumtk_cache_fetch!(get_regex_cache(), &key, || {compile_regex(expr)})?;
        let names: Vec<&str> = re
            .capture_names()
            .skip(1)
            .map(|x| x.unwrap_or(""))
            .collect();
        let mut clean_names: Vec<&str> = Vec::with_capacity(names.len());
        let mut groups = SearchGroups::new();

        for name in &names {
            if !name.is_empty() {
                clean_names.push(name);
            }
        }

        if clean_names.is_empty() {
            return Ok(groups);
        }

        for cap in re.captures_iter(input).map(|c| c) {
            for name in &clean_names {
                let val = cap.name(name).map_or("", |s| s.as_str());
                if !val.is_empty() {
                    groups.insert(RUMString::from(name.to_string()), RUMString::from(val));
                }
            }
        }

        Ok(groups)
    }

    ///
    /// Finds all of the regex captures regardless of name status and compile them into a list
    /// of strings. Elsewhere, this provides a simple way to iterate through the contents that
    /// were inside a group \(\).
    ///
    /// This function returns an instance of CapturedList which is the list of strings.
    ///
    pub fn string_search_all_captures<'a>(input: &'a str, expr: &str) -> RUMResult<CapturedList<'a>> {
        let key = hash_regex_expression(expr);
        let re: RUMRegex = rumtk_cache_fetch!(get_regex_cache(), &key, || {compile_regex(expr)})?;
        let mut capture_list = CapturedList::with_capacity(DEFAULT_REGEX_CACHE_PAGE_SIZE);


        for caps in re.captures_iter(input) {
            for c in caps.iter().skip(1) {
                let c_str = c.unwrap().as_str();
                capture_list.push(c_str);
            }
        }

        Ok(capture_list)
    }

///
/// Given a string input and a RegEx string,
/// ```text
///     - Compile the regex if not done so already.
///     - Do a string search for all regex matches.
///     - Collapse/join the matches into a single output string using join_pattern as the join fragment.
/// ```
/// Use \" \" in join_pattern if you wish to have spaces in between matches.
///
pub fn string_search_all<'a>(input: &'a str, expr: &str) -> RUMResult<RUMVec<&'a str>> {
    let key = hash_regex_expression(expr);
    let re: RUMRegex = rumtk_cache_fetch!(get_regex_cache(), &key, || {compile_regex(expr)})?;

    Ok(re.find_iter(input).map(|s| s.as_str()).collect())
}

    ///
    /// Given a string input and a RegEx string,
    /// ```text
    ///     - Compile the regex if not done so already.
    ///     - Do a string search for all regex matches.
    ///     - Collapse/join the matches into a single output string using join_pattern as the join fragment.
    /// ```
    /// Use \" \" in join_pattern if you wish to have spaces in between matches.
    ///
    pub fn string_search(input: &str, expr: &str) -> RUMResult<RUMString> {
        let key = hash_regex_expression(expr);
        let re: RUMRegex = rumtk_cache_fetch!(get_regex_cache(), &key, || {compile_regex(expr)})?;
        Ok(re.find_iter(input).map(|s| s.as_str()).collect::<Vec<&str>>().join(""))
    }

    ///
    /// Given a string input and a set of RegEx patterns, find the target value and return it as
    /// the given target type `T`.
    ///
    /// ```
    /// use rumtk_core::regex::string_find_value;
    ///
    /// let haystack = "Range (min \\xe2\\x80\\xa6 max):     0.6 ms \\xe2\\x80\\xa6   2.9 ms    1273 runs";
    /// let patterns = ["\\d+ runs", "\\d+"];
    /// let expected = 1273;
    /// let result = string_find_value::<usize>(haystack, &patterns);
    ///
    /// assert_eq!(result, Ok(expected), "Did not find the needle in the haystack or returned the wrong type!");
    /// ```
    /// Use \" \" in join_pattern if you wish to have spaces in between matches.
    ///
    pub fn string_find_value<T>(input: &str, patterns: &[&str]) -> RUMResult<T>
    where
        T: Default + FromStr
    {
        let mut haystack = input;
        let mut needle = RUMString::default();
        let mut result = T::default();

        for expr in patterns {
            needle = string_search(haystack, expr)?;
            haystack = &needle;
        }

        result = needle.trim().parse::<T>().unwrap_or_default();
        Ok(result)
    }