Skip to main content

Keyword

Struct Keyword 

Source
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")
:/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:

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

Source

pub fn plain<T>(name: T) -> Keyword
where T: Into<String>,

Source§

impl Keyword

Source

pub fn namespaced<N, T>(namespace: N, name: T) -> Keyword
where N: AsRef<str>, T: AsRef<str>,

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.

Source

pub fn name(&self) -> &str

Source

pub fn namespace(&self) -> Option<&str>

Source

pub fn components(&self) -> (&str, &str)

Source

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());
Source

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());
Source

pub fn is_namespaced(&self) -> bool

Source

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());
Source

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 Clone for Keyword

Source§

fn clone(&self) -> Keyword

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Keyword

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Keyword

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Keyword, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Keyword

Source§

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());
Source§

impl Eq for Keyword

Source§

impl From<Keyword> for TypedValue

Source§

fn from(value: Keyword) -> TypedValue

Converts to this type from the input type.
Source§

impl Hash for Keyword

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Keyword

Source§

fn cmp(&self, other: &Keyword) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Keyword

Source§

fn eq(&self, other: &Keyword) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Keyword

Source§

fn partial_cmp(&self, other: &Keyword) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Keyword

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Keyword

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.