containers_api/conn/
payload.rs

1use hyper::Body;
2
3/// Types of payload that can be sent
4pub enum Payload<B: Into<Body>> {
5    None,
6    Text(B),
7    Json(B),
8    XTar(B),
9    Tar(B),
10}
11
12impl Payload<Body> {
13    /// Creates an empty payload
14    pub fn empty() -> Self {
15        Payload::None
16    }
17}
18
19impl<B: Into<Body>> Payload<B> {
20    /// Extracts the inner body if there is one and returns it
21    pub fn into_inner(self) -> Option<B> {
22        match self {
23            Self::None => None,
24            Self::Text(b) => Some(b),
25            Self::Json(b) => Some(b),
26            Self::XTar(b) => Some(b),
27            Self::Tar(b) => Some(b),
28        }
29    }
30
31    /// Returns the mime type of this payload
32    pub fn mime_type(&self) -> Option<mime::Mime> {
33        match &self {
34            Self::None => None,
35            Self::Text(_) => None,
36            Self::Json(_) => Some(mime::APPLICATION_JSON),
37            Self::XTar(_) => Some("application/x-tar".parse().expect("parsed mime")),
38            Self::Tar(_) => Some("application/tar".parse().expect("parsed mime")),
39        }
40    }
41
42    /// Checks if there is no payload
43    pub fn is_none(&self) -> bool {
44        matches!(self, Self::None)
45    }
46}