replace_with call back function should be able to return an Error
gkorland opened this issue · comments
The current callback defined:
FnMut(&Value) -> Value
Allowing the callback to return a Result will allow the callback "abort" replace in case it can't be done.
FnMut(&Value) -> Result<Value, JsonPathError>
Perhaps a new method should be added
pub fn try_replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
where
F: FnMut(&Value) -> Result<Value, JsonPathError>,
looks good.
On the same topic I think it can also be useful to just do nothing.
Meaning if the callback code decides it doesn't want to make any effect it should be able to indicates it.
Currently, if the callback wants to maintain the same value it needs to clone it and return the cloned value.
That is how we've done it https://github.com/RedisLabsModules/RedisDoc/blob/4dc407d4682ce4b5569342c7136e7cffec68c153/src/redisjson.rs#L144
How about Option
instead of Result
?
None: skip,
Some: replace
pub fn try_replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
where
F: FnMut(&Value) -> Option<Value>,
May be an Option
of a Result
?
https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/option_result.html
pub fn try_replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
where
F: FnMut(&Value) -> Option<Result<Value, JsonPathError>>,
Second thought maybe we should F to get the Value and not a reference.
pub fn try_replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
where
F: FnMut(Value) -> Result<Value, JsonPathError>,
This way it will be able to return the same Value if no change needed or the and Error
The second example looks better to me.
I think I prefer to drop this issue for now