feat(export): add Default trait bound for Handler trait

This commit is contained in:
PoiScript 2019-10-09 10:19:12 +08:00
parent 3e68313095
commit 899773134f
5 changed files with 12 additions and 8 deletions

View file

@ -79,7 +79,7 @@ use std::convert::From;
use std::io::{Error as IOError, Write}; use std::io::{Error as IOError, Write};
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use orgize::export::{html::Escape, DefaultHtmlHandler, HtmlHandler}; use orgize::export::{DefaultHtmlHandler, HtmlHandler};
use orgize::{Element, Org}; use orgize::{Element, Org};
use slugify::slugify; use slugify::slugify;
@ -103,6 +103,7 @@ impl From<FromUtf8Error> for MyError {
} }
} }
#[derive(Default)]
struct MyHtmlHandler(DefaultHtmlHandler); struct MyHtmlHandler(DefaultHtmlHandler);
impl HtmlHandler<MyError> for MyHtmlHandler { impl HtmlHandler<MyError> for MyHtmlHandler {
@ -115,7 +116,7 @@ impl HtmlHandler<MyError> for MyHtmlHandler {
w, w,
"<h{0}><a id=\"{1}\" href=\"#{1}\">", "<h{0}><a id=\"{1}\" href=\"#{1}\">",
title.level, title.level,
slugify!(title.raw), slugify!(&title.raw),
)?; )?;
} }
} else { } else {
@ -137,8 +138,7 @@ impl HtmlHandler<MyError> for MyHtmlHandler {
fn main() -> Result<(), MyError> { fn main() -> Result<(), MyError> {
let mut writer = Vec::new(); let mut writer = Vec::new();
let mut handler = MyHtmlHandler::default();
let mut handler = MyHtmlHandler(DefaultHtmlHandler);
Org::parse("* title\n*section*").html_with_handler(&mut writer, &mut handler)?; Org::parse("* title\n*section*").html_with_handler(&mut writer, &mut handler)?;
assert_eq!( assert_eq!(

View file

@ -29,6 +29,7 @@ impl From<FromUtf8Error> for MyError {
} }
} }
#[derive(Default)]
struct MyHtmlHandler(DefaultHtmlHandler); struct MyHtmlHandler(DefaultHtmlHandler);
impl HtmlHandler<MyError> for MyHtmlHandler { impl HtmlHandler<MyError> for MyHtmlHandler {
@ -70,7 +71,7 @@ fn main() -> Result<(), MyError> {
let contents = String::from_utf8(fs::read(&args[1])?)?; let contents = String::from_utf8(fs::read(&args[1])?)?;
let mut writer = Vec::new(); let mut writer = Vec::new();
let mut handler = MyHtmlHandler(DefaultHtmlHandler); let mut handler = MyHtmlHandler::default();
Org::parse(&contents).html_with_handler(&mut writer, &mut handler)?; Org::parse(&contents).html_with_handler(&mut writer, &mut handler)?;
println!("{}", String::from_utf8(writer)?); println!("{}", String::from_utf8(writer)?);

View file

@ -36,7 +36,7 @@ impl<S: AsRef<str>> fmt::Display for Escape<S> {
} }
} }
pub trait HtmlHandler<E: From<Error>> { pub trait HtmlHandler<E: From<Error>>: Default {
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), E> { fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), E> {
use Element::*; use Element::*;
@ -199,6 +199,7 @@ pub trait HtmlHandler<E: From<Error>> {
} }
} }
#[derive(Default)]
pub struct DefaultHtmlHandler; pub struct DefaultHtmlHandler;
impl HtmlHandler<Error> for DefaultHtmlHandler {} impl HtmlHandler<Error> for DefaultHtmlHandler {}

View file

@ -3,7 +3,7 @@ use std::io::{Error, Write};
use crate::elements::{Element, Timestamp}; use crate::elements::{Element, Timestamp};
use crate::export::write_datetime; use crate::export::write_datetime;
pub trait OrgHandler<E: From<Error>> { pub trait OrgHandler<E: From<Error>>: Default {
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), E> { fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), E> {
use Element::*; use Element::*;
@ -233,6 +233,7 @@ fn write_timestamp<W: Write>(mut w: W, timestamp: &Timestamp) -> std::io::Result
Ok(()) Ok(())
} }
#[derive(Default)]
pub struct DefaultOrgHandler; pub struct DefaultOrgHandler;
impl OrgHandler<Error> for DefaultOrgHandler {} impl OrgHandler<Error> for DefaultOrgHandler {}

View file

@ -114,6 +114,7 @@
//! } //! }
//! } //! }
//! //!
//! #[derive(Default)]
//! struct MyHtmlHandler(DefaultHtmlHandler); //! struct MyHtmlHandler(DefaultHtmlHandler);
//! //!
//! impl HtmlHandler<MyError> for MyHtmlHandler { //! impl HtmlHandler<MyError> for MyHtmlHandler {
@ -148,7 +149,7 @@
//! //!
//! fn main() -> Result<(), MyError> { //! fn main() -> Result<(), MyError> {
//! let mut writer = Vec::new(); //! let mut writer = Vec::new();
//! let mut handler = MyHtmlHandler(DefaultHtmlHandler); //! let mut handler = MyHtmlHandler::default();
//! Org::parse("* title\n*section*").html_with_handler(&mut writer, &mut handler)?; //! Org::parse("* title\n*section*").html_with_handler(&mut writer, &mut handler)?;
//! //!
//! assert_eq!( //! assert_eq!(