use std::ops::Deref;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct ModelName(Arc<str>);
impl ModelName {
pub fn namespace_is(&self, namespace: &str) -> bool {
self.namespace() == Some(namespace)
}
pub fn namespace(&self) -> Option<&str> {
self.namespace_and_name().0
}
pub fn namespace_and_name(&self) -> (Option<&str>, &str) {
Self::split_as_namespace_and_name(&self.0)
}
pub(crate) fn split_as_namespace_and_name(model: &str) -> (Option<&str>, &str) {
if let Some(ns_idx) = model.find("::") {
let ns: &str = &model[..ns_idx];
let name: &str = &model[(ns_idx + 2)..];
(Some(ns), name)
} else {
(None, model)
}
}
}
impl From<ModelName> for String {
fn from(model_name: ModelName) -> Self {
model_name.0.to_string()
}
}
impl From<String> for ModelName {
fn from(s: String) -> Self {
Self(Arc::from(s))
}
}
impl From<&String> for ModelName {
fn from(s: &String) -> Self {
Self(Arc::from(s.as_str()))
}
}
impl From<&str> for ModelName {
fn from(s: &str) -> Self {
Self(Arc::from(s))
}
}
impl Deref for ModelName {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl PartialEq<str> for ModelName {
fn eq(&self, other: &str) -> bool {
&*self.0 == other
}
}
impl PartialEq<&str> for ModelName {
fn eq(&self, other: &&str) -> bool {
&*self.0 == *other
}
}
impl PartialEq<String> for ModelName {
fn eq(&self, other: &String) -> bool {
&*self.0 == other
}
}
impl PartialEq<ModelName> for str {
fn eq(&self, other: &ModelName) -> bool {
self == &*other.0
}
}
impl PartialEq<ModelName> for &str {
fn eq(&self, other: &ModelName) -> bool {
*self == &*other.0
}
}
impl PartialEq<ModelName> for String {
fn eq(&self, other: &ModelName) -> bool {
self == &*other.0
}
}
impl std::fmt::Display for ModelName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}