From 00b2c8f3c02be39aa3e821cb0d3741116b29ffac Mon Sep 17 00:00:00 2001 From: Justin Abrahms Date: Fri, 14 Jul 2023 12:33:17 -0700 Subject: [PATCH] Move to common trait for any exports --- examples/custom.rs | 6 +++--- src/export/html.rs | 17 ++++++----------- src/export/mod.rs | 12 +++++++++--- src/export/org.rs | 9 ++------- src/lib.rs | 16 ++++++++-------- src/org.rs | 43 +++++++++++++------------------------------ 6 files changed, 41 insertions(+), 62 deletions(-) diff --git a/examples/custom.rs b/examples/custom.rs index 3f650ff..fc7e21a 100644 --- a/examples/custom.rs +++ b/examples/custom.rs @@ -5,7 +5,7 @@ use std::io::{Error as IOError, Write}; use std::result::Result; use std::string::FromUtf8Error; -use orgize::export::{DefaultHtmlHandler, HtmlHandler}; +use orgize::export::{DefaultHtmlHandler, ExportHandler}; use orgize::{Element, Org}; use slugify::slugify; @@ -32,7 +32,7 @@ impl From for MyError { #[derive(Default)] struct MyHtmlHandler(DefaultHtmlHandler); -impl HtmlHandler for MyHtmlHandler { +impl ExportHandler for MyHtmlHandler { fn start(&mut self, mut w: W, element: &Element) -> Result<(), MyError> { if let Element::Title(title) = element { if title.level > 6 { @@ -72,7 +72,7 @@ fn main() -> Result<(), MyError> { let mut writer = Vec::new(); let mut handler = MyHtmlHandler::default(); - Org::parse(&contents).write_html_custom(&mut writer, &mut handler)?; + Org::parse(&contents).write(&mut writer, &mut handler)?; println!("{}", String::from_utf8(writer)?); } diff --git a/src/export/html.rs b/src/export/html.rs index e0b0bd1..4500e84 100644 --- a/src/export/html.rs +++ b/src/export/html.rs @@ -4,7 +4,7 @@ use std::io::{Error, Result as IOResult, Write}; use jetscii::{bytes, BytesConst}; use crate::elements::{Element, Table, TableCell, TableRow, Timestamp}; -use crate::export::write_datetime; +use crate::export::{write_datetime, ExportHandler}; /// A wrapper for escaping sensitive characters in html. /// @@ -49,16 +49,11 @@ impl> fmt::Display for HtmlEscape { } } -pub trait HtmlHandler>: Default { - fn start(&mut self, w: W, element: &Element) -> Result<(), E>; - fn end(&mut self, w: W, element: &Element) -> Result<(), E>; -} - /// Default Html Handler #[derive(Default)] pub struct DefaultHtmlHandler; -impl HtmlHandler for DefaultHtmlHandler { +impl ExportHandler for DefaultHtmlHandler { fn start(&mut self, mut w: W, element: &Element) -> IOResult<()> { match element { // container elements @@ -304,7 +299,7 @@ mod syntect_handler { /// /// } /// ``` - pub struct SyntectHtmlHandler, H: HtmlHandler> { + pub struct SyntectHtmlHandler, H: ExportHandler> { /// syntax set, default is `SyntaxSet::load_defaults_newlines()` pub syntax_set: SyntaxSet, /// theme set, default is `ThemeSet::load_defaults()` @@ -319,7 +314,7 @@ mod syntect_handler { pub error_type: PhantomData, } - impl, H: HtmlHandler> SyntectHtmlHandler { + impl, H: ExportHandler> SyntectHtmlHandler { pub fn new(inner: H) -> Self { SyntectHtmlHandler { inner, @@ -339,7 +334,7 @@ mod syntect_handler { } } - impl, H: HtmlHandler> Default for SyntectHtmlHandler { + impl, H: ExportHandler> Default for SyntectHtmlHandler { fn default() -> Self { SyntectHtmlHandler { syntax_set: SyntaxSet::load_defaults_newlines(), @@ -352,7 +347,7 @@ mod syntect_handler { } } - impl, H: HtmlHandler> HtmlHandler for SyntectHtmlHandler { + impl, H: ExportHandler> ExportHandler for SyntectHtmlHandler { fn start(&mut self, mut w: W, element: &Element) -> Result<(), E> { match element { Element::InlineSrc(inline_src) => write!( diff --git a/src/export/mod.rs b/src/export/mod.rs index 4eddfba..c25a7da 100644 --- a/src/export/mod.rs +++ b/src/export/mod.rs @@ -5,12 +5,18 @@ mod org; #[cfg(feature = "syntect")] pub use html::SyntectHtmlHandler; -pub use html::{DefaultHtmlHandler, HtmlEscape, HtmlHandler}; -pub use org::{DefaultOrgHandler, OrgHandler}; +pub use html::{DefaultHtmlHandler, HtmlEscape}; +pub use org::{DefaultOrgHandler}; use std::io::{Error, Write}; -use crate::elements::Datetime; +use crate::elements::{Datetime, Element}; + +pub trait ExportHandler>: Default { + fn start(&mut self, writer: W, element: &Element) -> Result<(), E>; + fn end(&mut self, writer: W, element: &Element) -> Result<(), E>; +} + pub(crate) fn write_datetime( mut w: W, diff --git a/src/export/org.rs b/src/export/org.rs index 44c0a5c..28460ee 100644 --- a/src/export/org.rs +++ b/src/export/org.rs @@ -1,17 +1,12 @@ use std::io::{Error, Result as IOResult, Write}; use crate::elements::{Clock, Element, Table, Timestamp}; -use crate::export::write_datetime; - -pub trait OrgHandler>: Default { - fn start(&mut self, w: W, element: &Element) -> Result<(), E>; - fn end(&mut self, w: W, element: &Element) -> Result<(), E>; -} +use crate::export::{write_datetime, ExportHandler}; #[derive(Default)] pub struct DefaultOrgHandler; -impl OrgHandler for DefaultOrgHandler { +impl ExportHandler for DefaultOrgHandler { fn start(&mut self, mut w: W, element: &Element) -> IOResult<()> { match element { // container elements diff --git a/src/lib.rs b/src/lib.rs index 0033ea8..35171e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,13 +74,13 @@ //! ); //! ``` //! -//! # Render html with custom `HtmlHandler` +//! # Render html with custom `ExportHandler` //! -//! To customize html rendering, simply implementing [`HtmlHandler`] trait and passing -//! it to the [`Org::write_html_custom`] function. +//! To customize html rendering, simply implementing [`ExportHandler`] trait and passing +//! it to the [`Org::write`] function. //! -//! [`HtmlHandler`]: export/trait.HtmlHandler.html -//! [`Org::write_html_custom`]: struct.Org.html#method.write_html_custom +//! [`ExportHandler`]: export/trait.ExportHandler.html +//! [`Org::write`]: struct.Org.html#method.write //! //! The following code demonstrates how to add a id for every headline and return //! own error type while rendering. @@ -90,7 +90,7 @@ //! use std::io::{Error as IOError, Write}; //! use std::string::FromUtf8Error; //! -//! use orgize::export::{DefaultHtmlHandler, HtmlHandler}; +//! use orgize::export::{DefaultHtmlHandler, ExportHandler}; //! use orgize::{Element, Org}; //! use slugify::slugify; //! @@ -117,7 +117,7 @@ //! #[derive(Default)] //! struct MyHtmlHandler(DefaultHtmlHandler); //! -//! impl HtmlHandler for MyHtmlHandler { +//! impl ExportHandler for MyHtmlHandler { //! fn start(&mut self, mut w: W, element: &Element) -> Result<(), MyError> { //! if let Element::Title(title) = element { //! if title.level > 6 { @@ -150,7 +150,7 @@ //! fn main() -> Result<(), MyError> { //! let mut writer = Vec::new(); //! let mut handler = MyHtmlHandler::default(); -//! Org::parse("* title\n*section*").write_html_custom(&mut writer, &mut handler)?; +//! Org::parse("* title\n*section*").write(&mut writer, &mut handler)?; //! //! assert_eq!( //! String::from_utf8(writer)?, diff --git a/src/org.rs b/src/org.rs index 37c06fc..5ac3a5e 100644 --- a/src/org.rs +++ b/src/org.rs @@ -5,7 +5,7 @@ use std::ops::{Index, IndexMut}; use crate::{ config::{ParseConfig, DEFAULT_CONFIG}, elements::{Element, Keyword}, - export::{DefaultHtmlHandler, DefaultOrgHandler, HtmlHandler, OrgHandler}, + export::{DefaultHtmlHandler, DefaultOrgHandler, ExportHandler}, parsers::{blank_lines_count, parse_container, Container, OwnedArena}, }; @@ -109,20 +109,12 @@ impl<'a> Org<'a> { }) } - /// Writes an `Org` struct as html format. - pub fn write_html(&self, writer: W) -> Result<(), Error> - where - W: Write, - { - self.write_html_custom(writer, &mut DefaultHtmlHandler) - } - - /// Writes an `Org` struct as html format with custom `HtmlHandler`. - pub fn write_html_custom(&self, mut writer: W, handler: &mut H) -> Result<(), E> + /// Writes the document using the given {ExportHandler} + pub fn write(&self, mut writer: W, handler: &mut X) -> Result<(), E> where W: Write, E: From, - H: HtmlHandler, + X: ExportHandler, { for event in self.iter() { match event { @@ -134,29 +126,20 @@ impl<'a> Org<'a> { Ok(()) } + /// Writes an `Org` struct as html format. + pub fn write_html(&self, writer: W) -> Result<(), Error> + where + W: Write, + { + self.write(writer, &mut DefaultHtmlHandler) + } + /// Writes an `Org` struct as org format. pub fn write_org(&self, writer: W) -> Result<(), Error> where W: Write, { - self.write_org_custom(writer, &mut DefaultOrgHandler) - } - - /// Writes an `Org` struct as org format with custom `OrgHandler`. - pub fn write_org_custom(&self, mut writer: W, handler: &mut H) -> Result<(), E> - where - W: Write, - E: From, - H: OrgHandler, - { - for event in self.iter() { - match event { - Event::Start(element) => handler.start(&mut writer, element)?, - Event::End(element) => handler.end(&mut writer, element)?, - } - } - - Ok(()) + self.write(writer, &mut DefaultOrgHandler) } }