use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Namespace {
Comments,
Review,
Todos,
Custom(String),
}
impl Namespace {
pub fn ref_path(&self) -> String {
match self {
Namespace::Comments => "refs/notes/comments".to_string(),
Namespace::Review => "refs/notes/review".to_string(),
Namespace::Todos => "refs/notes/todos".to_string(),
Namespace::Custom(name) => format!("refs/notes/{}", name),
}
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Self {
let stripped = s.strip_prefix("refs/notes/").unwrap_or(s);
match stripped {
"comments" => Namespace::Comments,
"review" => Namespace::Review,
"todos" => Namespace::Todos,
other => Namespace::Custom(other.to_string()),
}
}
}
impl fmt::Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Namespace::Comments => write!(f, "comments"),
Namespace::Review => write!(f, "review"),
Namespace::Todos => write!(f, "todos"),
Namespace::Custom(name) => write!(f, "{}", name),
}
}
}