Skip to content

Commit

Permalink
feat: convenience method from_writer
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsier committed Jan 22, 2021
1 parent f281845 commit 1d243aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use kml::{Kml, KmlWriter, types::{AltitudeMode, Coord, Point}};
let kml = Kml::Point(Point::new(1., 1., None));

let mut buf = Vec::new();
let mut writer = KmlWriter::new(quick_xml::Writer::new(&mut buf));
let mut writer = KmlWriter::from_writer(&mut buf);
writer.write(&kml).unwrap();
```

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! let kml = Kml::Point(Point::new(1., 1., None));
//!
//! let mut buf = Vec::new();
//! let mut writer = KmlWriter::new(quick_xml::Writer::new(&mut buf));
//! let mut writer = KmlWriter::from_writer(&mut buf);
//! writer.write(&kml).unwrap();
//! ```
//!
Expand Down
22 changes: 20 additions & 2 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ where
W: Write,
T: CoordType + FromStr + Default + fmt::Display,
{
/// Creates `KmlWriter` from an input that implements `Write`
///
/// # Example
///
/// ```
/// use std::str;
/// use quick_xml;
/// use kml::{Kml, KmlWriter, types::{AltitudeMode, Coord, Point}};
///
/// let kml = Kml::Point(Point::new(1., 1., None));
///
/// let mut buf = Vec::new();
/// let mut writer = KmlWriter::<_, f64>::from_writer(&mut buf);
/// ```
pub fn from_writer(w: W) -> KmlWriter<W, T> {
KmlWriter::new(quick_xml::Writer::new(w))
}

pub fn new(writer: quick_xml::Writer<W>) -> KmlWriter<W, T> {
KmlWriter {
writer,
Expand All @@ -46,7 +64,7 @@ where
/// let kml = Kml::Point(Point::new(1., 1., None));
///
/// let mut buf = Vec::new();
/// let mut writer = KmlWriter::new(quick_xml::Writer::new(&mut buf));
/// let mut writer = KmlWriter::from_writer(&mut buf);
/// writer.write(&kml).unwrap();
/// ```
pub fn write(&mut self, kml: &Kml<T>) -> Result<(), Error> {
Expand Down Expand Up @@ -439,7 +457,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = Vec::new();
KmlWriter::new(quick_xml::Writer::new(&mut buf))
KmlWriter::from_writer(&mut buf)
.write(self)
.map_err(|_| fmt::Error)
.and_then(|_| f.write_str(str::from_utf8(&buf).unwrap()))
Expand Down

0 comments on commit 1d243aa

Please sign in to comment.