use crate::Token;
use alloc::string::String;
use core::{fmt, num::ParseIntError, str::FromStr};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Index {
Num(usize),
Next,
}
impl Index {
pub fn for_len(&self, length: usize) -> Result<usize, OutOfBoundsError> {
match *self {
Self::Num(index) if index < length => Ok(index),
Self::Num(index) => Err(OutOfBoundsError { length, index }),
Self::Next => Err(OutOfBoundsError {
length,
index: length,
}),
}
}
pub fn for_len_incl(&self, length: usize) -> Result<usize, OutOfBoundsError> {
match *self {
Self::Num(index) if index <= length => Ok(index),
Self::Num(index) => Err(OutOfBoundsError { length, index }),
Self::Next => Ok(length),
}
}
pub fn for_len_unchecked(&self, length: usize) -> usize {
match *self {
Self::Num(idx) => idx,
Self::Next => length,
}
}
}
impl fmt::Display for Index {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
Self::Num(index) => write!(f, "{index}"),
Self::Next => f.write_str("-"),
}
}
}
impl From<usize> for Index {
fn from(value: usize) -> Self {
Self::Num(value)
}
}
impl FromStr for Index {
type Err = ParseIndexError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "-" {
Ok(Index::Next)
} else if s.starts_with('0') && s != "0" {
Err(ParseIndexError::LeadingZeros)
} else {
Ok(s.parse::<usize>().map(Index::Num)?)
}
}
}
impl TryFrom<&Token<'_>> for Index {
type Error = ParseIndexError;
fn try_from(value: &Token) -> Result<Self, Self::Error> {
Index::from_str(value.encoded())
}
}
impl TryFrom<&str> for Index {
type Error = ParseIndexError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Index::from_str(value)
}
}
impl TryFrom<Token<'_>> for Index {
type Error = ParseIndexError;
fn try_from(value: Token) -> Result<Self, Self::Error> {
Index::from_str(value.encoded())
}
}
macro_rules! derive_try_from {
($($t:ty),+ $(,)?) => {
$(
impl TryFrom<$t> for Index {
type Error = ParseIndexError;
fn try_from(value: $t) -> Result<Self, Self::Error> {
Index::from_str(&value)
}
}
)*
}
}
derive_try_from!(String, &String);
#[derive(Debug, PartialEq, Eq)]
pub struct OutOfBoundsError {
pub length: usize,
pub index: usize,
}
impl fmt::Display for OutOfBoundsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"index {} out of bounds (limit: {})",
self.index, self.length
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for OutOfBoundsError {}
#[derive(Debug, PartialEq, Eq)]
pub enum ParseIndexError {
InvalidInteger(ParseIntError),
LeadingZeros,
}
impl From<ParseIntError> for ParseIndexError {
fn from(source: ParseIntError) -> Self {
Self::InvalidInteger(source)
}
}
impl fmt::Display for ParseIndexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseIndexError::InvalidInteger(source) => {
write!(f, "failed to parse token as an integer: {source}")
}
ParseIndexError::LeadingZeros => write!(
f,
"token contained leading zeros, which are disallowed by RFC 6901"
),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseIndexError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ParseIndexError::InvalidInteger(source) => Some(source),
ParseIndexError::LeadingZeros => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Token;
#[test]
fn index_from_usize() {
let index = Index::from(5usize);
assert_eq!(index, Index::Num(5));
}
#[test]
fn index_try_from_token_num() {
let token = Token::new("3");
let index = Index::try_from(&token).unwrap();
assert_eq!(index, Index::Num(3));
}
#[test]
fn index_try_from_token_next() {
let token = Token::new("-");
let index = Index::try_from(&token).unwrap();
assert_eq!(index, Index::Next);
}
#[test]
fn index_try_from_str_num() {
let index = Index::try_from("42").unwrap();
assert_eq!(index, Index::Num(42));
}
#[test]
fn index_try_from_str_next() {
let index = Index::try_from("-").unwrap();
assert_eq!(index, Index::Next);
}
#[test]
fn index_try_from_string_num() {
let index = Index::try_from(String::from("7")).unwrap();
assert_eq!(index, Index::Num(7));
}
#[test]
fn index_try_from_string_next() {
let index = Index::try_from(String::from("-")).unwrap();
assert_eq!(index, Index::Next);
}
#[test]
fn index_for_len_incl_valid() {
assert_eq!(Index::Num(0).for_len_incl(1), Ok(0));
assert_eq!(Index::Next.for_len_incl(2), Ok(2));
}
#[test]
fn index_for_len_incl_out_of_bounds() {
Index::Num(2).for_len_incl(1).unwrap_err();
}
#[test]
fn index_for_len_unchecked() {
assert_eq!(Index::Num(10).for_len_unchecked(5), 10);
assert_eq!(Index::Next.for_len_unchecked(3), 3);
}
#[test]
fn display_index_num() {
let index = Index::Num(5);
assert_eq!(index.to_string(), "5");
}
#[test]
fn display_index_next() {
assert_eq!(Index::Next.to_string(), "-");
}
#[test]
fn for_len() {
assert_eq!(Index::Num(0).for_len(1), Ok(0));
assert!(Index::Num(1).for_len(1).is_err());
assert!(Index::Next.for_len(1).is_err());
}
#[test]
fn out_of_bounds_error_display() {
let err = OutOfBoundsError {
length: 5,
index: 10,
};
assert_eq!(err.to_string(), "index 10 out of bounds (limit: 5)");
}
#[test]
fn parse_index_error_display() {
let err = ParseIndexError::InvalidInteger("not a number".parse::<usize>().unwrap_err());
assert_eq!(
err.to_string(),
"failed to parse token as an integer: invalid digit found in string"
);
assert_eq!(
ParseIndexError::LeadingZeros.to_string(),
"token contained leading zeros, which are disallowed by RFC 6901"
);
}
#[test]
#[cfg(feature = "std")]
fn parse_index_error_source() {
use std::error::Error;
let err = ParseIndexError::InvalidInteger("not a number".parse::<usize>().unwrap_err());
assert_eq!(
err.source().unwrap().to_string(),
"not a number".parse::<usize>().unwrap_err().to_string()
);
assert!(ParseIndexError::LeadingZeros.source().is_none());
}
#[test]
fn try_from_token() {
let token = Token::new("3");
let index = <Index as TryFrom<Token>>::try_from(token).unwrap();
assert_eq!(index, Index::Num(3));
let token = Token::new("-");
let index = Index::try_from(&token).unwrap();
assert_eq!(index, Index::Next);
}
}