1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
///! Response from Dockerd
///!
use std::error::Error as StdError;
use std::fmt;

use serde_json::value as json;

#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Serialize, Deserialize)]
pub struct ProgressDetail {
    pub current: u64,
    pub total: u64,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct Progress {
    /// image tag or hash of image layer or ...
    pub id: String,
    /// progress bar
    pub progress: Option<String>,
    /// progress detail
    #[serde(deserialize_with = "progress_detail_opt::deserialize")]
    pub progressDetail: Option<ProgressDetail>,
    /// message or auxiliary info
    pub status: String,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Serialize, Deserialize)]
pub struct Status {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub status: String,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Serialize, Deserialize)]
pub struct ErrorDetail {
    pub message: String,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct Error {
    pub error: String,
    pub errorDetail: ErrorDetail,
}

/// Response of /images/create or other API
///
/// ## NOTE
/// Structure of this type is not documented officialy.
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Response {
    Progress(Progress),
    Status(Status),
    Error(Error),
    /// unknown response
    Unknown(json::Value),
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}: {}", self.error, self.errorDetail.message)
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        &self.error
    }

    fn cause(&self) -> Option<&::std::error::Error> {
        None
    }
}

impl Response {
    pub fn as_error(&self) -> Option<&Error> {
        use self::Response::*;
        if let &Error(ref err) = self {
            Some(err)
        } else {
            None
        }
    }
}

mod progress_detail_opt {
    use super::*;
    use serde::de::{self, Deserializer, MapAccess, Visitor};

    pub fn deserialize<'de, D>(de: D) -> Result<Option<ProgressDetail>, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct OptVisitor;

        impl<'de> Visitor<'de> for OptVisitor {
            type Value = Option<ProgressDetail>;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("Option<ProgressDetail>")
            }

            fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut current = None;
                let mut total = None;

                match map.next_key()? {
                    Some(mut key) => loop {
                        match key {
                            "current" => {
                                if current.is_some() {
                                    return Err(de::Error::duplicate_field("current"));
                                }
                                current = Some(map.next_value()?);
                            }
                            "total" => {
                                if total.is_some() {
                                    return Err(de::Error::duplicate_field("total"));
                                }
                                total = Some(map.next_value()?);
                            }
                            _ => return Err(de::Error::unknown_field(key, FIELDS)),
                        };
                        if let Some(key_) = map.next_key()? {
                            key = key_;
                        } else {
                            break;
                        }
                    },
                    None => return Ok(None), // {}
                }

                let current = current.ok_or_else(|| de::Error::missing_field("current"))?;
                let total = total.ok_or_else(|| de::Error::missing_field("total"))?;

                Ok(Some(ProgressDetail { current, total }))
            }
        }

        const FIELDS: &'static [&'static str] = &["current", "total"];
        de.deserialize_map(OptVisitor)
    }
}

#[cfg(test)]
mod tests {
    use self::Response as R;
    use super::*;
    use serde_json;

    #[test]
    #[cfg_attr(rustfmt, rustfmt_skip)]
    fn progress() {
        let s = r#"{
            "status": "Downloading",
            "progressDetail":{
                "current":1596117,
                "total":86451485
            },
            "progress":"[\u003e                                                  ]  1.596MB/86.45MB","id":"66aa7ce9b58b"
        }"#;
        assert_eq!(
            R::Progress(Progress {
                id: "66aa7ce9b58b".to_owned(),
                progress:
                    "[\u{003e}                                                  ]  1.596MB/86.45MB"
                        .to_owned()
                        .into(),
                status: "Downloading".to_owned(),
                progressDetail: Some(ProgressDetail {
                    current: 1596117,
                    total: 86451485,
                }),
            }),
            serde_json::from_str(s).unwrap()
        )
    }

    #[test]
    fn progress_empty() {
        let s = r#"{"status":"Already exists","progressDetail":{},"id":"18b8eb7e7f01"}"#;
        assert_eq!(
            Progress {
                id: "18b8eb7e7f01".to_owned(),
                progress: None,
                progressDetail: None,
                status: "Already exists".to_owned(),
            },
            serde_json::from_str(s).unwrap()
        );
    }

    #[test]
    fn status() {
        let s = r#"{"status":"Pulling from eldesh/smlnj","id":"110.78"}"#;
        assert_eq!(
            R::Status(Status {
                id: Some("110.78".to_owned()),
                status: "Pulling from eldesh/smlnj".to_owned(),
            }),
            serde_json::from_str(s).unwrap()
        )
    }

    #[test]
    #[cfg_attr(rustfmt, rustfmt_skip)]
    fn error() {
        let s = r#"{
            "errorDetail":{
                "message":"failed to register layer: Error processing tar file(exit status 1): write /foo/bar: no space left on device"
            },
            "error":"failed to register layer: Error processing tar file(exit status 1): write /foo/bar: no space left on device"
        }"#;
        assert_eq!(
            R::Error(Error {
                error: "failed to register layer: Error processing tar file(exit status 1): write /foo/bar: no space left on device".to_owned(),
                errorDetail: ErrorDetail {
                    message: "failed to register layer: Error processing tar file(exit status 1): write /foo/bar: no space left on device".to_owned(),
                },
            }),
            serde_json::from_str(s).unwrap()
        )
    }
}