use std::ops::{
Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo,
RangeToInclusive,
};
use crate::TryFromBoundsError;
pub trait TryFromBounds<I> {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError>
where
Self: Sized;
}
impl<I> TryFromBounds<I> for (Bound<I>, Bound<I>) {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError> {
Ok((start_bound, end_bound))
}
}
impl<I> TryFromBounds<I> for Range<I> {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError> {
match (start_bound, end_bound) {
(Bound::Included(start), Bound::Excluded(end)) => Ok(start..end),
_ => Err(TryFromBoundsError),
}
}
}
impl<I> TryFromBounds<I> for RangeInclusive<I> {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError> {
match (start_bound, end_bound) {
(Bound::Included(start), Bound::Included(end)) => Ok(start..=end),
_ => Err(TryFromBoundsError),
}
}
}
impl<I> TryFromBounds<I> for RangeFrom<I> {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError> {
match (start_bound, end_bound) {
(Bound::Included(start), Bound::Unbounded) => Ok(start..),
_ => Err(TryFromBoundsError),
}
}
}
impl<I> TryFromBounds<I> for RangeTo<I> {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError> {
match (start_bound, end_bound) {
(Bound::Unbounded, Bound::Excluded(end)) => Ok(..end),
_ => Err(TryFromBoundsError),
}
}
}
impl<I> TryFromBounds<I> for RangeToInclusive<I> {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError> {
match (start_bound, end_bound) {
(Bound::Unbounded, Bound::Included(end)) => Ok(..=end),
_ => Err(TryFromBoundsError),
}
}
}
impl<I> TryFromBounds<I> for RangeFull {
fn try_from_bounds(
start_bound: Bound<I>,
end_bound: Bound<I>,
) -> Result<Self, TryFromBoundsError> {
match (start_bound, end_bound) {
(Bound::Unbounded, Bound::Unbounded) => Ok(..),
_ => Err(TryFromBoundsError),
}
}
}