fhir_sdk/client/search.rs
1//! Search parameter handling.
2
3/// A collection of AND-joined search parameters.
4#[derive(Debug, Default, Clone)]
5pub struct SearchParameters {
6 /// List of search queries.
7 queries: Vec<(String, String)>,
8}
9
10impl SearchParameters {
11 /// Create a new list of [`SearchParameters`].
12 #[must_use]
13 pub fn empty() -> Self {
14 Self::default()
15 }
16
17 /// Add a search parameter.
18 #[must_use]
19 pub fn and<P>(mut self, parameter: P) -> Self
20 where
21 P: SearchParameter,
22 {
23 self.queries.push(parameter.into_query());
24 self
25 }
26
27 /// Add a raw custom search parameter.
28 ///
29 /// The key of the search query includes modifiers or chaining.
30 ///
31 /// The value of the search query might include multiple comma-separated
32 /// values. A value can consist of pipe-separated values for token search or
33 /// can be prepended by a comparator like `lt` for numbers.
34 #[must_use]
35 pub fn and_raw<K, V>(mut self, key: K, value: V) -> Self
36 where
37 K: Into<String>,
38 V: ToString,
39 {
40 self.queries.push((key.into(), value.to_string()));
41 self
42 }
43
44 /// Convert to a list of raw queries.
45 pub(crate) fn into_queries(self) -> Vec<(String, String)> {
46 self.queries
47 }
48}
49
50/// Functionality to convert a SearchParameter to the URL query.
51pub trait SearchParameter {
52 /// Convert this search parameter into the query pair (key, value).
53 fn into_query(self) -> (String, String);
54}