use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops::Deref;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct NamespaceIdent(Vec<String>);
impl NamespaceIdent {
pub fn new(parts: Vec<String>) -> Self {
assert!(!parts.is_empty(), "Namespace cannot be empty");
Self(parts)
}
pub fn from_strs(parts: &[&str]) -> Self {
Self::new(parts.iter().map(|s| s.to_string()).collect())
}
pub fn as_slice(&self) -> &[String] {
&self.0
}
pub fn into_vec(self) -> Vec<String> {
self.0
}
}
impl Deref for NamespaceIdent {
type Target = [String];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for NamespaceIdent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.join("."))
}
}
impl From<Vec<String>> for NamespaceIdent {
fn from(parts: Vec<String>) -> Self {
Self::new(parts)
}
}
impl<'a> From<&'a [&'a str]> for NamespaceIdent {
fn from(parts: &'a [&'a str]) -> Self {
Self::from_strs(parts)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TableIdent {
namespace: NamespaceIdent,
name: String,
}
impl TableIdent {
pub fn new(namespace: NamespaceIdent, name: String) -> Self {
assert!(!name.is_empty(), "Table name cannot be empty");
Self { namespace, name }
}
pub fn from_strs(namespace: &[&str], name: &str) -> Self {
Self::new(NamespaceIdent::from_strs(namespace), name.to_string())
}
pub fn namespace(&self) -> &NamespaceIdent {
&self.namespace
}
pub fn name(&self) -> &str {
&self.name
}
pub fn into_parts(self) -> (NamespaceIdent, String) {
(self.namespace, self.name)
}
}
impl fmt::Display for TableIdent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.namespace, self.name)
}
}