2018-05-11 16:52:17 +00:00
|
|
|
// Copyright 2018 Mozilla
|
2017-01-11 21:51:34 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use std::fmt::{Display, Formatter, Write};
|
2018-05-29 14:45:53 +00:00
|
|
|
|
2020-08-06 03:03:58 +00:00
|
|
|
use crate::namespaceable_name::NamespaceableName;
|
2017-01-28 22:18:17 +00:00
|
|
|
|
2018-02-01 17:17:07 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! ns_keyword {
|
|
|
|
($ns: expr, $name: expr) => {{
|
2018-05-11 16:52:17 +00:00
|
|
|
$crate::Keyword::namespaced($ns, $name)
|
2020-01-14 15:46:21 +00:00
|
|
|
}};
|
2018-02-01 17:17:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 21:51:34 +00:00
|
|
|
/// A simplification of Clojure's Symbol.
|
2020-01-14 15:46:21 +00:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
|
2017-01-11 21:51:34 +00:00
|
|
|
pub struct PlainSymbol(pub String);
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
|
2018-05-11 16:52:17 +00:00
|
|
|
pub struct NamespacedSymbol(NamespaceableName);
|
2017-01-11 21:51:34 +00:00
|
|
|
|
|
|
|
/// A keyword is a symbol, optionally with a namespace, that prints with a leading colon.
|
|
|
|
/// This concept is imported from Clojure, as it features in EDN and the query
|
|
|
|
/// syntax that we use.
|
|
|
|
///
|
|
|
|
/// Clojure's constraints are looser than ours, allowing empty namespaces or
|
|
|
|
/// names:
|
|
|
|
///
|
|
|
|
/// ```clojure
|
|
|
|
/// user=> (keyword "" "")
|
|
|
|
/// :/
|
|
|
|
/// user=> (keyword "foo" "")
|
|
|
|
/// :foo/
|
|
|
|
/// user=> (keyword "" "bar")
|
|
|
|
/// :/bar
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// We think that's nonsense, so we only allow keywords like `:bar` and `:foo/bar`,
|
|
|
|
/// with both namespace and main parts containing no whitespace and no colon or slash:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use edn::symbols::Keyword;
|
2018-05-11 16:52:17 +00:00
|
|
|
/// let bar = Keyword::plain("bar"); // :bar
|
|
|
|
/// let foo_bar = Keyword::namespaced("foo", "bar"); // :foo/bar
|
|
|
|
/// assert_eq!("bar", bar.name());
|
|
|
|
/// assert_eq!(None, bar.namespace());
|
|
|
|
/// assert_eq!("bar", foo_bar.name());
|
|
|
|
/// assert_eq!(Some("foo"), foo_bar.namespace());
|
2017-01-11 21:51:34 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If you're not sure whether your input is well-formed, you should use a
|
|
|
|
/// parser or a reader function first to validate. TODO: implement `read`.
|
|
|
|
///
|
|
|
|
/// Callers are expected to follow these rules:
|
|
|
|
/// http://www.clojure.org/reference/reader#_symbols
|
|
|
|
///
|
|
|
|
/// Future: fast equality (interning?) for keywords.
|
|
|
|
///
|
2020-01-14 15:46:21 +00:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
|
2018-02-09 17:55:19 +00:00
|
|
|
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
2018-05-11 16:52:17 +00:00
|
|
|
pub struct Keyword(NamespaceableName);
|
2017-01-11 21:51:34 +00:00
|
|
|
|
|
|
|
impl PlainSymbol {
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn plain<T>(name: T) -> Self
|
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
2017-01-25 22:06:19 +00:00
|
|
|
let n = name.into();
|
|
|
|
assert!(!n.is_empty(), "Symbols cannot be unnamed.");
|
2017-01-11 21:51:34 +00:00
|
|
|
|
2017-01-28 22:18:17 +00:00
|
|
|
PlainSymbol(n)
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
2017-02-01 22:34:51 +00:00
|
|
|
|
|
|
|
/// Return the name of the symbol without any leading '?' or '$'.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use edn::symbols::PlainSymbol;
|
2018-05-11 16:52:17 +00:00
|
|
|
/// assert_eq!("foo", PlainSymbol::plain("?foo").name());
|
|
|
|
/// assert_eq!("foo", PlainSymbol::plain("$foo").name());
|
|
|
|
/// assert_eq!("!foo", PlainSymbol::plain("!foo").name());
|
2017-02-01 22:34:51 +00:00
|
|
|
/// ```
|
2018-05-11 16:52:17 +00:00
|
|
|
pub fn name(&self) -> &str {
|
2017-02-01 22:34:51 +00:00
|
|
|
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('$')
|
|
|
|
}
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NamespacedSymbol {
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn namespaced<N, T>(namespace: N, name: T) -> Self
|
|
|
|
where
|
|
|
|
N: AsRef<str>,
|
|
|
|
T: AsRef<str>,
|
|
|
|
{
|
2018-05-11 16:52:17 +00:00
|
|
|
let r = namespace.as_ref();
|
2020-01-14 15:46:21 +00:00
|
|
|
assert!(
|
|
|
|
!r.is_empty(),
|
|
|
|
"Namespaced symbols cannot have an empty non-null namespace."
|
|
|
|
);
|
2018-05-11 16:52:17 +00:00
|
|
|
NamespacedSymbol(NamespaceableName::namespaced(r, name))
|
|
|
|
}
|
2017-01-29 06:51:46 +00:00
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
self.0.name()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn namespace(&self) -> &str {
|
|
|
|
self.0.namespace().unwrap()
|
|
|
|
}
|
2017-01-11 21:51:34 +00:00
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
#[inline]
|
2020-01-23 18:16:19 +00:00
|
|
|
pub fn components(&self) -> (&str, &str) {
|
2018-05-11 16:52:17 +00:00
|
|
|
self.0.components()
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keyword {
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn plain<T>(name: T) -> Self
|
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
2018-05-11 16:52:17 +00:00
|
|
|
Keyword(NamespaceableName::plain(name))
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
impl Keyword {
|
|
|
|
/// Creates a new `Keyword`.
|
2017-01-28 20:01:15 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-05-11 16:52:17 +00:00
|
|
|
/// # use edn::symbols::Keyword;
|
|
|
|
/// let keyword = Keyword::namespaced("foo", "bar");
|
2017-01-28 20:01:15 +00:00
|
|
|
/// assert_eq!(keyword.to_string(), ":foo/bar");
|
|
|
|
/// ```
|
2018-02-01 17:17:07 +00:00
|
|
|
///
|
|
|
|
/// See also the `kw!` macro in the main `mentat` crate.
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn namespaced<N, T>(namespace: N, name: T) -> Self
|
|
|
|
where
|
|
|
|
N: AsRef<str>,
|
|
|
|
T: AsRef<str>,
|
|
|
|
{
|
2018-05-11 16:52:17 +00:00
|
|
|
let r = namespace.as_ref();
|
2020-01-14 15:46:21 +00:00
|
|
|
assert!(
|
|
|
|
!r.is_empty(),
|
|
|
|
"Namespaced keywords cannot have an empty non-null namespace."
|
|
|
|
);
|
2018-05-11 16:52:17 +00:00
|
|
|
Keyword(NamespaceableName::namespaced(r, name))
|
|
|
|
}
|
2017-01-11 21:51:34 +00:00
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
self.0.name()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn namespace(&self) -> Option<&str> {
|
|
|
|
self.0.namespace()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-23 18:16:19 +00:00
|
|
|
pub fn components(&self) -> (&str, &str) {
|
2018-05-11 16:52:17 +00:00
|
|
|
self.0.components()
|
2017-01-28 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
/// Whether this `Keyword` should be interpreted in reverse order. For example,
|
2017-01-28 20:01:15 +00:00
|
|
|
/// the two following snippets are identical:
|
2017-01-31 01:39:32 +00:00
|
|
|
///
|
|
|
|
/// ```edn
|
2017-01-28 20:01:15 +00:00
|
|
|
/// [?y :person/friend ?x]
|
|
|
|
/// [?x :person/hired ?y]
|
|
|
|
///
|
|
|
|
/// [?y :person/friend ?x]
|
|
|
|
/// [?y :person/_hired ?x]
|
2017-01-31 01:39:32 +00:00
|
|
|
/// ```
|
2017-01-28 20:01:15 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-05-11 16:52:17 +00:00
|
|
|
/// # use edn::symbols::Keyword;
|
|
|
|
/// assert!(!Keyword::namespaced("foo", "bar").is_backward());
|
|
|
|
/// assert!(Keyword::namespaced("foo", "_bar").is_backward());
|
2017-01-28 20:01:15 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn is_backward(&self) -> bool {
|
2018-05-11 16:52:17 +00:00
|
|
|
self.0.is_backward()
|
2017-01-28 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
/// Whether this `Keyword` should be interpreted in forward order.
|
|
|
|
/// See `symbols::Keyword::is_backward`.
|
2017-01-28 20:01:15 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-05-11 16:52:17 +00:00
|
|
|
/// # use edn::symbols::Keyword;
|
|
|
|
/// assert!(Keyword::namespaced("foo", "bar").is_forward());
|
|
|
|
/// assert!(!Keyword::namespaced("foo", "_bar").is_forward());
|
2017-01-28 20:01:15 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn is_forward(&self) -> bool {
|
2018-05-11 16:52:17 +00:00
|
|
|
self.0.is_forward()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_namespaced(&self) -> bool {
|
|
|
|
self.0.is_namespaced()
|
2017-01-28 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
/// Returns a `Keyword` with the same namespace and a
|
|
|
|
/// 'backward' name. See `symbols::Keyword::is_backward`.
|
|
|
|
///
|
|
|
|
/// Returns a forward name if passed a reversed keyword; i.e., this
|
|
|
|
/// function is its own inverse.
|
2017-01-28 20:01:15 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-05-11 16:52:17 +00:00
|
|
|
/// # use edn::symbols::Keyword;
|
|
|
|
/// let nsk = Keyword::namespaced("foo", "bar");
|
2017-01-28 20:01:15 +00:00
|
|
|
/// assert!(!nsk.is_backward());
|
|
|
|
/// assert_eq!(":foo/bar", nsk.to_string());
|
|
|
|
///
|
2017-02-01 22:34:51 +00:00
|
|
|
/// let reversed = nsk.to_reversed();
|
2017-01-28 20:01:15 +00:00
|
|
|
/// assert!(reversed.is_backward());
|
|
|
|
/// assert_eq!(":foo/_bar", reversed.to_string());
|
|
|
|
/// ```
|
2018-05-11 16:52:17 +00:00
|
|
|
pub fn to_reversed(&self) -> Keyword {
|
|
|
|
Keyword(self.0.to_reversed())
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
2017-06-07 20:37:40 +00:00
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
/// If this `Keyword` is 'backward' (see `symbols::Keyword::is_backward`),
|
2017-06-07 20:37:40 +00:00
|
|
|
/// return `Some('forward name')`; otherwise, return `None`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-05-11 16:52:17 +00:00
|
|
|
/// # use edn::symbols::Keyword;
|
|
|
|
/// let nsk = Keyword::namespaced("foo", "bar");
|
2017-06-07 20:37:40 +00:00
|
|
|
/// assert_eq!(None, nsk.unreversed());
|
|
|
|
///
|
|
|
|
/// let reversed = nsk.to_reversed();
|
|
|
|
/// assert_eq!(Some(nsk), reversed.unreversed());
|
|
|
|
/// ```
|
2018-05-11 16:52:17 +00:00
|
|
|
pub fn unreversed(&self) -> Option<Keyword> {
|
2017-06-07 20:37:40 +00:00
|
|
|
if self.is_backward() {
|
2018-05-11 16:52:17 +00:00
|
|
|
Some(self.to_reversed())
|
2017-06-07 20:37:40 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Note that we don't currently do any escaping.
|
|
|
|
//
|
|
|
|
|
2017-01-28 22:18:17 +00:00
|
|
|
impl Display for PlainSymbol {
|
2017-01-11 21:51:34 +00:00
|
|
|
/// Print the symbol in EDN format.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use edn::symbols::PlainSymbol;
|
2018-05-11 16:52:17 +00:00
|
|
|
/// assert_eq!("baz", PlainSymbol::plain("baz").to_string());
|
2017-01-11 21:51:34 +00:00
|
|
|
/// ```
|
2017-01-28 22:18:17 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
2018-05-29 14:45:53 +00:00
|
|
|
self.0.fmt(f)
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 22:18:17 +00:00
|
|
|
impl Display for NamespacedSymbol {
|
2017-01-11 21:51:34 +00:00
|
|
|
/// Print the symbol in EDN format.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use edn::symbols::NamespacedSymbol;
|
2018-05-11 16:52:17 +00:00
|
|
|
/// assert_eq!("bar/baz", NamespacedSymbol::namespaced("bar", "baz").to_string());
|
2017-01-11 21:51:34 +00:00
|
|
|
/// ```
|
2017-01-28 22:18:17 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
2018-05-29 14:45:53 +00:00
|
|
|
self.0.fmt(f)
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 22:18:17 +00:00
|
|
|
impl Display for Keyword {
|
2017-01-11 21:51:34 +00:00
|
|
|
/// Print the keyword in EDN format.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use edn::symbols::Keyword;
|
2018-05-11 16:52:17 +00:00
|
|
|
/// assert_eq!(":baz", Keyword::plain("baz").to_string());
|
|
|
|
/// assert_eq!(":bar/baz", Keyword::namespaced("bar", "baz").to_string());
|
|
|
|
/// assert_eq!(":bar/_baz", Keyword::namespaced("bar", "baz").to_reversed().to_string());
|
|
|
|
/// assert_eq!(":bar/baz", Keyword::namespaced("bar", "baz").to_reversed().to_reversed().to_string());
|
2017-01-11 21:51:34 +00:00
|
|
|
/// ```
|
2017-01-28 22:18:17 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
2018-05-29 14:45:53 +00:00
|
|
|
f.write_char(':')?;
|
|
|
|
self.0.fmt(f)
|
2017-01-11 21:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 21:55:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ns_keyword_macro() {
|
2020-01-14 15:46:21 +00:00
|
|
|
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()
|
|
|
|
);
|
2017-02-17 21:55:36 +00:00
|
|
|
}
|