rs_docker_api_rs/opts/
task.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
use containers_api::opts::{Filter, FilterItem};
use containers_api::{impl_filter_func, impl_opts_builder};

impl_opts_builder!(url => TaskList);

#[derive(Clone, Copy, Debug)]
pub enum TaskStateFilter {
    Running,
    Shutdown,
    Accepted,
}

impl AsRef<str> for TaskStateFilter {
    fn as_ref(&self) -> &str {
        match &self {
            Self::Running => "running",
            Self::Shutdown => "shutdown",
            Self::Accepted => "accepted",
        }
    }
}

pub enum TaskFilter {
    /// The state that the task should be in.
    DesiredState(TaskStateFilter),
    /// The ID of the config.
    Id(String),
    /// Label in the form of `label=key`
    LabelKey(String),
    /// Label in the form of `label=key=val`
    Label(String, String),
    /// The name of the config.
    Name(String),
    /// Name of the node.
    Node(String),
    /// Name of the service.
    Service(String),
}

impl Filter for TaskFilter {
    fn query_item(&self) -> FilterItem {
        use TaskFilter::*;
        match &self {
            DesiredState(state) => FilterItem::new("desired-state", state.as_ref().to_string()),
            Id(id) => FilterItem::new("id", id.to_owned()),
            LabelKey(key) => FilterItem::new("label", key.to_owned()),
            Label(key, val) => FilterItem::new("label", format!("{key}={val}")),
            Name(name) => FilterItem::new("name", name.to_owned()),
            Node(node) => FilterItem::new("node", node.to_owned()),
            Service(service) => FilterItem::new("service", service.to_owned()),
        }
    }
}

impl TaskListOptsBuilder {
    impl_filter_func!(
        /// Filter listed tasks by variants of the enum.
        TaskFilter
    );
}