Skip to content

Commit

Permalink
fix: change as_ to to_json_pointer to follow API guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
hiltontj committed Feb 1, 2024
1 parent ee1c605 commit 53ce390
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 18 additions & 0 deletions serde_json_path_core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,24 @@ impl<'a> LocatedNodeList<'a> {
///
/// To iterate over just locations, see [`locations`][LocatedNodeList::locations]. To iterate
/// over just nodes, see [`nodes`][LocatedNodeList::nodes].
///
/// # Example
/// ```rust
/// # use serde_json::{json, Value};
/// # use serde_json_path::JsonPath;
/// # fn main() -> Result<(), serde_json_path::ParseError> {
/// let value = json!({"foo": ["bar", "baz"]});
/// let path = JsonPath::parse("$.foo.*")?;
/// let pairs: Vec<(String, &Value)> = path
/// .query_located(&value)
/// .iter()
/// .map(|q| (q.location().to_string(), q.node()))
/// .collect();
/// assert_eq!(pairs[0], ("$['foo'][0]".to_owned(), &json!("bar")));
/// assert_eq!(pairs[1], ("$['foo'][1]".to_owned(), &json!("baz")));
/// # Ok(())
/// # }
/// ```
pub fn iter(&self) -> Iter<'_, LocatedNode<'a>> {
self.0.iter()
}
Expand Down
12 changes: 6 additions & 6 deletions serde_json_path_core/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ impl<'a> NormalizedPath<'a> {
/// .query_located(&value)
/// .exactly_one()?
/// .location()
/// .as_json_pointer();
/// .to_json_pointer();
/// *value.pointer_mut(&pointer).unwrap() = "bop".into();
/// assert_eq!(value, json!({"foo": ["bop", "baz"]}));
/// # Ok(())
/// # }
/// ```
///
/// [json-pointer]: https://datatracker.ietf.org/doc/html/rfc6901
pub fn as_json_pointer(&self) -> String {
pub fn to_json_pointer(&self) -> String {
self.0
.iter()
.map(PathElement::as_json_pointer)
.map(PathElement::to_json_pointer)
.fold(String::from(""), |mut acc, s| {
acc.push('/');
acc.push_str(&s);
Expand Down Expand Up @@ -219,7 +219,7 @@ pub enum PathElement<'a> {
}

impl<'a> PathElement<'a> {
fn as_json_pointer(&self) -> String {
fn to_json_pointer(&self) -> String {
match self {
PathElement::Name(s) => s.replace('~', "~0").replace('/', "~1"),
PathElement::Index(i) => i.to_string(),
Expand Down Expand Up @@ -334,7 +334,7 @@ mod tests {
PathElement::Index(42),
PathElement::Name("bar"),
]);
assert_eq!(np.as_json_pointer(), "/foo/42/bar",);
assert_eq!(np.to_json_pointer(), "/foo/42/bar");
}

#[test]
Expand All @@ -344,6 +344,6 @@ mod tests {
PathElement::Index(42),
PathElement::Name("baz/bop"),
]);
assert_eq!(np.as_json_pointer(), "/foo~0bar/42/baz~1bop",);
assert_eq!(np.to_json_pointer(), "/foo~0bar/42/baz~1bop");
}
}

0 comments on commit 53ce390

Please sign in to comment.