wx_core/helpers/
lazy_xml.rs1use std::fmt::{Debug, Formatter};
2use crate::{WxError, WxResult};
3use std::str::FromStr;
4use sxd_document::Package;
5use sxd_xpath::{Context, Factory, Value};
6
7pub struct LazyXML {
8 factory: Factory,
9 package: Package,
10}
11
12impl Debug for LazyXML {
13 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14 Debug::fmt(&self.package, f)
15 }
16}
17
18impl FromStr for LazyXML {
19 type Err = WxError;
20
21 fn from_str(s: &str) -> WxResult<Self> {
22 Ok(LazyXML { factory: Factory::new(), package: sxd_document::parser::parse(s)? })
23 }
24}
25impl LazyXML {
26 pub fn get_xpath(&self, xpath: &str) -> WxResult<Value> {
27 let xpath = match self.factory.build(xpath)? {
28 Some(s) => s,
29 None => Err(WxError::custom("invalid xpath"))?,
30 };
31 let context = Context::new();
32 Ok(xpath.evaluate(&context, self.package.as_document().root())?)
33 }
34}