use std::fmt::{Display, Formatter, Write};
use std::str::FromStr;
use crate::namespaceable_name::NamespaceableName;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KeywordParseError {
MissingColonPrefix,
EmptyNamespace,
EmptyName,
}
impl Display for KeywordParseError {
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
match self {
KeywordParseError::MissingColonPrefix => {
write!(f, "keyword must start with ':'")
}
KeywordParseError::EmptyNamespace => {
write!(f, "keyword namespace cannot be empty")
}
KeywordParseError::EmptyName => {
write!(f, "keyword name cannot be empty")
}
}
}
}
impl std::error::Error for KeywordParseError {}
#[macro_export]
macro_rules! kw {
( : $ns:ident $(. $nss:ident)+ / $nn:ident $(. $nns:ident)+ ) => {
$crate::Keyword::namespaced(
concat!(stringify!($ns) $(, ".", stringify!($nss))*),
concat!(stringify!($nn) $(, ".", stringify!($nns))*),
)
};
( : $ns:ident $(. $nss:ident)+ / $nn:ident ) => {
$crate::Keyword::namespaced(
concat!(stringify!($ns) $(, ".", stringify!($nss))*),
stringify!($nn)
)
};
( : $ns:ident $(. $nss:ident)+ / $nn:ident $(- $nnh:ident)+ ) => {
$crate::Keyword::namespaced(
concat!(stringify!($ns) $(, ".", stringify!($nss))*),
concat!(stringify!($nn) $(, "-", stringify!($nnh))*)
)
};
( : $ns:ident / $nn:ident $(. $nns:ident)+ ) => {
$crate::Keyword::namespaced(
stringify!($ns),
concat!(stringify!($nn) $(, ".", stringify!($nns))*),
)
};
( : $ns:ident / $nn:ident ) => {
$crate::Keyword::namespaced(
stringify!($ns),
stringify!($nn)
)
};
( : $ns:ident / $nn:ident $(- $nnh:ident)+ ) => {
$crate::Keyword::namespaced(
stringify!($ns),
concat!(stringify!($nn) $(, "-", stringify!($nnh))*)
)
};
( : $ns:ident $(- $nsh:ident)+ / $nn:ident ) => {
$crate::Keyword::namespaced(
concat!(stringify!($ns) $(, "-", stringify!($nsh))*),
stringify!($nn)
)
};
( : $n:ident ) => {
$crate::Keyword::plain(
stringify!($n)
)
};
( : $n:ident $(- $nh:ident)+ ) => {
$crate::Keyword::plain(
concat!(stringify!($n) $(, "-", stringify!($nh))*)
)
};
}
#[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(serde::Serialize, serde::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 FromStr for Keyword {
type Err = KeywordParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s
.strip_prefix(':')
.ok_or(KeywordParseError::MissingColonPrefix)?;
match s.split_once('/') {
Some((ns, name)) => {
if ns.is_empty() {
return Err(KeywordParseError::EmptyNamespace);
}
if name.is_empty() {
return Err(KeywordParseError::EmptyName);
}
Ok(Keyword::namespaced(ns, name))
}
None => {
if s.is_empty() {
return Err(KeywordParseError::EmptyName);
}
Ok(Keyword::plain(s))
}
}
}
}
impl TryFrom<&str> for Keyword {
type Error = KeywordParseError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Keyword::from_str(s)
}
}
impl Display for Keyword {
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
f.write_char(':')?;
self.0.fmt(f)
}
}
#[test]
fn test_kw_macro() {
assert_eq!(kw!(:test/name), Keyword::namespaced("test", "name"));
assert_eq!(kw!(:ns/_name), Keyword::namespaced("ns", "_name"));
assert_eq!(
kw!(:db.type/keyword),
Keyword::namespaced("db.type", "keyword")
);
assert_eq!(kw!(:name), Keyword::plain("name"));
assert_eq!(kw!(:last-name), Keyword::plain("last-name"));
assert_eq!(kw!(:foo/bar-baz), Keyword::namespaced("foo", "bar-baz"));
assert_eq!(kw!(:my-ns/attr), Keyword::namespaced("my-ns", "attr"));
}