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
use bytes::Bytes;
use hyper::Body;



/// the optional body of a http request.
///
/// instances of this object are generally instantiated using one of the provided
/// [`From`](https://doc.rust-lang.org/std/convert/trait.From.html) or
/// [`Into`](https://doc.rust-lang.org/std/convert/trait.Into.html) conversions.
///
/// # conversions
///
/// [`HttpContent`](struct.HttpContent.html)s can be created from any of the following types, or from an
/// [`Option`](https://doc.rust-lang.org/std/option/enum.Option.html) of any of the following types:
///
/// ## byte types
///
///   - [`Vec<u8>`](https://doc.rust-lang.org/std/vec/struct.Vec.html)
///   - [`&'static [u8]`](https://doc.rust-lang.org/std/primitive.slice.html)
///
/// ## string types
///
///   - [`std::string::String`](https://doc.rust-lang.org/std/string/struct.String.html)
///   - [`&'static str`](https://doc.rust-lang.org/std/primitive.slice.html)
///
/// ## types from other crates
///
///   - [`bytes::Bytes`](https://docs.rs/bytes/0.4.4/bytes/struct.Bytes.html)
///   - [`Option<hyper::Body>`](https://docs.rs/hyper/0.11.1/hyper/struct.Body.html)
///
#[derive(Debug, Default)]
pub struct HttpContent {
    underlying: Body,
}

impl HttpContent {
    /// creates a content object that is empty and has no value.
    ///
    /// # examples
    ///
    /// ```
    /// use simplist::HttpContent;
    ///
    /// let content = HttpContent::none();
    /// ```
    pub fn none() -> HttpContent {
        HttpContent { underlying: Body::default() }
    }
}



impl Into<Body> for HttpContent {
    fn into(self) -> Body {
        self.underlying
    }
}



impl From<Bytes> for HttpContent {
    #[inline]
    fn from(bytes: Bytes) -> HttpContent {
        HttpContent { underlying: bytes.into() }
    }
}

impl From<Vec<u8>> for HttpContent {
    #[inline]
    fn from(vec: Vec<u8>) -> HttpContent {
        HttpContent { underlying: vec.into() }
    }
}

impl From<&'static [u8]> for HttpContent {
    #[inline]
    fn from(slice: &'static [u8]) -> HttpContent {
        HttpContent { underlying: slice.into() }
    }
}

impl From<String> for HttpContent {
    #[inline]
    fn from(string: String) -> HttpContent {
        HttpContent { underlying: string.into() }
    }
}

impl From<&'static str> for HttpContent {
    #[inline]
    fn from(slice: &'static str) -> HttpContent {
        HttpContent { underlying: slice.into() }
    }
}

impl From<Option<Body>> for HttpContent {
    #[inline]
    fn from(body: Option<Body>) -> HttpContent {
        HttpContent { underlying: body.into() }
    }
}

impl<T> From<Option<T>> for HttpContent where T: Into<HttpContent> {
    #[inline]
    fn from(body: Option<T>) -> HttpContent {
        match body {
            Some(x) => x.into(),
            None    => HttpContent::none(),
        }
    }
}