From c86fef91a0c104f8de15290523fbb7a95f55c695 Mon Sep 17 00:00:00 2001 From: PoiScript Date: Thu, 27 Jun 2019 14:03:42 +0800 Subject: [PATCH] feat(examples): add iter example --- examples/{custom_handler.rs => custom.rs} | 6 +++--- examples/iter.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) rename examples/{custom_handler.rs => custom.rs} (91%) create mode 100644 examples/iter.rs diff --git a/examples/custom_handler.rs b/examples/custom.rs similarity index 91% rename from examples/custom_handler.rs rename to examples/custom.rs index 2d1959e..1ae53d2 100644 --- a/examples/custom_handler.rs +++ b/examples/custom.rs @@ -29,9 +29,9 @@ impl From for MyError { } } -struct CustomHtmlHandler; +struct MyHtmlHandler; -impl HtmlHandler for CustomHtmlHandler { +impl HtmlHandler for MyHtmlHandler { fn start(&mut self, mut w: W, container: Container<'_>) -> Result<(), MyError> { let mut default_handler = DefaultHtmlHandler; match container { @@ -62,7 +62,7 @@ fn main() -> Result<(), MyError> { let contents = String::from_utf8(fs::read(&args[1])?)?; let mut writer = Vec::new(); - Org::parse(&contents).html(&mut writer, CustomHtmlHandler)?; + Org::parse(&contents).html(&mut writer, MyHtmlHandler)?; println!("{}", String::from_utf8(writer)?); } diff --git a/examples/iter.rs b/examples/iter.rs new file mode 100644 index 0000000..1f95f67 --- /dev/null +++ b/examples/iter.rs @@ -0,0 +1,19 @@ +use orgize::Org; +use std::env::args; +use std::fs; +use std::io::Result; + +fn main() -> Result<()> { + let args: Vec<_> = args().collect(); + + if args.len() < 2 { + eprintln!("Usage: {} ", args[0]); + } else { + let contents = String::from_utf8(fs::read(&args[1])?).unwrap(); + + for event in Org::parse(&contents).iter() { + println!("{:?}", event); + } + } + Ok(()) +}