rs-docker-api-rs 0.3.2

Interface to Docker API
Documentation
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
    );
}