use crate::error::{WriteError, WriteResult};
use std::any::Any;
pub trait NodeContent: std::fmt::Debug + Send + Sync {
fn is_block(&self) -> bool;
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
pub trait NodeClone: NodeContent {
fn clone_box(&self) -> Box<dyn NodeContent>;
fn eq_box(&self, other: &dyn NodeContent) -> bool;
}
pub trait CustomNode: NodeClone + super::formatting::CommonMarkRenderable {
fn html_render(&self, writer: &mut crate::writer::HtmlWriter) -> WriteResult<()> {
writer
.raw_html(&format!(
"<!-- HTML rendering not implemented for {} -->",
self.type_name()
))
.map_err(WriteError::from)
}
fn attributes(&self) -> Option<&std::collections::HashMap<String, String>> {
None
}
fn supports_capability(&self, capability: &str) -> bool {
match capability {
"commonmark" => true,
"html" => false,
_ => false,
}
}
}
pub trait Writer {
fn write_str(&mut self, s: &str) -> WriteResult<()>;
fn write_char(&mut self, c: char) -> WriteResult<()>;
fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> WriteResult<()> {
self.write_str(&args.to_string())
}
}
impl Writer for crate::writer::CommonMarkWriter {
fn write_str(&mut self, s: &str) -> WriteResult<()> {
self.write_str(s)
}
fn write_char(&mut self, c: char) -> WriteResult<()> {
self.write_char(c)
}
}
impl Writer for crate::writer::HtmlWriter {
fn write_str(&mut self, s: &str) -> WriteResult<()> {
self.write_str(s).map_err(WriteError::from)
}
fn write_char(&mut self, c: char) -> WriteResult<()> {
self.write_str(&c.to_string()).map_err(WriteError::from)
}
}