assemblyline_models/types/
strings.rs

1use serde::{Deserialize, Serialize};
2use serde_with::{DeserializeFromStr, SerializeDisplay};
3use struct_metadata::Described;
4
5use crate::{ElasticMeta, ModelError};
6
7
8/// A string that maps to a keyword field in elasticsearch.
9/// 
10/// This is the default behaviour for a String in a mapped struct, the only reason
11/// to use this over a standard String is cases where the 'mapping' field has been overwritten
12/// by a container and the more explicit 'mapping' this provided is needed to reassert
13/// the keyword type.
14/// 
15/// Example:
16///         #[metadata(store=false, mapping="flattenedobject")]
17///         pub safelisted_tags: HashMap<String, Vec<Keyword>>,
18/// 
19/// In that example, if the inner Keyword was String the entire HashMap would have its 
20/// mapping set to 'flattenedobject', the inner Keyword more explicitly overrides this.
21#[derive(Debug, Serialize, Deserialize, Described, Clone, PartialEq, Eq, PartialOrd, Ord)]
22#[metadata_type(ElasticMeta)]
23#[metadata(mapping="keyword")]
24pub struct Keyword(String);
25
26impl std::fmt::Display for Keyword {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        f.write_str(&self.0)
29    }
30}
31
32impl std::ops::Deref for Keyword {
33    type Target = str;
34
35    fn deref(&self) -> &Self::Target {
36        &self.0
37    }
38}
39
40impl From<String> for Keyword {
41    fn from(s: String) -> Self {
42        Keyword(s)
43    }
44}
45
46impl From<&str> for Keyword {
47    fn from(s: &str) -> Self {
48        Keyword(s.to_string())
49    }
50}
51
52
53#[derive(Debug, Serialize, Deserialize, Described, Clone, PartialEq, Eq, PartialOrd, Ord)]
54#[metadata_type(ElasticMeta)]
55#[metadata(mapping="wildcard")]
56pub struct Wildcard(String);
57
58impl std::fmt::Display for Wildcard {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        f.write_str(&self.0)
61    }
62}
63
64impl std::ops::Deref for Wildcard {
65    type Target = str;
66
67    fn deref(&self) -> &Self::Target {
68        &self.0
69    }
70}
71
72impl From<String> for Wildcard {
73    fn from(s: String) -> Self {
74        Wildcard(s)
75    }
76}
77
78impl From<&str> for Wildcard {
79    fn from(s: &str) -> Self {
80        Wildcard(s.to_string())
81    }
82}
83
84
85/// Uppercase String
86#[derive(Debug, SerializeDisplay, DeserializeFromStr, Described, Clone, PartialEq, Eq, PartialOrd, Ord)]
87#[metadata_type(ElasticMeta)]
88pub struct UpperString(String);
89
90
91impl std::fmt::Display for UpperString {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        f.write_str(&self.0)
94    }
95}
96
97impl std::ops::Deref for UpperString {
98    type Target = str;
99
100    fn deref(&self) -> &Self::Target {
101        &self.0
102    }
103}
104
105impl std::str::FromStr for UpperString {
106    type Err = ModelError;
107
108    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
109        let value = s.trim().to_uppercase();
110        Ok(UpperString(value))
111    }
112}
113
114impl From<&str> for UpperString {
115    fn from(s: &str) -> Self {
116        let value = s.trim().to_uppercase();
117        UpperString(value)
118    }
119}