Struct CommonMarkWriter

Source
pub struct CommonMarkWriter {
    pub options: WriterOptions,
    /* private fields */
}
Expand description

CommonMark writer

This struct is responsible for serializing AST nodes to CommonMark-compliant text.

Fields§

§options: WriterOptions

Writer options

Implementations§

Source§

impl CommonMarkWriter

Source

pub fn new() -> Self

Create a new CommonMark writer with default options

§Example
use cmark_writer::writer::CommonMarkWriter;
use cmark_writer::ast::Node;

let mut writer = CommonMarkWriter::new();
writer.write(&Node::Text("Hello".to_string())).unwrap();
assert_eq!(writer.into_string(), "Hello");
Source

pub fn with_options(options: WriterOptions) -> Self

Create a new CommonMark writer with specified options

§Parameters
  • options - Custom CommonMark formatting options
§Example
use cmark_writer::writer::CommonMarkWriter;
use cmark_writer::options::WriterOptions;

let options = WriterOptions {
    strict: true,
    hard_break_spaces: false,  // Use backslash for line breaks
    indent_spaces: 2,          // Use 2 spaces for indentation
    ..Default::default()       // Other options can be set as needed
};
let writer = CommonMarkWriter::with_options(options);
Source

pub fn write(&mut self, node: &Node) -> WriteResult<()>

Write an AST node as CommonMark format

§Parameters
  • node - The AST node to write
§Returns

If writing succeeds, returns Ok(()), otherwise returns Err(WriteError)

§Example
use cmark_writer::writer::CommonMarkWriter;
use cmark_writer::ast::Node;

let mut writer = CommonMarkWriter::new();
writer.write(&Node::Text("Hello".to_string())).unwrap();
Source

pub fn into_string(self) -> String

Get the generated CommonMark format text

Consumes the writer and returns the generated string

§Example
use cmark_writer::writer::CommonMarkWriter;
use cmark_writer::ast::Node;

let mut writer = CommonMarkWriter::new();
writer.write(&Node::Text("Hello".to_string())).unwrap();
let result = writer.into_string();
assert_eq!(result, "Hello");
Source

pub fn write_str(&mut self, s: &str) -> WriteResult<()>

Write a string to the output buffer

This method is provided for custom node implementations to use

Source

pub fn write_char(&mut self, c: char) -> WriteResult<()>

Write a character to the output buffer

This method is provided for custom node implementations to use

Trait Implementations§

Source§

impl Debug for CommonMarkWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CommonMarkWriter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.