podman_client/images/
search.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::search::{ImageSearch, ImageSearchOptions},
13    },
14    utils::bool_to_str::bool_to_str,
15};
16
17impl Client {
18    pub async fn image_search(
19        &self,
20        options: Option<ImageSearchOptions<'_>>,
21    ) -> Result<ImageSearch, Error> {
22        let mut path = "/libpod/images/search".to_owned();
23
24        if let Some(options) = options {
25            let mut query = form_urlencoded::Serializer::new(String::new());
26            if let Some(opt_filters) = options.filters {
27                let mut filters = HashMap::<&str, Vec<String>>::new();
28                if let Some(is_automated) = opt_filters.is_automated
29                    && !is_automated.is_empty()
30                {
31                    filters.insert(
32                        "is-automated",
33                        is_automated
34                            .iter()
35                            .map(|&b| bool_to_str(b).to_owned())
36                            .collect(),
37                    );
38                }
39                if let Some(is_official) = opt_filters.is_official
40                    && !is_official.is_empty()
41                {
42                    filters.insert(
43                        "is-official",
44                        is_official
45                            .iter()
46                            .map(|&b| bool_to_str(b).to_owned())
47                            .collect(),
48                    );
49                }
50                if let Some(stars) = opt_filters.stars
51                    && !stars.is_empty()
52                {
53                    filters.insert(
54                        "stars",
55                        stars
56                            .iter()
57                            .map(|&s| itoa::Buffer::new().format(s).to_owned())
58                            .collect(),
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
68            if let Some(limit) = options.limit {
69                query.append_pair("limit", itoa::Buffer::new().format(limit));
70            }
71            if let Some(list_tags) = options.list_tags {
72                query.append_pair("listTags", bool_to_str(list_tags));
73            }
74            if let Some(term) = options.term {
75                query.append_pair("term", term);
76            }
77            if let Some(tls_verify) = options.tls_verify {
78                query.append_pair("tlsVerify", bool_to_str(tls_verify));
79            }
80            let query_string = query.finish();
81            if !query_string.is_empty() {
82                path += &["?", &query_string].concat();
83            }
84        }
85
86        let (_, data) = self
87            .send_request::<_, (), _>(SendRequestOptions {
88                method: "GET",
89                path: &path,
90                header: None,
91                body: Empty::<Bytes>::new(),
92            })
93            .await?;
94
95        Ok(data)
96    }
97}