xpath_reader 0.5.3

Provides a convenient API to read from XML using XPath expressions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2017-2018 Leonardo Schwarz <mail@leoschwarz.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! XPath based document parsing.

use errors::{Error, ErrorKind};
use expression::XPathExpression;
use std::borrow::{Borrow, Cow};
use sxd_document::parser::parse as sxd_parse;
use sxd_document::Package;
use sxd_xpath::nodeset::{Node, Nodeset};
use sxd_xpath::{Context, Value, XPath};
use util::Refable;

/// Convenience redefinition of the FromXml result type.
pub type FromXmlResult<T> = Result<T, Error>;

/// A value that can be deserialized from a XML reader.
pub trait FromXml
where
    Self: Sized,
{
    /// Read an instance of `Self` from the provided `reader`.
    ///
    /// The exact semantics of when this fails or succeeds are implementor
    /// defined. However for `Option<T>` a best effort approach should
    /// be followed, returning `Ok(None)` in absence of a value instead of
    /// an error.
    fn from_xml<'d>(reader: &'d Reader<'d>) -> FromXmlResult<Self>;
}

/// A helper trait to define two `FromXml` implementations at once.
///
/// In general you want to implement `FromXml` directly, however
/// sometimes you want to implement it for `T` and `Option<T>`.
/// In this case you can implement `FromXmlOptional` for `T` and
/// `FromXml` will be implemented for both types where `FromXml`
/// for `T` will fail if the your implementation returns None.
pub trait FromXmlOptional
where
    Self: Sized,
{
    /// `FromXml::from_xml` impl for `Option<T>`.
    fn from_xml_optional<'d>(reader: &'d Reader<'d>) -> FromXmlResult<Option<Self>>;
}

impl<T> FromXml for T
where
    T: FromXmlOptional,
{
    fn from_xml<'d>(reader: &'d Reader<'d>) -> FromXmlResult<Self> {
        // TODO: Better error message.
        T::from_xml_optional(reader).and_then(|opt| {
            opt.ok_or_else(|| {
                Error::custom_msg(format!("Missing value for type {:?}", stringify!(T)))
            })
        })
    }
}

impl<T> FromXml for Option<T>
where
    T: FromXmlOptional,
{
    fn from_xml<'d>(reader: &'d Reader<'d>) -> FromXmlResult<Self> {
        T::from_xml_optional(reader)
    }
}

enum Anchor<'d> {
    Nodeset(Nodeset<'d>),
    Root(Package),
}

/// XML element tree reader using XPath expressions.
///
/// # Anchor nodeset
///
/// An instance of `Reader` either contains a complete document, or
/// references an anchor nodeset (a "relative" reader).
/// There are two aspects for which this distinction is relevant:
///
/// 1) Relative expressions: If there is an anchor nodeset, relative
///    XPath expressions will be evaluated relative to the first
///    node in the nodeset, in document order.
/// 2) `FromXml` implementors can query the anchor nodeset to convert
///    multiple nodes into a single target value.
pub struct Reader<'d> {
    context: Refable<'d, Context<'d>>,
    anchor: Anchor<'d>,
}

impl<'d> Reader<'d> {
    /// Read the result of the XPath expression into a value of type `V`.
    pub fn read<'a, V, X>(&'d self, xpath_expr: X) -> Result<V, Error>
    where
        V: FromXml,
        X: Into<XPathExpression<'a>>,
    {
        let reader = self.with_nodeset_eval(xpath_expr)?;
        V::from_xml(&reader)
    }

    /// Construct a new reader for the specified XML document.
    ///
    /// A context can be specified to define custom functions,
    /// variables and namespaces.
    pub fn from_str(xml: &str, context: Option<&'d Context<'d>>) -> Result<Self, Error> {
        // TODO: Display all.
        let package =
            sxd_parse(xml).map_err(|e| Error::internal(format!("{}", e), ErrorKind::ParseXml))?;

        let context_refable = match context {
            Some(c) => Refable::Borrowed(c),
            None => Refable::Owned(Context::default()),
        };

        Ok(Reader {
            context: context_refable,
            anchor: Anchor::Root(package),
        })
    }

    /// Construct a new reader for the specified nodeset.
    ///
    /// Relative XPath expressions will then resolve to the first node
    /// in the nodeset.
    ///
    /// The nodeset can be obtained from a `Reader<'d>` with the
    /// `anchor_nodeset` and `anchor_node` methods.
    /// A context can be specified to define custom functions,
    /// variables and namespaces.
    ///
    /// Note: The nodeset can even be empty, which can be used by `FromXml`
    /// implementors to cover the absence of a value in some cases.
    pub fn from_nodeset(nodeset: Nodeset<'d>, context: Option<&'d Context<'d>>) -> Self {
        let context_refable = match context {
            Some(c) => Refable::Borrowed(c),
            None => Refable::Owned(Context::default()),
        };

        Reader {
            context: context_refable,
            anchor: Anchor::Nodeset(nodeset),
        }
    }

    /// Convenience method over `from_nodeset` when there is only one `Node` for
    /// the nodeset.
    pub fn from_node(node: Node<'d>, context: Option<&'d Context<'d>>) -> Self {
        let mut nodeset = Nodeset::new();
        nodeset.add(node);
        Self::from_nodeset(nodeset, context)
    }

    /// Creates a new `Reader` instance by evaluating an XPath expression and
    /// using the result nodeset as anchor nodeset.
    ///
    /// The current context will be passed to the new reader.
    pub fn with_nodeset_eval<'a, X>(&'d self, xpath_expr: X) -> Result<Self, Error>
    where
        X: Into<XPathExpression<'a>>,
    {
        let xpath = xpath_expr.into();
        match self.evaluate(&xpath)? {
            Value::Nodeset(nodeset) => Ok(Reader {
                context: self.context.clone_ref(),
                anchor: Anchor::Nodeset(nodeset),
            }),
            _ => Err(Error::internal(
                format!(
                    "XPath expression did not evaluate to nodeset: '{}'",
                    xpath.to_string()
                ),
                ErrorKind::EvalXPath,
            )),
        }
    }

    /// References the evaluation context of this Reader.
    pub fn context(&'d self) -> &'d Context<'d> {
        self.context.borrow()
    }

    /// Returns the anchor nodeset of the current reader.
    pub fn anchor_nodeset(&'d self) -> Cow<Nodeset<'d>> {
        match self.anchor {
            Anchor::Nodeset(ref nodeset) => Cow::Borrowed(nodeset),
            Anchor::Root(ref package) => {
                let mut nodeset = Nodeset::new();
                let root = package.as_document().root().clone();
                nodeset.add(Node::Root(root));
                Cow::Owned(nodeset)
            }
        }
    }

    /// Returns the first (in document order) node in the anchor nodeset.
    ///
    /// If the anchor nodeset is empty, `None` will be returned.
    pub fn anchor_node(&'d self) -> Option<Node<'d>> {
        match self.anchor {
            Anchor::Nodeset(ref nodeset) => nodeset.document_order_first(),
            Anchor::Root(ref package) => Some(package.as_document().root().clone().into()),
        }
    }

    fn evaluate<'a, X>(&'d self, xpath_expr: X) -> Result<Value<'d>, Error>
    where
        X: Into<XPathExpression<'a>>,
    {
        let xpath_expr = xpath_expr.into();
        let xpath = xpath_expr.parsed()?;
        // TODO: Error message.
        let anchor = self.anchor_node().ok_or_else(|| {
            let xpath_ref: &XPath = xpath.borrow();
            Error::internal(
                format!("Anchor node not found when evaluating: {:?}", xpath_ref),
                ErrorKind::EvalXPath,
            )
        })?;

        // Note: This is very ugly but otherwise does not compile.
        let xpath_ref: &XPath = xpath.borrow();
        xpath_ref
            .evaluate(self.context.borrow(), anchor)
            .map_err(|e| Error::internal(format!("{}", e), ErrorKind::EvalXPath))
    }
}

impl FromXml for String {
    fn from_xml<'d>(reader: &'d Reader<'d>) -> Result<Self, Error> {
        reader
            .anchor_node()
            .ok_or(Error::custom_msg("Missing (anchor) node."))
            .map(|n| n.string_value())
    }
}

impl FromXml for Option<String> {
    fn from_xml<'d>(reader: &'d Reader<'d>) -> Result<Self, Error> {
        Ok(reader.anchor_node().and_then(|node| {
            let s = node.string_value();
            if s.is_empty() {
                None
            } else {
                Some(s)
            }
        }))
    }
}

impl<T> FromXml for Vec<T>
where
    T: FromXml,
{
    fn from_xml<'d>(reader: &'d Reader<'d>) -> Result<Self, Error> {
        reader
            .anchor_nodeset()
            .document_order()
            .iter()
            .map(|node| {
                let reader = Reader::from_node(*node, Some(reader.context()));
                T::from_xml(&reader)
            })
            .collect()
    }
}

macro_rules! from_parse_str {
    ( $( $type:ty ),* ) => {
        $(
            impl FromXml for $type {
                fn from_xml<'d>(reader: &'d Reader<'d>) -> Result<Self, Error>
                {
                    let s = String::from_xml(reader)?;
                    s.parse::<$type>().map_err(|e| Error::custom_err(e))
                }
            }

            impl FromXml for Option<$type> {
                fn from_xml<'d>(reader: &'d Reader<'d>) -> Result<Self, Error>
                {
                    if let Some(s) = Option::<String>::from_xml(reader)? {
                        Ok(Some(s.parse::<$type>().map_err(|e| Error::custom_err(e))?))
                    } else {
                        Ok(None)
                    }
                }
            }
        )*
    }
}

from_parse_str!(f32, f64, u8, u16, u32, u64, i8, i16, i32, i64, bool);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn xpath_str_reader() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
                     <root><child name="Hello World"/></root>"#;
        let reader = Reader::from_str(xml, None).unwrap();
        assert_eq!(
            reader.evaluate(".//child/@name").unwrap().string(),
            "Hello World".to_string()
        );
    }

    #[test]
    fn string_from_xml() {
        let xml = r#"<?xml version="1.0"?>
                     <root><title>Hello World</title><empty/></root>"#;
        let reader = Reader::from_str(xml, None).unwrap();

        let title = reader.with_nodeset_eval("//title").unwrap();
        assert_eq!(String::from_xml(&title).unwrap(), "Hello World");
        assert_eq!(
            Option::<String>::from_xml(&title).unwrap(),
            Some("Hello World".to_string())
        );

        let empty = reader.with_nodeset_eval("//empty").unwrap();
        assert_eq!(String::from_xml(&empty).unwrap(), "");
        assert_eq!(Option::<String>::from_xml(&empty).unwrap(), None);

        let inexistent = reader.with_nodeset_eval("//inexistent").unwrap();
        assert!(String::from_xml(&inexistent).is_err());
        assert_eq!(Option::<String>::from_xml(&inexistent).unwrap(), None);
    }

    #[test]
    fn num_from_xml() {
        let xml = r#"<?xml version="1.0"?><root><float>-23.85</float><int>42</int></root>"#;
        let reader = Reader::from_str(xml, None).unwrap();

        let float = reader.with_nodeset_eval("//float").unwrap();
        let int = reader.with_nodeset_eval("//int").unwrap();

        assert_eq!(f32::from_xml(&float).unwrap(), -23.85f32);
        assert_eq!(f32::from_xml(&int).unwrap(), 42f32);
        assert_eq!(f64::from_xml(&float).unwrap(), -23.85f64);
        assert_eq!(f64::from_xml(&int).unwrap(), 42f64);

        assert_eq!(u8::from_xml(&int).unwrap(), 42u8);
        assert_eq!(u16::from_xml(&int).unwrap(), 42u16);
        assert_eq!(u32::from_xml(&int).unwrap(), 42u32);
        assert_eq!(u64::from_xml(&int).unwrap(), 42u64);

        assert_eq!(i8::from_xml(&int).unwrap(), 42i8);
        assert_eq!(i16::from_xml(&int).unwrap(), 42i16);
        assert_eq!(i32::from_xml(&int).unwrap(), 42i32);
        assert_eq!(i64::from_xml(&int).unwrap(), 42i64);
    }

    #[test]
    fn num_absent() {
        let xml = r#"<?xml version="1.0"?><root><float>-23.85</float><int>42</int></root>"#;
        let reader = Reader::from_str(xml, None).unwrap();

        let opt1: Option<f32> = reader.read("//float").unwrap();
        let opt2: Option<f32> = reader.read("//ffloat").unwrap();

        assert_eq!(opt1, Some(-23.85f32));
        assert_eq!(opt2, None);
    }

    #[test]
    fn bool_from_xml() {
        let xml = r#"<?xml version="1.0"?><root><t>true</t><f>false</f></root>"#;
        let reader = Reader::from_str(xml, None).unwrap();

        let t = reader.with_nodeset_eval("//t").unwrap();
        let f = reader.with_nodeset_eval("//f").unwrap();

        assert_eq!(bool::from_xml(&t).unwrap(), true);
        assert_eq!(bool::from_xml(&f).unwrap(), false);
    }

    #[test]
    fn vec_existent() {
        let xml = r#"<?xml version="1.0"?><book><tags><tag name="cyberpunk"/><tag name="sci-fi"/></tags></book>"#;
        let reader = Reader::from_str(xml, None).unwrap();

        let tags: Vec<String> = reader.read("//book/tags/tag/@name").unwrap();
        assert_eq!(tags, vec!["cyberpunk".to_string(), "sci-fi".to_string()]);
    }

    #[test]
    fn vec_non_existent() {
        let xml = r#"<?xml version="1.0"?><root><t>true</t><f>false</f></root>"#;
        let reader = Reader::from_str(xml, None).unwrap();

        let tags: Vec<String> = reader.read("//book/tags/tag/@name").unwrap();
        assert_eq!(tags, Vec::<String>::new());
    }
}