Skip to content

Commit 3f811b3

Browse files
committed
fix formatting issues
1 parent b3bfc8e commit 3f811b3

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ extern crate pest;
9595

9696
use crate::query::queryable::Queryable;
9797
use crate::query::{Queried, QueryPath, QueryRef};
98-
use serde_json::Value;
98+
use serde_json::Value;
9999

100100
/// A trait for types that can be queried with JSONPath.
101101
pub trait JsonPath: Queryable {

src/query/queryable.rs

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::parser::errors::JsonPathError;
22
use crate::parser::model::{JpQuery, Segment, Selector};
33
use crate::parser::{parse_json_path, Parsed};
4-
use crate::query::{QueryPath, Queried};
4+
use crate::query::{Queried, QueryPath};
55
use crate::JsonPath;
66
use serde_json::Value;
77
use std::borrow::Cow;
@@ -141,34 +141,36 @@ where
141141
}
142142

143143
/// Deletes all elements matching the given JSONPath
144-
///
144+
///
145145
/// # Arguments
146146
/// * `path` - JSONPath string specifying elements to delete
147-
///
147+
///
148148
/// # Returns
149149
/// * `Ok(usize)` - Number of elements deleted
150150
/// * `Err(JsonPathError)` - If the path is invalid or deletion fails
151-
///
151+
///
152152
/// # Examples
153153
/// ```
154154
/// use serde_json::json;
155155
/// use jsonpath_rust::JsonPath;
156156
/// use crate::jsonpath_rust::query::queryable::Queryable;
157-
///
157+
///
158158
/// let mut data = json!({
159159
/// "users": [
160160
/// {"name": "Alice", "age": 30},
161161
/// {"name": "Bob", "age": 25},
162162
/// {"name": "Charlie", "age": 35}
163163
/// ]
164164
/// });
165-
///
165+
///
166166
/// // Delete users older than 30
167167
/// let deleted = data.delete_by_path("$.users[?(@.age > 30)]").unwrap();
168168
/// assert_eq!(deleted, 1);
169169
/// ```
170170
fn delete_by_path(&mut self, _path: &str) -> Queried<usize> {
171-
Err(JsonPathError::InvalidJsonPath("Deletion not supported".to_string()))
171+
Err(JsonPathError::InvalidJsonPath(
172+
"Deletion not supported".to_string(),
173+
))
172174
}
173175
}
174176

@@ -319,25 +321,25 @@ impl Queryable for Value {
319321
deletions.push(deletion_info);
320322
}
321323
}
322-
324+
323325
// Sort deletions to handle array indices correctly (delete from end to start)
324-
deletions.sort_by(|a, b|
325-
b.path_depth().cmp(&a.path_depth()).then_with(|| {
326-
match (a, b) {
326+
deletions.sort_by(|a, b| {
327+
b.path_depth()
328+
.cmp(&a.path_depth())
329+
.then_with(|| match (a, b) {
327330
(
328331
DeletionInfo::ArrayIndex { index: idx_a, .. },
329332
DeletionInfo::ArrayIndex { index: idx_b, .. },
330333
) => idx_b.cmp(idx_a),
331334
_ => std::cmp::Ordering::Equal,
332-
}
333-
})
334-
);
335-
335+
})
336+
});
337+
336338
// Perform deletions
337-
let deleted_count = deletions
338-
.iter()
339-
.try_fold(0, |c, d| execute_deletion(self, d).map(|deleted| if deleted { c + 1 } else { c }))?;
340-
339+
let deleted_count = deletions.iter().try_fold(0, |c, d| {
340+
execute_deletion(self, d).map(|deleted| if deleted { c + 1 } else { c })
341+
})?;
342+
341343
Ok(deleted_count)
342344
}
343345
}
@@ -359,10 +361,8 @@ impl DeletionInfo {
359361
fn path_depth(&self) -> usize {
360362
match self {
361363
DeletionInfo::Root => 0,
362-
DeletionInfo::ObjectField { parent_path, .. } |
363-
DeletionInfo::ArrayIndex { parent_path, .. } => {
364-
parent_path.matches('/').count()
365-
}
364+
DeletionInfo::ObjectField { parent_path, .. }
365+
| DeletionInfo::ArrayIndex { parent_path, .. } => parent_path.matches('/').count(),
366366
}
367367
}
368368
}
@@ -371,16 +371,16 @@ fn parse_deletion_path(query_path: &str) -> Result<Option<DeletionInfo>, JsonPat
371371
if query_path == "$" {
372372
return Ok(Some(DeletionInfo::Root));
373373
}
374-
374+
375375
let JpQuery { segments } = parse_json_path(query_path)?;
376-
376+
377377
if segments.is_empty() {
378378
return Ok(None);
379379
}
380-
380+
381381
let mut parent_path = String::new();
382382
let mut segments_iter = segments.iter().peekable();
383-
383+
384384
while let Some(segment) = segments_iter.next() {
385385
if segments_iter.peek().is_some() {
386386
// Not the last segment, add to parent path
@@ -422,7 +422,7 @@ fn parse_deletion_path(query_path: &str) -> Result<Option<DeletionInfo>, JsonPat
422422
}
423423
}
424424
}
425-
425+
426426
Ok(None)
427427
}
428428

@@ -432,20 +432,23 @@ fn execute_deletion(value: &mut Value, deletion: &DeletionInfo) -> Queried<bool>
432432
*value = Value::Null;
433433
Ok(true)
434434
}
435-
DeletionInfo::ObjectField { parent_path, field_name } => {
435+
DeletionInfo::ObjectField {
436+
parent_path,
437+
field_name,
438+
} => {
436439
let parent = if parent_path.is_empty() {
437440
value
438441
} else {
439442
value.pointer_mut(parent_path).ok_or_else(|| {
440443
JsonPathError::InvalidJsonPath("Parent path not found".to_string())
441444
})?
442445
};
443-
446+
444447
if let Some(obj) = parent.as_object_mut() {
445448
Ok(obj.remove(field_name).is_some())
446449
} else {
447450
Err(JsonPathError::InvalidJsonPath(
448-
"Parent is not an object".to_string()
451+
"Parent is not an object".to_string(),
449452
))
450453
}
451454
}
@@ -457,7 +460,7 @@ fn execute_deletion(value: &mut Value, deletion: &DeletionInfo) -> Queried<bool>
457460
JsonPathError::InvalidJsonPath("Parent path not found".to_string())
458461
})?
459462
};
460-
463+
461464
if let Some(arr) = parent.as_array_mut() {
462465
if *index < arr.len() {
463466
arr.remove(*index);
@@ -467,7 +470,7 @@ fn execute_deletion(value: &mut Value, deletion: &DeletionInfo) -> Queried<bool>
467470
}
468471
} else {
469472
Err(JsonPathError::InvalidJsonPath(
470-
"Parent is not an array".to_string()
473+
"Parent is not an array".to_string(),
471474
))
472475
}
473476
}
@@ -647,33 +650,33 @@ mod tests {
647650
"bob": {"age": 25}
648651
}
649652
});
650-
653+
651654
let deleted = data.delete_by_path("$.users.alice").unwrap();
652655
assert_eq!(deleted, 1);
653-
656+
654657
let expected = json!({
655658
"users": {
656659
"bob": {"age": 25}
657660
}
658661
});
659662
assert_eq!(data, expected);
660663
}
661-
664+
662665
#[test]
663666
fn test_delete_array_element() {
664667
let mut data = json!({
665668
"numbers": [1, 2, 3, 4, 5]
666669
});
667-
670+
668671
let deleted = data.delete_by_path("$.numbers[2]").unwrap();
669672
assert_eq!(deleted, 1);
670-
673+
671674
let expected = json!({
672675
"numbers": [1, 2, 4, 5]
673676
});
674677
assert_eq!(data, expected);
675678
}
676-
679+
677680
#[test]
678681
fn test_delete_multiple_elements() {
679682
let mut data = json!({
@@ -684,19 +687,19 @@ mod tests {
684687
{"name": "David", "age": 22}
685688
]
686689
});
687-
690+
688691
// Delete users older than 24
689692
let deleted = data.delete_by_path("$.users[?(@.age > 24)]").unwrap();
690693
assert_eq!(deleted, 3);
691-
694+
692695
let expected = json!({
693696
"users": [
694697
{"name": "David", "age": 22}
695698
]
696699
});
697700
assert_eq!(data, expected);
698701
}
699-
702+
700703
#[test]
701704
fn test_delete_nested_fields() {
702705
let mut data = json!({
@@ -708,10 +711,12 @@ mod tests {
708711
}
709712
}
710713
});
711-
712-
let deleted = data.delete_by_path("$.company.departments.marketing").unwrap();
714+
715+
let deleted = data
716+
.delete_by_path("$.company.departments.marketing")
717+
.unwrap();
713718
assert_eq!(deleted, 1);
714-
719+
715720
let expected = json!({
716721
"company": {
717722
"departments": {
@@ -722,31 +727,31 @@ mod tests {
722727
});
723728
assert_eq!(data, expected);
724729
}
725-
730+
726731
#[test]
727732
fn test_delete_nonexistent_path() {
728733
let mut data = json!({
729734
"test": "value"
730735
});
731-
736+
732737
let deleted = data.delete_by_path("$.nonexistent").unwrap();
733738
assert_eq!(deleted, 0);
734-
739+
735740
// Data should remain unchanged
736741
let expected = json!({
737742
"test": "value"
738743
});
739744
assert_eq!(data, expected);
740745
}
741-
746+
742747
#[test]
743748
fn test_delete_root() {
744749
let mut data = json!({
745750
"test": "value"
746751
});
747-
752+
748753
let deleted = data.delete_by_path("$").unwrap();
749754
assert_eq!(deleted, 1);
750755
assert_eq!(data, Value::Null);
751756
}
752-
}
757+
}

0 commit comments

Comments
 (0)