use std::mem::discriminant;
#[derive(Debug, PartialEq)]
pub struct Document {
pub(crate) sections: Vec<Section>,
}
impl Document {
pub fn sections(self) -> Vec<Section> {
self.sections
}
}
#[derive(Debug, PartialEq)]
pub enum Block {
Line(Line),
EmptyLine(EmptyLine),
Ruler(Ruler),
}
#[derive(Debug, PartialEq)]
pub struct Line {
pub nodes: Vec<InlineNode>,
pub alignment: Alignment,
}
impl Default for Line {
fn default() -> Self {
Self::new()
}
}
impl Line {
fn new() -> Self {
Self {
nodes: Vec::new(),
alignment: Alignment::default(),
}
}
pub(crate) fn add_node(&mut self, node: InlineNode) {
self.nodes.push(node);
}
}
#[derive(Debug, PartialEq)]
pub struct Section {
pub level: u8,
pub header: Option<Line>,
pub blocks: Vec<Block>,
}
impl Default for Section {
fn default() -> Self {
Self::new()
}
}
impl Section {
fn new() -> Self {
Self {
level: 0,
header: None,
blocks: Vec::new(),
}
}
pub(crate) fn add_to_header(&mut self, node: InlineNode) {
if let Some(header) = self.header.as_mut() {
header.add_node(node);
} else {
self.header = Some(Line::new());
self.add_to_header(node);
}
}
pub(crate) fn set_header_alignment(&mut self, alignment: Alignment) {
if let Some(header) = self.header.as_mut() {
header.alignment = alignment;
}
}
}
#[derive(Debug, PartialEq, Default)]
pub struct EmptyLine {
pub style: StyleSet,
}
#[derive(Debug, PartialEq)]
pub struct Ruler {
pub symbol: Option<String>,
pub style: StyleSet,
}
impl Default for Ruler {
fn default() -> Self {
Self::new()
}
}
impl Ruler {
fn new() -> Self {
Self {
symbol: None,
style: StyleSet::default(),
}
}
}
#[derive(Debug, PartialEq)]
pub enum InlineNode {
Text { style: StyleSet, text: String },
Newline { style: StyleSet },
Link { style: StyleSet, link: Link },
Field { style: StyleSet, field: Field },
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum Alignment {
#[default]
Default,
Left,
Center,
Right,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct StyleSet(pub Vec<Style>);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Style {
Bold,
Italic,
Underline,
ForegroundColor(String),
BackgroundColor(String),
}
impl StyleSet {
fn pos_of(&self, modifier: &Style) -> Option<usize> {
self.0
.iter()
.position(|m| discriminant(m) == discriminant(modifier))
}
pub(crate) fn set(&mut self, modifier: Style) {
self.unset(&modifier);
self.0.push(modifier);
}
pub(crate) fn unset(&mut self, modifier: &Style) {
while let Some(i) = self.pos_of(modifier) {
self.0.remove(i);
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Link {
pub label: Option<String>,
pub url: String,
pub fields: Vec<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Field {
pub kind: FieldKind,
pub name: String,
pub value: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FieldKind {
Checkbox { pre_checked: bool },
Radio { pre_checked: bool },
Input { hidden: bool, size: Option<u8> },
}