upcloud_sdk/types/
common.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Labels {
    pub label: Vec<Label>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Label {
    pub key: String,
    pub value: String,
}

impl Labels {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn with(mut self, key: &str, value: &str) -> Self {
        self.label.push(Label { key: key.to_string(), value: value.to_string() });
        self
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Tags {
    pub tag: Vec<String>,
}

// Add this new struct
#[derive(Debug, Default)]
pub struct LabelFilter {
    labels: Vec<String>,
}

impl LabelFilter {
    pub fn new() -> Self {
        Self { labels: Vec::new() }
    }

    pub fn with(mut self, key: &str, value: &str) -> Self {
        self.labels.push(format!("{}={}", key, value));
        self
    }

    pub fn add_label(&mut self, key: &str) -> &mut Self {
        self.labels.push(key.to_string());
        self
    }

    pub fn add_label_value(&mut self, key: &str, value: &str) -> &mut Self {
        self.labels.push(format!("{}={}", key, value));
        self
    }

    pub fn to_query_params(&self) -> String {
        if self.labels.is_empty() {
            String::new()
        } else {
            self.labels
                .iter()
                .map(|l| format!("label={}", urlencoding::encode(l)))
                .collect::<Vec<_>>()
                .join("&")
        }
    }
}