podman_client/images/
prune.rs

1use std::collections::HashMap;
2
3use http_body_util::Empty;
4use hyper::body::Bytes;
5use url::form_urlencoded::{self, byte_serialize};
6
7use crate::{
8    client::Client,
9    models::{
10        connection::SendRequestOptions,
11        lib::Error,
12        podman::images::prune::{ImagePrune, ImagePruneOptions},
13    },
14    utils::bool_to_str::bool_to_str,
15};
16
17impl Client {
18    pub async fn image_prune(
19        &self,
20        options: Option<ImagePruneOptions<'_>>,
21    ) -> Result<ImagePrune, Error> {
22        let mut path = "/libpod/images/prune".to_owned();
23
24        if let Some(options) = options {
25            let mut query = form_urlencoded::Serializer::new(String::new());
26            if let Some(all) = options.all {
27                query.append_pair("all", bool_to_str(all));
28            }
29            if let Some(build_cache) = options.build_cache {
30                query.append_pair("buildcache", bool_to_str(build_cache));
31            }
32            if let Some(external) = options.external {
33                query.append_pair("external", bool_to_str(external));
34            }
35            if let Some(opt_filters) = options.filters {
36                let mut filters = HashMap::new();
37                if let Some(dangling) = opt_filters.dangling
38                    && !dangling.is_empty()
39                {
40                    filters.insert(
41                        "dangling",
42                        dangling.iter().map(|&d| bool_to_str(d)).collect(),
43                    );
44                }
45                if let Some(until) = opt_filters.until
46                    && !until.is_empty()
47                {
48                    filters.insert("until", until);
49                }
50                if let Some(label) = opt_filters.label
51                    && !label.is_empty()
52                {
53                    filters.insert("label", label);
54                }
55                if let Some(labelnot) = opt_filters.labelnot
56                    && !labelnot.is_empty()
57                {
58                    filters.insert("label!", labelnot);
59                }
60
61                if !filters.is_empty() {
62                    let filters_json = serde_json::to_string(&filters)?;
63                    let filters_encoded: String = byte_serialize(filters_json.as_bytes()).collect();
64                    query.append_pair("filters", &filters_encoded);
65                }
66
67                let query_string = query.finish();
68
69                if !query_string.is_empty() {
70                    path += &["?", &query_string].concat();
71                }
72            }
73        }
74
75        let (_, data) = self
76            .send_request::<_, (), _>(SendRequestOptions {
77                method: "POST",
78                path: &path,
79                header: None,
80                body: Empty::<Bytes>::new(),
81            })
82            .await?;
83
84        Ok(data)
85    }
86}