use serde::{Deserialize, Serialize};
use crate::node::{Common, IntoNode, Node};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Code {
#[serde(flatten)]
pub common: Common,
pub source: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line_numbers: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub start_line: Option<u32>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub highlight_lines: Vec<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wrap: Option<bool>,
}
impl Code {
#[must_use]
pub fn new(source: impl Into<String>) -> Self {
Self {
common: Common::new(),
source: source.into(),
language: None,
line_numbers: None,
start_line: None,
highlight_lines: Vec::new(),
wrap: None,
}
}
#[must_use]
pub fn language(mut self, language: impl Into<String>) -> Self {
self.language = Some(language.into());
self
}
#[must_use]
pub fn line_numbers(mut self, line_numbers: bool) -> Self {
self.line_numbers = Some(line_numbers);
self
}
#[must_use]
pub fn start_line(mut self, start_line: u32) -> Self {
self.start_line = Some(start_line);
self
}
#[must_use]
pub fn highlight_lines(mut self, lines: impl IntoIterator<Item = u32>) -> Self {
self.highlight_lines = lines.into_iter().collect();
self
}
#[must_use]
pub fn wrap(mut self, wrap: bool) -> Self {
self.wrap = Some(wrap);
self
}
}
impl IntoNode for Code {}
impl From<Code> for Node {
fn from(c: Code) -> Self {
Node::Code(c)
}
}