pub enum ByteRange {
FromTo(u64, u64),
From(u64),
Suffix(u64),
}Expand description
A single byte range specifier as used in the Range header (RFC 7233 §2.1).
FromTo(first, last)— a closed byte rangefirst-last(both inclusive).From(first)— an open-ended range starting atfirst.Suffix(n)— the lastnbytes of the representation.
Variants§
FromTo(u64, u64)
A closed byte range: <first>-<last> (both inclusive).
From(u64)
An open-ended byte range: <first>- (from first to end).
Suffix(u64)
A suffix byte range: -<n> (last n bytes).
Implementations§
Source§impl ByteRange
impl ByteRange
Sourcepub fn is_valid(&self, length: u64) -> bool
pub fn is_valid(&self, length: u64) -> bool
Validate this range against the total length of the resource.
Returns false if:
FromTo(first, last):first > lastorfirst >= length.From(first):first >= length.Suffix(n):n == 0.
use api_bones::range::ByteRange;
assert!(ByteRange::FromTo(0, 499).is_valid(1000));
assert!(!ByteRange::FromTo(500, 200).is_valid(1000)); // first > last
assert!(!ByteRange::FromTo(1000, 1999).is_valid(1000)); // first >= length
assert!(ByteRange::Suffix(100).is_valid(1000));
assert!(!ByteRange::Suffix(0).is_valid(1000));Sourcepub fn resolve(&self, length: u64) -> Option<(u64, u64)>
pub fn resolve(&self, length: u64) -> Option<(u64, u64)>
Resolve this range to a (first, last) byte range against the given
resource length. Returns None if the range is unsatisfiable.
use api_bones::range::ByteRange;
assert_eq!(ByteRange::FromTo(0, 99).resolve(500), Some((0, 99)));
assert_eq!(ByteRange::From(400).resolve(500), Some((400, 499)));
assert_eq!(ByteRange::Suffix(100).resolve(500), Some((400, 499)));
assert_eq!(ByteRange::Suffix(600).resolve(500), Some((0, 499)));
assert_eq!(ByteRange::FromTo(0, 99).resolve(0), None);Trait Implementations§
Source§impl<'de> Deserialize<'de> for ByteRange
impl<'de> Deserialize<'de> for ByteRange
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for ByteRange
impl StructuralPartialEq for ByteRange
Auto Trait Implementations§
impl Freeze for ByteRange
impl RefUnwindSafe for ByteRange
impl Send for ByteRange
impl Sync for ByteRange
impl Unpin for ByteRange
impl UnsafeUnpin for ByteRange
impl UnwindSafe for ByteRange
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ValidateIp for Twhere
T: ToString,
impl<T> ValidateIp for Twhere
T: ToString,
Source§fn validate_ipv4(&self) -> bool
fn validate_ipv4(&self) -> bool
Validates whether the given string is an IP V4
Source§fn validate_ipv6(&self) -> bool
fn validate_ipv6(&self) -> bool
Validates whether the given string is an IP V6
Source§fn validate_ip(&self) -> bool
fn validate_ip(&self) -> bool
Validates whether the given string is an IP