libsql_sys/
name.rs

1use bytes::Bytes;
2use std::fmt;
3use std::path::Path;
4
5#[derive(Clone, PartialEq, Eq, Hash)]
6pub struct NamespaceName(pub Bytes);
7
8impl fmt::Debug for NamespaceName {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        write!(f, "{self}")
11    }
12}
13
14impl Default for NamespaceName {
15    fn default() -> Self {
16        Self(Bytes::from_static(b"default"))
17    }
18}
19
20impl AsRef<str> for NamespaceName {
21    fn as_ref(&self) -> &str {
22        self.as_str()
23    }
24}
25
26impl From<&'static str> for NamespaceName {
27    fn from(value: &'static str) -> Self {
28        Self(Bytes::from_static(value.as_bytes()))
29    }
30}
31
32impl NamespaceName {
33    pub fn from_string(s: String) -> Self {
34        Self(Bytes::from(s))
35    }
36
37    pub fn as_str(&self) -> &str {
38        // Safety: the namespace is always valid UTF8
39        unsafe { std::str::from_utf8_unchecked(&self.0) }
40    }
41
42    pub fn as_slice(&self) -> &[u8] {
43        &self.0
44    }
45}
46
47impl fmt::Display for NamespaceName {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        self.as_str().fmt(f)
50    }
51}
52
53/// Translates a path to a namespace name
54pub trait NamespaceResolver: Send + Sync + 'static {
55    fn resolve(&self, path: &Path) -> NamespaceName;
56}
57
58impl<F: Fn(&Path) -> NamespaceName + Send + Sync + 'static> NamespaceResolver for F {
59    fn resolve(&self, path: &Path) -> NamespaceName {
60        (self)(path)
61    }
62}