pub enum Pointer<'a> {
Key(&'a str),
Index(usize),
}
impl JsonPointer for Pointer<'_> {
#[inline]
fn as_key(&self) -> Option<&str> {
match self {
Pointer::Key(v) => Some(v),
_ => None,
}
}
#[inline]
fn as_index(&self) -> Option<usize> {
match self {
Pointer::Index(v) => Some(*v),
_ => None,
}
}
}
impl<'a> From<&'a str> for Pointer<'a> {
#[inline]
fn from(value: &'a str) -> Self {
Self::Key(value)
}
}
impl From<usize> for Pointer<'_> {
#[inline]
fn from(value: usize) -> Self {
Self::Index(value)
}
}
pub trait JsonPointer {
fn as_key(&self) -> Option<&str>;
fn as_index(&self) -> Option<usize>;
}
impl JsonPointer for &str {
#[inline(always)]
fn as_key(&self) -> Option<&str> {
Some(self)
}
#[inline(always)]
fn as_index(&self) -> Option<usize> {
None
}
}
impl JsonPointer for usize {
#[inline(always)]
fn as_key(&self) -> Option<&str> {
None
}
#[inline(always)]
fn as_index(&self) -> Option<usize> {
Some(*self)
}
}
#[macro_export]
macro_rules! jsonp {
() => (
[] as [$crate::pointer::Pointer; 0]
);
($($x:expr),+ $(,)?) => (
[$($crate::pointer::Pointer::from($x)),+]
);
}