|
| 1 | +use std::fmt::{self, Display}; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +header! { |
| 5 | + #[doc="`Accept-Ranges` header, defined in"] |
| 6 | + #[doc="[RFC7233](http://tools.ietf.org/html/rfc7233#section-2.3)"] |
| 7 | + #[doc=""] |
| 8 | + #[doc="The `Accept-Ranges` header field allows a server to indicate that it"] |
| 9 | + #[doc="supports range requests for the target resource."] |
| 10 | + #[doc=""] |
| 11 | + #[doc="# ABNF"] |
| 12 | + #[doc="```plain"] |
| 13 | + #[doc="Accept-Ranges = acceptable-ranges"] |
| 14 | + #[doc="acceptable-ranges = 1#range-unit / \"none\""] |
| 15 | + #[doc=""] |
| 16 | + #[doc="# Example values"] |
| 17 | + #[doc="* `bytes`"] |
| 18 | + #[doc="* `none`"] |
| 19 | + #[doc="```"] |
| 20 | + (AcceptRanges, "Accept-Ranges") => (RangeUnit)+ |
| 21 | + |
| 22 | + test_acccept_ranges { |
| 23 | + test_header!(test1, vec![b"bytes"]); |
| 24 | + test_header!(test2, vec![b"none"]); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Range Units, described in [RFC7233](http://tools.ietf.org/html/rfc7233#section-2) |
| 29 | +/// |
| 30 | +/// A representation can be partitioned into subranges according to |
| 31 | +/// various structural units, depending on the structure inherent in the |
| 32 | +/// representation's media type. |
| 33 | +/// |
| 34 | +/// # ABNF |
| 35 | +/// ```plain |
| 36 | +/// range-unit = bytes-unit / other-range-unit |
| 37 | +/// bytes-unit = "bytes" |
| 38 | +/// other-range-unit = token |
| 39 | +/// ``` |
| 40 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 41 | +pub enum RangeUnit { |
| 42 | + /// Indicating byte-range requests are supported. |
| 43 | + Bytes, |
| 44 | + /// Reserved as keyword, indicating no ranges are supported. |
| 45 | + None, |
| 46 | + /// The given range unit is not registered at IANA. |
| 47 | + Unregistered(String), |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +impl FromStr for RangeUnit { |
| 52 | + type Err = (); |
| 53 | + fn from_str(s: &str) -> Result<Self, ()> { |
| 54 | + match s { |
| 55 | + "bytes" => Ok(RangeUnit::Bytes), |
| 56 | + "none" => Ok(RangeUnit::None), |
| 57 | + // FIXME: Check if s is really a Token |
| 58 | + _ => Ok(RangeUnit::Unregistered(s.to_string())), |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl Display for RangeUnit { |
| 64 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 65 | + match *self { |
| 66 | + RangeUnit::Bytes => f.write_str("bytes"), |
| 67 | + RangeUnit::None => f.write_str("none"), |
| 68 | + RangeUnit::Unregistered(ref x) => f.write_str(&x), |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments