mirror of
https://github.com/sile/hls_m3u8.git
synced 2024-12-23 04:26:27 +00:00
improve panic messages for ByteRange
This commit is contained in:
parent
8ece080cda
commit
47eccfdef9
1 changed files with 20 additions and 10 deletions
|
@ -39,8 +39,8 @@ pub struct ByteRange {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use hls_m3u8::types::ByteRange;
|
/// # use hls_m3u8::types::ByteRange;
|
||||||
/// assert_eq!(ByteRange::from(0..5).start(), Some(0));
|
/// assert_eq!(ByteRange::from(0..5).end(), 5);
|
||||||
/// assert_eq!(ByteRange::from(..5).start(), None);
|
/// assert_eq!(ByteRange::from(..=5).end(), 6);
|
||||||
/// ```
|
/// ```
|
||||||
end: usize,
|
end: usize,
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,11 @@ impl ByteRange {
|
||||||
/// [`end`](ByteRange::end).
|
/// [`end`](ByteRange::end).
|
||||||
pub fn set_start(&mut self, new_start: Option<usize>) -> &mut Self {
|
pub fn set_start(&mut self, new_start: Option<usize>) -> &mut Self {
|
||||||
if new_start.map_or(false, |s| s > self.end) {
|
if new_start.map_or(false, |s| s > self.end) {
|
||||||
panic!("attempt to make the start larger than the end");
|
panic!(
|
||||||
|
"attempt to make the start ({}) larger than the end ({})",
|
||||||
|
new_start.unwrap(),
|
||||||
|
self.end
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.start = new_start;
|
self.start = new_start;
|
||||||
|
@ -288,7 +292,10 @@ macro_rules! impl_from_ranges {
|
||||||
impl From<Range<$type>> for ByteRange {
|
impl From<Range<$type>> for ByteRange {
|
||||||
fn from(range: Range<$type>) -> Self {
|
fn from(range: Range<$type>) -> Self {
|
||||||
if range.start > range.end {
|
if range.start > range.end {
|
||||||
panic!("the range start must be smaller than the end");
|
panic!(
|
||||||
|
"the range start ({}) must be smaller than the end ({})",
|
||||||
|
range.start, range.end
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -304,7 +311,10 @@ macro_rules! impl_from_ranges {
|
||||||
let (start, end) = range.into_inner();
|
let (start, end) = range.into_inner();
|
||||||
|
|
||||||
if start > end {
|
if start > end {
|
||||||
panic!("the range start must be smaller than the end");
|
panic!(
|
||||||
|
"the range start ({}) must be smaller than the end ({}+1)",
|
||||||
|
start, end
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -361,7 +371,7 @@ impl TryInto<RangeTo<usize>> for ByteRange {
|
||||||
|
|
||||||
fn try_into(self) -> Result<RangeTo<usize>, Self::Error> {
|
fn try_into(self) -> Result<RangeTo<usize>, Self::Error> {
|
||||||
if self.start.is_some() {
|
if self.start.is_some() {
|
||||||
return Err(Error::custom("A `RangeTo` (`..end`) does not have a start"));
|
return Err(Error::custom("a `RangeTo` (`..end`) does not have a start"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(RangeTo { end: self.end })
|
Ok(RangeTo { end: self.end })
|
||||||
|
@ -375,7 +385,7 @@ impl TryInto<Range<usize>> for ByteRange {
|
||||||
fn try_into(self) -> Result<Range<usize>, Self::Error> {
|
fn try_into(self) -> Result<Range<usize>, Self::Error> {
|
||||||
if self.start.is_none() {
|
if self.start.is_none() {
|
||||||
return Err(Error::custom(
|
return Err(Error::custom(
|
||||||
"A `Range` (`start..end`) has to have a start.",
|
"a `Range` (`start..end`) has to have a start.",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -427,11 +437,11 @@ mod tests {
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic = "the range start must be smaller than the end"]
|
#[should_panic = "the range start (6) must be smaller than the end (0)"]
|
||||||
fn test_from_range_panic() { let _ = ByteRange::from(6..0); }
|
fn test_from_range_panic() { let _ = ByteRange::from(6..0); }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic = "the range start must be smaller than the end"]
|
#[should_panic = "the range start (6) must be smaller than the end (0+1)"]
|
||||||
fn test_from_range_inclusive_panic() { let _ = ByteRange::from(6..=0); }
|
fn test_from_range_inclusive_panic() { let _ = ByteRange::from(6..=0); }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -493,7 +503,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic = "attempt to make the start larger than the end"]
|
#[should_panic = "attempt to make the start (11) larger than the end (10)"]
|
||||||
fn test_set_start() { let _ = ByteRange::from(4..10).set_start(Some(11)); }
|
fn test_set_start() { let _ = ByteRange::from(4..10).set_start(Some(11)); }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue