use jsonptr::{PointerBuf, Token};
use crate::json::{Path, PathArgs, Value};
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Context {
pub target: Value,
pub target_override: Option<Value>,
pub path: Path,
pub args: PathArgs,
}
impl Context {
pub fn new() -> Self {
Self::default()
}
pub fn with_target(self, target: Value) -> Self {
Self {
target_override: Some(target),
..self
}
}
pub fn with_path(self, path: impl AsRef<str>) -> Self {
let path = Path::new(
PointerBuf::parse(path.as_ref())
.expect("invalid JSON Pointer path")
.as_ptr(),
);
Self { path, ..self }
}
pub fn with_arg(self, key: impl AsRef<str>, value: impl Into<String>) -> Self {
let Self { mut args, .. } = self;
let value: String = value.into();
let encoded = Token::from(value).encoded().to_owned();
args.insert(key, encoded);
Self { args, ..self }
}
pub fn with_args(self, args: PathArgs) -> Self {
Self { args, ..self }
}
}