Skip to main content

fhir_search/
string.rs

1// SPDX-FileCopyrightText: Cadasto B.V.
2// SPDX-License-Identifier: BUSL-1.1
3
4//! How a string value matches a field (<https://hl7.org/fhir/R4/search.html>,
5//! §3.1.1.4.8).
6
7use unicode_normalization::UnicodeNormalization as _;
8use unicode_normalization::char::is_combining_mark;
9
10/// How a string value matches a field.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
12pub enum StringMatch {
13    /// No modifier: the field equals or starts with the value, both
14    /// [`fold`]ed.
15    #[default]
16    Start,
17    /// `:contains`: the value appears anywhere in the field, both
18    /// [`fold`]ed.
19    Contains,
20    /// `:exact`: the field is the value, "including casing and accents".
21    Exact,
22}
23
24impl StringMatch {
25    /// The match a string parameter's modifier asks for, or `None` for a
26    /// modifier that is no string match (`:missing`, `:text` and the rest of
27    /// §3.1.1.4.4), which the caller reads or refuses.
28    #[must_use]
29    pub fn from_modifier(modifier: Option<&str>) -> Option<Self> {
30        match modifier {
31            None => Some(Self::Start),
32            Some("contains") => Some(Self::Contains),
33            Some("exact") => Some(Self::Exact),
34            Some(_) => None,
35        }
36    }
37
38    /// The modifier that asks for this match, `None` for the default.
39    #[must_use]
40    pub const fn modifier(self) -> Option<&'static str> {
41        match self {
42            Self::Start => None,
43            Self::Contains => Some("contains"),
44            Self::Exact => Some("exact"),
45        }
46    }
47
48    /// Whether `field` matches the unescaped search `value`.
49    #[must_use]
50    pub fn matches(self, field: &str, value: &str) -> bool {
51        match self {
52            Self::Start => fold(field).starts_with(&fold(value)),
53            Self::Contains => fold(field).contains(&fold(value)),
54            Self::Exact => field == value,
55        }
56    }
57}
58
59/// Returns `text` in the form the default and the `:contains` match compare.
60///
61/// The search "is insensitive to casing and included combining characters",
62/// and "punctuation and non-significant whitespace (e.g. repeated space
63/// characters, tab vs space) should also be ignored" (§3.1.1.4.8). So the
64/// text is decomposed (NFD) and loses its combining marks, its letters are
65/// lowercased, every character that is neither alphanumeric nor whitespace
66/// is dropped, and each run of whitespace becomes one space, with none at
67/// either end.
68#[must_use]
69pub fn fold(text: &str) -> String {
70    let mut folded = String::with_capacity(text.len());
71    let mut gap = false;
72    for character in text
73        .nfd()
74        .filter(|character| !is_combining_mark(*character))
75    {
76        if character.is_whitespace() {
77            gap = true;
78        } else if character.is_alphanumeric() {
79            if gap && !folded.is_empty() {
80                folded.push(' ');
81            }
82            gap = false;
83            folded.extend(character.to_lowercase());
84        }
85    }
86    folded
87}