stackify_docker_api/opts/
task.rs

1use containers_api::opts::{Filter, FilterItem};
2use containers_api::{impl_filter_func, impl_opts_builder};
3
4impl_opts_builder!(url => TaskList);
5
6#[derive(Clone, Copy, Debug)]
7pub enum TaskStateFilter {
8    Running,
9    Shutdown,
10    Accepted,
11}
12
13impl AsRef<str> for TaskStateFilter {
14    fn as_ref(&self) -> &str {
15        match &self {
16            Self::Running => "running",
17            Self::Shutdown => "shutdown",
18            Self::Accepted => "accepted",
19        }
20    }
21}
22
23pub enum TaskFilter {
24    /// The state that the task should be in.
25    DesiredState(TaskStateFilter),
26    /// The ID of the config.
27    Id(String),
28    /// Label in the form of `label=key`
29    LabelKey(String),
30    /// Label in the form of `label=key=val`
31    Label(String, String),
32    /// The name of the config.
33    Name(String),
34    /// Name of the node.
35    Node(String),
36    /// Name of the service.
37    Service(String),
38}
39
40impl Filter for TaskFilter {
41    fn query_item(&self) -> FilterItem {
42        use TaskFilter::*;
43        match &self {
44            DesiredState(state) => FilterItem::new("desired-state", state.as_ref().to_string()),
45            Id(id) => FilterItem::new("id", id.to_owned()),
46            LabelKey(key) => FilterItem::new("label", key.to_owned()),
47            Label(key, val) => FilterItem::new("label", format!("{key}={val}")),
48            Name(name) => FilterItem::new("name", name.to_owned()),
49            Node(node) => FilterItem::new("node", node.to_owned()),
50            Service(service) => FilterItem::new("service", service.to_owned()),
51        }
52    }
53}
54
55impl TaskListOptsBuilder {
56    impl_filter_func!(
57        /// Filter listed tasks by variants of the enum.
58        TaskFilter
59    );
60}