docker_api/
models.rs

1//! Generated Docker models
2
3pub use docker_api_stubs::models::*;
4
5use crate::errors::{Error, Result};
6
7use hyper::header::HeaderMap;
8use serde::{Deserialize, Serialize};
9
10use std::convert::TryFrom;
11
12#[derive(Serialize, Debug)]
13pub struct PingInfo {
14    pub api_version: String,
15    pub builder_version: Option<String>,
16    pub docker_experimental: bool,
17    pub cache_control: String,
18    pub pragma: String,
19    pub os_type: String,
20    pub server: String,
21    pub date: String,
22}
23
24impl TryFrom<&HeaderMap> for PingInfo {
25    type Error = Error;
26
27    fn try_from(value: &HeaderMap) -> Result<Self> {
28        macro_rules! extract_str {
29            ($id:literal) => {{
30                if let Some(val) = value.get($id) {
31                    val.to_str().map(ToString::to_string).map_err(|e| {
32                        Error::InvalidResponse(format!(
33                            "failed to convert header to string - {}",
34                            e
35                        ))
36                    })?
37                } else {
38                    return Err(Error::InvalidResponse(format!(
39                        "expected `{}` field in headers",
40                        $id
41                    )));
42                }
43            }};
44        }
45
46        Ok(PingInfo {
47            api_version: extract_str!("api-version"),
48            builder_version: value
49                .get("builder-version")
50                .and_then(|v| v.to_str().map(ToString::to_string).ok()),
51            docker_experimental: extract_str!("docker-experimental").parse().map_err(|e| {
52                Error::InvalidResponse(format!("expected header value to be bool - {e}"))
53            })?,
54            cache_control: extract_str!("cache-control"),
55            pragma: extract_str!("pragma"),
56            os_type: extract_str!("ostype"),
57            date: extract_str!("date"),
58            server: extract_str!("server"),
59        })
60    }
61}
62
63#[derive(Clone, Serialize, Deserialize, Debug)]
64#[serde(untagged)]
65/// Represents a response chunk from Docker api when building, pulling or importing an image.
66pub enum ImageBuildChunk {
67    Update {
68        stream: String,
69    },
70    Error {
71        error: String,
72        #[serde(rename = "errorDetail")]
73        error_detail: ErrorDetail,
74    },
75    Digest {
76        aux: Aux,
77    },
78    PullStatus {
79        status: String,
80        id: Option<String>,
81        progress: Option<String>,
82        #[serde(rename = "progressDetail")]
83        progress_detail: Option<ProgressDetail>,
84    },
85}
86
87#[derive(Clone, Serialize, Deserialize, Debug)]
88pub struct Aux {
89    #[serde(rename = "ID")]
90    pub id: String,
91}
92
93#[derive(Clone, Serialize, Deserialize, Debug)]
94pub struct ErrorDetail {
95    pub message: String,
96}
97
98#[derive(Clone, Serialize, Deserialize, Debug)]
99pub struct ProgressDetail {
100    pub current: Option<u64>,
101    pub total: Option<u64>,
102}
103
104pub type Labels = std::collections::HashMap<String, String>;