use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Collection {
Blog,
Notes,
Snippets,
Talks,
Projects,
Recipes,
Slides,
Tags,
Contexts,
Root,
}
impl Collection {
pub fn from_path(path: &str) -> Self {
let first_component = path.split('/').next().unwrap_or("");
match first_component {
"blog" => Collection::Blog,
"notes" => Collection::Notes,
"snippets" => Collection::Snippets,
"talks" => Collection::Talks,
"projects" => Collection::Projects,
"recipes" => Collection::Recipes,
"slides" => Collection::Slides,
"tags" => Collection::Tags,
"contexts" => Collection::Contexts,
_ => Collection::Root,
}
}
pub fn url_base(&self) -> &'static str {
match self {
Collection::Blog => "/blog/",
Collection::Notes => "/notes/",
Collection::Snippets => "/snippets/",
Collection::Talks => "/talks/",
Collection::Projects => "/projects/",
Collection::Recipes => "/recipes/",
Collection::Slides => "/slides/",
Collection::Tags => "/tags/",
Collection::Contexts => "/contexts/",
Collection::Root => "/",
}
}
pub fn is_hierarchical(&self) -> bool {
matches!(self, Collection::Notes)
}
pub fn supports_backlinks(&self) -> bool {
!matches!(self, Collection::Slides)
}
pub fn has_rss(&self) -> bool {
matches!(self, Collection::Blog)
}
pub fn is_date_sorted(&self) -> bool {
matches!(self, Collection::Blog)
}
pub fn layout_name(&self) -> &'static str {
match self {
Collection::Blog => "blog",
Collection::Notes => "notes",
Collection::Snippets => "snippets",
Collection::Talks => "talks",
Collection::Projects => "projects",
Collection::Recipes => "recipes",
Collection::Slides => "slides",
Collection::Tags => "tags",
Collection::Contexts => "contexts",
Collection::Root => "default",
}
}
pub fn directory_name(&self) -> &'static str {
match self {
Collection::Blog => "blog",
Collection::Notes => "notes",
Collection::Snippets => "snippets",
Collection::Talks => "talks",
Collection::Projects => "projects",
Collection::Recipes => "recipes",
Collection::Slides => "slides",
Collection::Tags => "tags",
Collection::Contexts => "contexts",
Collection::Root => "",
}
}
}
impl fmt::Display for Collection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.directory_name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Structure {
Flat,
Hierarchical,
}
impl Collection {
pub fn structure(&self) -> Structure {
if self.is_hierarchical() {
Structure::Hierarchical
} else {
Structure::Flat
}
}
}