use super::super::annotation::Annotation;
use super::references::ReferenceInline;
pub type InlineContent = Vec<InlineNode>;
#[derive(Debug, Clone, PartialEq)]
pub enum InlineNode {
Plain {
text: String,
annotations: Vec<Annotation>,
},
Strong {
content: InlineContent,
annotations: Vec<Annotation>,
},
Emphasis {
content: InlineContent,
annotations: Vec<Annotation>,
},
Code {
text: String,
annotations: Vec<Annotation>,
},
Math {
text: String,
annotations: Vec<Annotation>,
},
Reference {
data: ReferenceInline,
annotations: Vec<Annotation>,
},
}
impl InlineNode {
pub fn plain(text: String) -> Self {
InlineNode::Plain {
text,
annotations: Vec::new(),
}
}
pub fn code(text: String) -> Self {
InlineNode::Code {
text,
annotations: Vec::new(),
}
}
pub fn math(text: String) -> Self {
InlineNode::Math {
text,
annotations: Vec::new(),
}
}
pub fn strong(content: InlineContent) -> Self {
InlineNode::Strong {
content,
annotations: Vec::new(),
}
}
pub fn emphasis(content: InlineContent) -> Self {
InlineNode::Emphasis {
content,
annotations: Vec::new(),
}
}
pub fn reference(data: ReferenceInline) -> Self {
InlineNode::Reference {
data,
annotations: Vec::new(),
}
}
pub fn as_plain(&self) -> Option<&str> {
match self {
InlineNode::Plain { text, .. } => Some(text),
InlineNode::Code { text, .. } => Some(text),
InlineNode::Math { text, .. } => Some(text),
_ => None,
}
}
pub fn children(&self) -> Option<&InlineContent> {
match self {
InlineNode::Strong { content, .. } | InlineNode::Emphasis { content, .. } => {
Some(content)
}
_ => None,
}
}
pub fn is_plain(&self) -> bool {
matches!(self, InlineNode::Plain { .. })
}
pub fn annotations(&self) -> &[Annotation] {
match self {
InlineNode::Plain { annotations, .. }
| InlineNode::Strong { annotations, .. }
| InlineNode::Emphasis { annotations, .. }
| InlineNode::Code { annotations, .. }
| InlineNode::Math { annotations, .. }
| InlineNode::Reference { annotations, .. } => annotations,
}
}
pub fn annotations_mut(&mut self) -> &mut Vec<Annotation> {
match self {
InlineNode::Plain { annotations, .. }
| InlineNode::Strong { annotations, .. }
| InlineNode::Emphasis { annotations, .. }
| InlineNode::Code { annotations, .. }
| InlineNode::Math { annotations, .. }
| InlineNode::Reference { annotations, .. } => annotations,
}
}
pub fn with_annotation(mut self, annotation: Annotation) -> Self {
self.annotations_mut().push(annotation);
self
}
pub fn with_annotations(mut self, mut annotations: Vec<Annotation>) -> Self {
self.annotations_mut().append(&mut annotations);
self
}
}