use crate::md_elem::{MdContext, MdElem};
use crate::output::{write_md, MdWriterOptions};
use crate::util::output::{Output, SimpleWrite};
use std::{fmt, io};
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MdWriter {
options: MdWriterOptions,
}
impl MdWriter {
pub fn with_options(options: MdWriterOptions) -> Self {
Self { options }
}
pub fn write<'md, W>(&self, ctx: &'md MdContext, nodes: &'md [MdElem], out: &mut W)
where
W: fmt::Write,
{
write_md(
self.options,
&mut Output::new(IoAdapter(out), self.options.text_width),
ctx,
nodes,
)
}
}
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IoAdapter<W>(pub W);
impl<W> From<W> for IoAdapter<W> {
fn from(value: W) -> Self {
Self(value)
}
}
impl<W: fmt::Write> SimpleWrite for IoAdapter<W> {
fn write_char(&mut self, ch: char) -> io::Result<()> {
self.0
.write_char(ch)
.map_err(|err| io::Error::other(format!("while writing char: {err}")))
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl<W: io::Write> fmt::Write for IoAdapter<W> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.0.write_all(s.as_bytes()).map_err(|_| fmt::Error)
}
}