Module index

Source
Expand description

Abstract index representation for RFC 6901.

RFC 6901 defines two valid ways to represent array indices as Pointer tokens: non-negative integers, and the character -, which stands for the index after the last existing array member. While attempting to use - to resolve an array value will always be out of bounds, the token can be useful when paired with utilities which can mutate a value, such as this crate’s assign functionality or JSON Patch RFC 6902, as it provides a way to express where to put the new element when extending an array.

While this crate doesn’t implement RFC 6902, it still must consider non-numerical indices as valid, and provide a mechanism for manipulating them. This is what this module provides.

The main use of the Index type is when resolving a Token instance as a concrete index for a given array length:

assert_eq!(Token::new("1").to_index(), Ok(Index::Num(1)));
assert_eq!(Token::new("-").to_index(), Ok(Index::Next));
assert!(Token::new("a").to_index().is_err());

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());

assert_eq!(Index::Num(1).for_len_incl(1), Ok(1));
assert_eq!(Index::Next.for_len_incl(1), Ok(1));
assert!(Index::Num(2).for_len_incl(1).is_err());

assert_eq!(Index::Num(42).for_len_unchecked(30), 42);
assert_eq!(Index::Next.for_len_unchecked(30), 30);

Structs§

InvalidCharacterError
Indicates that a non-digit character was found when parsing the RFC 6901 array index.
OutOfBoundsError
Indicates that an Index is not within the given bounds.

Enums§

Index
Represents an abstract index into an array.
ParseIndexError
Indicates that the Token could not be parsed as valid RFC 6901 array index.