parse_book_source/utils/
mod.rs1use crate::Result;
2use jsonpath_rust::JsonPath;
3use regex::{Captures, Regex};
4use serde_json::Value;
5use std::str::FromStr;
6
7pub fn replace_all(
8 re: &Regex,
9 haystack: &str,
10 mut replacement: impl FnMut(&Captures) -> Result<String>,
11) -> Result<String> {
12 let mut new = String::with_capacity(haystack.len());
13 let mut last_match = 0;
14 for caps in re.captures_iter(haystack) {
15 let m = caps.get(0).unwrap();
16 new.push_str(&haystack[last_match..m.start()]);
17 new.push_str(&replacement(&caps)?);
18 last_match = m.end();
19 }
20 new.push_str(&haystack[last_match..]);
21 Ok(new)
22}
23
24pub fn json_path(data: &Value, path: &str) -> Result<Value> {
25 Ok(JsonPath::from_str(path)?.find(data))
26}