Skip to main content

gn_core/
namespace.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// The three built-in note namespaces. Extensible via `Custom`.
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub enum Namespace {
7    Comments,
8    Review,
9    Todos,
10    Custom(String),
11}
12
13impl Namespace {
14    /// Returns the full ref path for this namespace.
15    pub fn ref_path(&self) -> String {
16        match self {
17            Namespace::Comments => "refs/notes/comments".to_string(),
18            Namespace::Review => "refs/notes/review".to_string(),
19            Namespace::Todos => "refs/notes/todos".to_string(),
20            Namespace::Custom(name) => format!("refs/notes/{}", name),
21        }
22    }
23
24    /// Parses a string into a Namespace. Will match built-in names if possible.
25    #[allow(clippy::should_implement_trait)]
26    pub fn from_str(s: &str) -> Self {
27        // Strip the standard prefix if provided
28        let stripped = s.strip_prefix("refs/notes/").unwrap_or(s);
29        match stripped {
30            "comments" => Namespace::Comments,
31            "review" => Namespace::Review,
32            "todos" => Namespace::Todos,
33            other => Namespace::Custom(other.to_string()),
34        }
35    }
36}
37
38impl fmt::Display for Namespace {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Namespace::Comments => write!(f, "comments"),
42            Namespace::Review => write!(f, "review"),
43            Namespace::Todos => write!(f, "todos"),
44            Namespace::Custom(name) => write!(f, "{}", name),
45        }
46    }
47}