json_predicate/predicate/
context.rs1use std::error::Error;
2
3use crate::json_path::{JSONPath, JSONPathError};
4
5#[derive(Default, Clone, Debug)]
8pub struct PredicateContext {
9 location: Option<JSONPath>,
10}
11
12#[derive(Debug, thiserror::Error)]
13pub enum PredicateContextError {
14 #[error("{0}")]
15 Deserialize(#[from] Box<dyn Error>),
16 #[error("{0}")]
17 JSONPath(#[from] JSONPathError),
18}
19
20impl From<JSONPath> for PredicateContext {
21 fn from(value: JSONPath) -> Self {
22 Self {
23 location: Some(value),
24 }
25 }
26}
27
28impl From<Option<JSONPath>> for PredicateContext {
29 fn from(value: Option<JSONPath>) -> Self {
30 Self { location: value }
31 }
32}
33
34impl PredicateContext {
35 pub fn new(path: String) -> Result<Self, PredicateContextError> {
36 let location = JSONPath::new(path)?;
37
38 Ok(Self {
39 location: Some(location),
40 })
41 }
42
43 pub fn final_path(&self, path: &Option<JSONPath>) -> Option<JSONPath> {
44 match (self.location.clone(), path.clone()) {
46 (None, None) => None,
47 (Some(path), None) | (None, Some(path)) => Some(path),
48 (Some(root), Some(path)) => Some(root + path),
49 }
50 }
51}