cmark_writer/ast/
custom.rs

1//! Custom node definitions for the CommonMark AST.
2
3use crate::error::WriteResult;
4use std::any::Any;
5
6/// Trait for implementing custom node behavior
7pub trait CustomNode: std::fmt::Debug + Send + Sync {
8    /// Write the custom node content to the provided writer
9    fn write(&self, writer: &mut dyn CustomNodeWriter) -> WriteResult<()>;
10
11    /// Clone the custom node
12    fn clone_box(&self) -> Box<dyn CustomNode>;
13
14    /// Check if two custom nodes are equal
15    fn eq_box(&self, other: &dyn CustomNode) -> bool;
16
17    /// Whether the custom node is a block element
18    fn is_block(&self) -> bool;
19
20    /// Convert to Any for type casting
21    fn as_any(&self) -> &dyn Any;
22
23    /// Convert to mutable Any for type casting
24    fn as_any_mut(&mut self) -> &mut dyn Any;
25
26    /// Get the type name of the custom node for pattern matching
27    fn type_name(&self) -> &'static str {
28        std::any::type_name::<Self>()
29    }
30}
31
32/// Trait for custom node writer implementation
33pub trait CustomNodeWriter {
34    /// Write a string to the output
35    fn write_str(&mut self, s: &str) -> std::fmt::Result;
36
37    /// Write a character to the output
38    fn write_char(&mut self, c: char) -> std::fmt::Result;
39}
40
41// Implement Clone for Box<dyn CustomNode>
42impl Clone for Box<dyn CustomNode> {
43    fn clone(&self) -> Self {
44        self.clone_box()
45    }
46}
47
48// Implement PartialEq for Box<dyn CustomNode>
49impl PartialEq for Box<dyn CustomNode> {
50    fn eq(&self, other: &Self) -> bool {
51        self.eq_box(&**other)
52    }
53}