elasticctl_api/value_lists/
model.rs1use std::collections::BTreeSet;
2use std::fmt;
3
4use elasticctl_core::{Error, ErrorKind, Result};
5use serde::{Deserialize, Serialize};
6use serde_json::{Map, Value};
7
8#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
9#[serde(from = "String", into = "String")]
10pub enum ValueListType {
11 Keyword,
12 Ip,
13 IpRange,
14 Text,
15 Unsupported(String),
16}
17
18impl From<String> for ValueListType {
19 fn from(name: String) -> Self {
20 match name.as_str() {
21 "keyword" => Self::Keyword,
22 "ip" => Self::Ip,
23 "ip_range" => Self::IpRange,
24 "text" => Self::Text,
25 _ => Self::Unsupported(name),
26 }
27 }
28}
29
30impl From<ValueListType> for String {
31 fn from(value: ValueListType) -> Self {
32 value.to_string()
33 }
34}
35
36impl fmt::Display for ValueListType {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 f.write_str(match self {
39 Self::Keyword => "keyword",
40 Self::Ip => "ip",
41 Self::IpRange => "ip_range",
42 Self::Text => "text",
43 Self::Unsupported(name) => name,
44 })
45 }
46}
47
48impl ValueListType {
49 pub fn require_supported(&self) -> Result<()> {
50 if let Self::Unsupported(name) = self {
51 return Err(Error::new(
52 ErrorKind::Unsupported,
53 format!(
54 "unsupported value-list type {name:?}; supported types: keyword, ip, ip_range, text"
55 ),
56 ));
57 }
58 Ok(())
59 }
60}
61
62#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
63#[serde(deny_unknown_fields)]
64pub struct Container {
65 pub id: String,
66 #[serde(rename = "type")]
67 pub list_type: ValueListType,
68 pub name: String,
69 pub description: String,
70 #[serde(skip_serializing_if = "Option::is_none")]
71 pub meta: Option<Map<String, Value>>,
72}
73
74pub type ValueSet = BTreeSet<String>;
75
76#[derive(Clone, Debug, PartialEq, Serialize)]
77pub struct LocalPair {
78 pub stem: String,
79 pub container: Container,
80 pub values: ValueSet,
81}
82
83#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
84pub struct StoredValues {
85 pub values: Vec<String>,
86}
87
88impl StoredValues {
89 pub fn new(values: Vec<String>) -> Self {
90 Self { values }
91 }
92
93 pub fn set(&self) -> ValueSet {
94 self.values.iter().cloned().collect()
95 }
96
97 pub fn dash_count(&self) -> usize {
98 self.values
99 .iter()
100 .filter(|value| value.contains('-'))
101 .count()
102 }
103}