pub struct Keyword(/* private fields */);Expand description
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:
user=> (keyword "" "")
:/
user=> (keyword "foo" "")
:foo/
user=> (keyword "" "bar")
:/barWe 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:
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());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.
Implementations§
Source§impl Keyword
impl Keyword
Sourcepub fn namespaced<N, T>(namespace: N, name: T) -> Keyword
pub fn namespaced<N, T>(namespace: N, name: T) -> Keyword
Creates a new Keyword.
§Examples
let keyword = Keyword::namespaced("foo", "bar");
assert_eq!(keyword.to_string(), ":foo/bar");See also the kw! macro in the main mentat crate.
pub fn name(&self) -> &str
pub fn namespace(&self) -> Option<&str>
pub fn components(&self) -> (&str, &str)
Sourcepub fn is_backward(&self) -> bool
pub fn is_backward(&self) -> bool
Whether this Keyword should be interpreted in reverse order. For example,
the two following snippets are identical:
[?y :person/friend ?x]
[?x :person/hired ?y]
[?y :person/friend ?x]
[?y :person/_hired ?x]§Examples
assert!(!Keyword::namespaced("foo", "bar").is_backward());
assert!(Keyword::namespaced("foo", "_bar").is_backward());Sourcepub fn is_forward(&self) -> bool
pub fn is_forward(&self) -> bool
Whether this Keyword should be interpreted in forward order.
See symbols::Keyword::is_backward.
§Examples
assert!(Keyword::namespaced("foo", "bar").is_forward());
assert!(!Keyword::namespaced("foo", "_bar").is_forward());pub fn is_namespaced(&self) -> bool
Sourcepub fn to_reversed(&self) -> Keyword
pub fn to_reversed(&self) -> Keyword
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.
§Examples
let nsk = Keyword::namespaced("foo", "bar");
assert!(!nsk.is_backward());
assert_eq!(":foo/bar", nsk.to_string());
let reversed = nsk.to_reversed();
assert!(reversed.is_backward());
assert_eq!(":foo/_bar", reversed.to_string());Sourcepub fn unreversed(&self) -> Option<Keyword>
pub fn unreversed(&self) -> Option<Keyword>
If this Keyword is ‘backward’ (see symbols::Keyword::is_backward),
return Some('forward name'); otherwise, return None.
§Examples
let nsk = Keyword::namespaced("foo", "bar");
assert_eq!(None, nsk.unreversed());
let reversed = nsk.to_reversed();
assert_eq!(Some(nsk), reversed.unreversed());Trait Implementations§
Source§impl<'de> Deserialize<'de> for Keyword
impl<'de> Deserialize<'de> for Keyword
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Keyword, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Keyword, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for Keyword
impl Display for Keyword
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Print the keyword in EDN format.
§Examples
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());impl Eq for Keyword
Source§impl From<Keyword> for TypedValue
impl From<Keyword> for TypedValue
Source§fn from(value: Keyword) -> TypedValue
fn from(value: Keyword) -> TypedValue
Source§impl Ord for Keyword
impl Ord for Keyword
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Keyword
impl PartialOrd for Keyword
Source§impl Serialize for Keyword
impl Serialize for Keyword
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for Keyword
Auto Trait Implementations§
impl Freeze for Keyword
impl RefUnwindSafe for Keyword
impl Send for Keyword
impl Sync for Keyword
impl Unpin for Keyword
impl UnsafeUnpin for Keyword
impl UnwindSafe for Keyword
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more