use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Namespace {
World,
Work,
Code,
Research,
Self_,
}
impl Namespace {
pub const ALL: [Namespace; 5] = [
Namespace::World,
Namespace::Work,
Namespace::Code,
Namespace::Research,
Namespace::Self_,
];
pub fn as_str(&self) -> &'static str {
match self {
Namespace::World => "world",
Namespace::Work => "work",
Namespace::Code => "code",
Namespace::Research => "research",
Namespace::Self_ => "self",
}
}
pub fn from_str_loose(s: &str) -> Option<Namespace> {
match s.to_lowercase().as_str() {
"world" => Some(Namespace::World),
"work" => Some(Namespace::Work),
"code" | "codegraph" => Some(Namespace::Code),
"research" => Some(Namespace::Research),
"self" | "self_" => Some(Namespace::Self_),
_ => None,
}
}
}
impl fmt::Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_namespace_roundtrip() {
for ns in Namespace::ALL {
let s = ns.as_str();
let parsed = Namespace::from_str_loose(s).unwrap();
assert_eq!(ns, parsed);
}
}
#[test]
fn test_namespace_case_insensitive() {
assert_eq!(Namespace::from_str_loose("WORLD"), Some(Namespace::World));
assert_eq!(Namespace::from_str_loose("Self"), Some(Namespace::Self_));
assert_eq!(Namespace::from_str_loose("CODE"), Some(Namespace::Code));
}
#[test]
fn test_namespace_codegraph_alias() {
assert_eq!(
Namespace::from_str_loose("codegraph"),
Some(Namespace::Code)
);
}
#[test]
fn test_namespace_unknown() {
assert_eq!(Namespace::from_str_loose("unknown"), None);
}
#[test]
fn test_namespace_all_has_five() {
assert_eq!(Namespace::ALL.len(), 5);
assert_eq!(Namespace::ALL[0], Namespace::World);
assert_eq!(Namespace::ALL[1], Namespace::Work);
assert_eq!(Namespace::ALL[2], Namespace::Code);
assert_eq!(Namespace::ALL[3], Namespace::Research);
assert_eq!(Namespace::ALL[4], Namespace::Self_);
}
#[test]
fn test_code_namespace_parquet_roundtrip() {
let ns = Namespace::Code;
let serialized = ns.as_str();
assert_eq!(serialized, "code");
let deserialized = Namespace::from_str_loose(serialized).unwrap();
assert_eq!(ns, deserialized);
}
}