@@ -7,6 +7,9 @@ use alloc::{
7
7
vec:: Vec ,
8
8
} ;
9
9
use core:: { borrow:: Borrow , cmp:: Ordering , ops:: Deref , str:: FromStr } ;
10
+ use slice:: SlicePointer ;
11
+
12
+ mod slice;
10
13
11
14
/*
12
15
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
@@ -277,23 +280,35 @@ impl Pointer {
277
280
. map ( |s| unsafe { Self :: new_unchecked ( s) } )
278
281
}
279
282
280
- /// Attempts to get a `Token` by the index. Returns `None` if the index is
281
- /// out of bounds.
283
+ /// Attempts to get a `Token` or a segment of the `Pointer`, depending on
284
+ /// the type of index.
285
+ ///
286
+ /// Returns `None` if the index is out of bounds.
287
+ ///
288
+ /// Note that this operation is O(n).
282
289
///
283
290
/// ## Example
284
291
/// ```rust
285
292
/// use jsonptr::{Pointer, Token};
286
293
///
287
- /// let ptr = Pointer::from_static("/foo/bar");
294
+ /// let ptr = Pointer::from_static("/foo/bar/qux ");
288
295
/// assert_eq!(ptr.get(0), Some("foo".into()));
289
296
/// assert_eq!(ptr.get(1), Some("bar".into()));
290
- /// assert_eq!(ptr.get(2), None);
297
+ /// assert_eq!(ptr.get(3), None);
298
+ /// assert_eq!(ptr.get(..), Some(Pointer::from_static("/foo/bar/qux")));
299
+ /// assert_eq!(ptr.get(..1), Some(Pointer::from_static("/foo")));
300
+ /// assert_eq!(ptr.get(1..3), Some(Pointer::from_static("/bar/qux")));
301
+ /// assert_eq!(ptr.get(1..=2), Some(Pointer::from_static("/bar/qux")));
291
302
///
292
303
/// let ptr = Pointer::root();
293
304
/// assert_eq!(ptr.get(0), None);
305
+ /// assert_eq!(ptr.get(..), Some(Pointer::root()));
294
306
/// ```
295
- pub fn get ( & self , index : usize ) -> Option < Token > {
296
- self . tokens ( ) . nth ( index) . clone ( )
307
+ pub fn get < ' p , I > ( & ' p self , index : I ) -> Option < I :: Output >
308
+ where
309
+ I : SlicePointer < ' p > ,
310
+ {
311
+ index. get ( self )
297
312
}
298
313
299
314
/// Attempts to resolve a [`R::Value`] based on the path in this [`Pointer`].
0 commit comments