use crate::error::SourceRange;
use std::{collections::HashMap, fmt, path::PathBuf};
pub const WIKI_EXTENSION: &str = "mull";
pub const TITLE_MARKER: &str = "#";
pub const TITLE_PREFIX: &str = "# ";
pub const FILE_LINK_PREFIX: &str = "file:";
pub const DIRECTORY_LINK_PREFIX: &str = "dir:";
pub const HOME_TITLE: &str = "Home";
#[derive(Clone, Debug)]
pub enum Link {
Text {
title: String,
source_range: SourceRange,
},
File {
path: PathBuf,
source_range: SourceRange,
},
Directory {
path: PathBuf,
source_range: SourceRange,
},
}
#[derive(Clone, Debug)]
pub struct TextNode {
pub title: String, pub content: String, pub links: Vec<Link>,
pub depth: Option<usize>, #[allow(
dead_code,
reason = "Retained for diagnostics covering a complete text node."
)]
pub source_range: SourceRange, pub title_source_range: SourceRange, }
#[derive(Clone, Debug, Default)]
pub struct Wiki {
pub text_nodes: HashMap<String, TextNode>,
}
impl fmt::Display for TextNode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.content.is_empty() {
writeln!(formatter, "{TITLE_PREFIX}{}", self.title)
} else {
writeln!(
formatter,
"{TITLE_PREFIX}{}\n\n{}",
self.title,
self.content,
)
}
}
}
impl fmt::Display for Wiki {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut nodes = self.text_nodes.iter().collect::<Vec<_>>();
nodes.sort_by_key(|(title, node)| (node.depth.is_none(), node.depth, *title));
for (index, (_title, node)) in nodes.into_iter().enumerate() {
if index > 0 {
writeln!(formatter)?;
}
write!(formatter, "{node}")?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::{TextNode, Wiki};
use crate::error::SourceRange;
use std::collections::HashMap;
const SOURCE_RANGE: SourceRange = SourceRange { start: 0, end: 0 };
#[test]
fn node_display() {
let node = TextNode {
title: "Greeting".to_owned(),
content: "Hello, world!".to_owned(),
links: Vec::new(),
depth: None,
source_range: SOURCE_RANGE,
title_source_range: SOURCE_RANGE,
};
assert_eq!(node.to_string(), "# Greeting\n\nHello, world!\n");
}
#[test]
fn empty_node_display() {
let node = TextNode {
title: "Greeting".to_owned(),
content: String::new(),
links: Vec::new(),
depth: None,
source_range: SOURCE_RANGE,
title_source_range: SOURCE_RANGE,
};
assert_eq!(node.to_string(), "# Greeting\n");
}
#[test]
fn empty_node_wiki_display() {
let wiki = Wiki {
text_nodes: HashMap::from([(
"Greeting".to_owned(),
TextNode {
title: "Greeting".to_owned(),
content: String::new(),
links: Vec::new(),
depth: None,
source_range: SOURCE_RANGE,
title_source_range: SOURCE_RANGE,
},
)]),
};
assert_eq!(wiki.to_string(), "# Greeting\n");
}
#[test]
fn wiki_display() {
let wiki = Wiki {
text_nodes: HashMap::from([
(
"Greeting".to_owned(),
TextNode {
title: "Greeting".to_owned(),
content: "Hello, world!".to_owned(),
links: Vec::new(),
depth: Some(1),
source_range: SOURCE_RANGE,
title_source_range: SOURCE_RANGE,
},
),
(
"Home".to_owned(),
TextNode {
title: "Home".to_owned(),
content: "Check out the [Greeting].".to_owned(),
links: Vec::new(),
depth: Some(0),
source_range: SOURCE_RANGE,
title_source_range: SOURCE_RANGE,
},
),
(
"Orphan".to_owned(),
TextNode {
title: "Orphan".to_owned(),
content: String::new(),
links: Vec::new(),
depth: None,
source_range: SOURCE_RANGE,
title_source_range: SOURCE_RANGE,
},
),
]),
};
assert_eq!(
wiki.to_string(),
concat!(
"# Home\n\nCheck out the [Greeting].\n\n",
"# Greeting\n\nHello, world!\n\n",
"# Orphan\n",
),
);
}
#[test]
fn empty_wiki_display() {
assert_eq!(Wiki::default().to_string(), "");
}
}