Skip to content

Commit 8759d3a

Browse files
committed
ns: Add constructors and destructors for parity with Reader
1 parent 16168e1 commit 8759d3a

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/reader/ns_reader.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
//! [qualified names]: https://www.w3.org/TR/xml-names11/#dt-qualname
55
//! [expanded names]: https://www.w3.org/TR/xml-names11/#dt-expname
66
7-
use std::io::BufRead;
7+
use std::fs::File;
8+
use std::io::{BufRead, BufReader};
89
use std::ops::{Deref, DerefMut};
10+
use std::path::Path;
911

1012
use crate::errors::Result;
1113
use crate::events::Event;
@@ -29,6 +31,15 @@ pub struct NsReader<R> {
2931
pending_pop: bool,
3032
}
3133

34+
/// Builder methods
35+
impl<R> NsReader<R> {
36+
/// Creates a `NsReader` that reads from a reader.
37+
#[inline]
38+
pub fn from_reader(reader: R) -> Self {
39+
Self::new(Reader::from_reader(reader))
40+
}
41+
}
42+
3243
/// Private methods
3344
impl<R> NsReader<R> {
3445
#[inline]
@@ -96,6 +107,14 @@ impl<R> NsReader<R> {
96107

97108
/// Getters
98109
impl<R> NsReader<R> {
110+
/// Consumes `NsReader` returning the underlying reader
111+
///
112+
/// See the [`Reader::into_inner`] for examples
113+
#[inline]
114+
pub fn into_inner(self) -> R {
115+
self.reader.into_inner()
116+
}
117+
99118
/// Resolves a potentially qualified **element name** or **attribute name**
100119
/// into (namespace name, local name).
101120
///
@@ -386,6 +405,15 @@ impl<R: BufRead> NsReader<R> {
386405
}
387406
}
388407

408+
impl NsReader<BufReader<File>> {
409+
/// Creates an XML reader from a file path.
410+
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
411+
let file = File::open(path)?;
412+
let reader = BufReader::new(file);
413+
Ok(Self::from_reader(reader))
414+
}
415+
}
416+
389417
impl<'i> NsReader<&'i [u8]> {
390418
/// Creates an XML reader from a string slice.
391419
#[inline]

0 commit comments

Comments
 (0)