use std::fmt::{Display, Formatter, Write};
use crate::namespaceable_name::NamespaceableName;
#[macro_export]
macro_rules! ns_keyword {
($ns: expr, $name: expr) => {{
$crate::Keyword::namespaced($ns, $name)
}};
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub struct PlainSymbol(pub String);
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub struct NamespacedSymbol(NamespaceableName);
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Keyword(NamespaceableName);
impl PlainSymbol {
pub fn plain<T>(name: T) -> Self
where
T: Into<String>,
{
let n = name.into();
assert!(!n.is_empty(), "Symbols cannot be unnamed.");
PlainSymbol(n)
}
pub fn name(&self) -> &str {
if self.is_src_symbol() || self.is_var_symbol() {
&self.0[1..]
} else {
&self.0
}
}
#[inline]
pub fn is_var_symbol(&self) -> bool {
self.0.starts_with('?')
}
#[inline]
pub fn is_src_symbol(&self) -> bool {
self.0.starts_with('$')
}
}
impl NamespacedSymbol {
pub fn namespaced<N, T>(namespace: N, name: T) -> Self
where
N: AsRef<str>,
T: AsRef<str>,
{
let r = namespace.as_ref();
assert!(
!r.is_empty(),
"Namespaced symbols cannot have an empty non-null namespace."
);
NamespacedSymbol(NamespaceableName::namespaced(r, name))
}
#[inline]
pub fn name(&self) -> &str {
self.0.name()
}
#[inline]
pub fn namespace(&self) -> &str {
self.0.namespace().unwrap()
}
#[inline]
pub fn components(&self) -> (&str, &str) {
self.0.components()
}
}
impl Keyword {
pub fn plain<T>(name: T) -> Self
where
T: Into<String>,
{
Keyword(NamespaceableName::plain(name))
}
}
impl Keyword {
pub fn namespaced<N, T>(namespace: N, name: T) -> Self
where
N: AsRef<str>,
T: AsRef<str>,
{
let r = namespace.as_ref();
assert!(
!r.is_empty(),
"Namespaced keywords cannot have an empty non-null namespace."
);
Keyword(NamespaceableName::namespaced(r, name))
}
#[inline]
pub fn name(&self) -> &str {
self.0.name()
}
#[inline]
pub fn namespace(&self) -> Option<&str> {
self.0.namespace()
}
#[inline]
pub fn components(&self) -> (&str, &str) {
self.0.components()
}
#[inline]
pub fn is_backward(&self) -> bool {
self.0.is_backward()
}
#[inline]
pub fn is_forward(&self) -> bool {
self.0.is_forward()
}
#[inline]
pub fn is_namespaced(&self) -> bool {
self.0.is_namespaced()
}
pub fn to_reversed(&self) -> Keyword {
Keyword(self.0.to_reversed())
}
pub fn unreversed(&self) -> Option<Keyword> {
if self.is_backward() {
Some(self.to_reversed())
} else {
None
}
}
}
impl Display for PlainSymbol {
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
self.0.fmt(f)
}
}
impl Display for NamespacedSymbol {
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
self.0.fmt(f)
}
}
impl Display for Keyword {
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
f.write_char(':')?;
self.0.fmt(f)
}
}
#[test]
fn test_ns_keyword_macro() {
assert_eq!(
ns_keyword!("test", "name").to_string(),
Keyword::namespaced("test", "name").to_string()
);
assert_eq!(
ns_keyword!("ns", "_name").to_string(),
Keyword::namespaced("ns", "_name").to_string()
);
}