Skip to content

Commit

Permalink
docs for NonNegativeArrayIndex and associated
Browse files Browse the repository at this point in the history
  • Loading branch information
zwerdlds committed May 11, 2023
1 parent 7695a35 commit f423485
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/rsonpath-lib/src/engine/recursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::engine::{Compiler, Engine};
use crate::input::Input;
use crate::query::automaton::{Automaton, State, TransitionLabel};
use crate::query::error::CompilerError;
use crate::query::nonnegative_array_index::NonNegativeArrayIndex;
use crate::query::NonNegativeArrayIndex;
use crate::query::{JsonPathQuery, Label};
use crate::result::QueryResult;
use crate::BLOCK_SIZE;
Expand Down
2 changes: 2 additions & 0 deletions crates/rsonpath-lib/src/input/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ impl From<Vec<u8>> for OwnedBytes {
}

impl<T: AsRef<[u8]>> From<&T> for OwnedBytes {
#[inline]
fn from(value: &T) -> Self {
Self::new(value)
}
}

impl From<&str> for OwnedBytes {
#[inline]
fn from(value: &str) -> Self {
Self::from(&value.as_bytes())
}
Expand Down
7 changes: 4 additions & 3 deletions crates/rsonpath-lib/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ pub mod automaton;
pub mod builder;
pub mod error;
mod label;
pub mod nonnegative_array_index;
mod nonnegative_array_index;
mod parser;
use aligners::alignment;
pub use label::Label;
pub use nonnegative_array_index::NonNegativeArrayIndex;

use log::*;
use std::fmt::{self, Display};
Expand Down Expand Up @@ -74,7 +75,7 @@ pub enum JsonPathQueryNode {

use JsonPathQueryNode::*;

use self::{error::ParserError, nonnegative_array_index::NonNegativeArrayIndex};
use self::error::ParserError;

impl JsonPathQueryNode {
/// Retrieve the child of the node or `None` if it is the last one
Expand Down Expand Up @@ -266,7 +267,7 @@ impl JsonPathQueryNodeType for JsonPathQueryNode {
#[inline(always)]
fn array_index(&self) -> Option<&NonNegativeArrayIndex> {
match self {
ArrayIndexChild(i, _) | ArrayIndexDescendant(i,_) => Some(i),
ArrayIndexChild(i, _) | ArrayIndexDescendant(i, _) => Some(i),
Child(_, _) | Descendant(_, _) | Root(_) | AnyChild(_) | AnyDescendant(_) => None,
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rsonpath-lib/src/query/automaton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub struct Automaton<'q> {
/// Represent the distinct methods of moving on a match between states.
#[derive(Debug, Copy, PartialEq, Clone, Eq)]
pub enum TransitionLabel<'q> {
/// Wraps a textual field [`Label`] in a JSON object.
ObjectMember(&'q Label),
/// Wraps an array index [`NonNegativeArrayIndex`] in a JSON object.
ArrayIndex(NonNegativeArrayIndex),
}

Expand All @@ -38,6 +40,7 @@ impl<'q> TransitionLabel<'q> {
}
}

///Return the textual [`Label`] being wrapped if so. Returns [`None`] otherwise.
#[must_use]
#[inline(always)]
pub fn get_label(&self) -> Option<&Label> {
Expand All @@ -47,6 +50,7 @@ impl<'q> TransitionLabel<'q> {
}
}

/// Consumes the [`TransitionLabel`] and gives the wrapped [`Label`], if so. Returns [`None`] otherwise.
#[must_use]
#[inline(always)]
pub fn get_label_owned(self) -> Option<&'q Label> {
Expand All @@ -56,12 +60,14 @@ impl<'q> TransitionLabel<'q> {
}
}

/// Wraps a [`Label`] in a [`TransitionLabel`].
#[must_use]
#[inline(always)]
pub fn new_object_member(label: &'q Label) -> Self {
TransitionLabel::ObjectMember(label)
}

/// Wraps a [`NonNegativeArrayIndex`] in a [`TransitionLabel`].
#[must_use]
#[inline(always)]
pub fn new_array_index(label: NonNegativeArrayIndex) -> Self {
Expand Down
11 changes: 10 additions & 1 deletion crates/rsonpath-lib/src/query/nonnegative_array_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ use std::fmt::{self, Display, Formatter};
///
/// Represents a specific location from the front of the list in a json array.
/// Provides the [IETF-conforming index value](https://www.rfc-editor.org/rfc/rfc7493.html#section-2). Values are \[0, (2^53)-1].
/// # Examples
///
/// ```
/// # use rsonpath_lib::query::NonNegativeArrayIndex;
///
/// let idx = NonNegativeArrayIndex::new(2);
///
/// assert_eq!(idx.get_index(), 2);
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct NonNegativeArrayIndex(u64);

/// The upper inclusive bound on index values.
pub const ARRAY_INDEX_ULIMIT: u64 = (1 << 53) - 1;
pub(crate) const ARRAY_INDEX_ULIMIT: u64 = (1 << 53) - 1;
impl TryFrom<u64> for NonNegativeArrayIndex {
type Error = ArrayIndexError;

Expand Down

0 comments on commit f423485

Please sign in to comment.