use serde::{Deserialize, Serialize};
use crate::config::EmbedConfig;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum NoteStatus {
Draft,
Staging,
PendingReview,
Live,
Deprecated,
Garbage,
}
impl NoteStatus {
pub fn can_transition_to(&self, target: NoteStatus) -> bool {
use NoteStatus::*;
matches!(
(self, target),
(Draft, PendingReview)
| (Draft, Garbage)
| (PendingReview, Live)
| (PendingReview, Garbage)
| (PendingReview, Staging)
| (Staging, Live)
| (Staging, Garbage)
| (Live, Deprecated)
| (Live, Garbage)
| (Deprecated, Live) | (Garbage, Live) )
}
pub fn is_visible_default(&self) -> bool {
matches!(self, NoteStatus::Live)
}
pub fn is_embeddable_default(&self) -> bool {
matches!(
self,
NoteStatus::Live | NoteStatus::PendingReview | NoteStatus::Staging
)
}
pub fn is_embeddable(&self, cfg: &EmbedConfig) -> bool {
match cfg.embeddable_status.as_ref() {
Some(allowed) => allowed.iter().any(|s| s == self.serde_kebab_repr()),
None => self.is_embeddable_default(),
}
}
fn serde_kebab_repr(&self) -> &'static str {
match self {
NoteStatus::Draft => "draft",
NoteStatus::Staging => "staging",
NoteStatus::PendingReview => "pending-review",
NoteStatus::Live => "live",
NoteStatus::Deprecated => "deprecated",
NoteStatus::Garbage => "garbage",
}
}
}
impl std::fmt::Display for NoteStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.serde_kebab_repr())
}
}