[][src]Enum lexpr::value::Value

pub enum Value {
    Atom(Atom),
    List(Vec<Value>),
    ImproperList(Vec<Value>, Atom),
}

Represents an S-expression value.

Variants

Atom(Atom)

Represents a lisp atom (non-list).

let atoms = vec![
    sexp!(#nil),
    sexp!(5.0),
    sexp!("Hello"),
    sexp!(symbol),
    sexp!(#:keyword),
    sexp!(#:"kebab-keyword"),
];
List(Vec<Value>)

Represents a proper Lisp list.

let v = sexp!((a list 1 2 3));
ImproperList(Vec<Value>, Atom)

Represents an improper (aka dotted) Lisp list.

Note that this may also represent a single atom, if the Vec field has zero length. This degenerate case should never be constructed by the sexp macro, but the API does not prevent users from constructing such values. Use Value::as_atom when you need to treat these two cases identically.

let v = sexp!((a dotted . list));

Methods

impl Value[src]

pub fn nil() -> Self[src]

Construct the nil value.

pub fn symbol(name: impl Into<String>) -> Self[src]

Construct a symbol, given its name.

pub fn keyword(name: impl Into<String>) -> Self[src]

Construct a keyword, given its name.

let value = Value::keyword("foo");
assert!(value.is_keyword());
assert_eq!(value.as_name().unwrap(), "foo");

pub fn list<I>(elements: I) -> Self where
    I: IntoIterator,
    I::Item: Into<Value>, 
[src]

Create a list value from elements convertible into Value.

assert_eq!(Value::list(vec![1, 2, 3]), sexp!((1 2 3)));

pub fn empty_list() -> Self[src]

Create an empty list.

assert_eq!(Value::empty_list(), sexp!(()));

pub fn improper_list<I, T>(elements: I, tail: T) -> Self where
    I: IntoIterator,
    I::Item: Into<Value>,
    T: Into<Atom>, 
[src]

Create a list value from elements convertible into Value.

assert_eq!(Value::improper_list(vec![1u32, 2], 3), sexp!((1 2 . 3)));

pub fn is_string(&self) -> bool[src]

Returns true if the Value is a String. Returns false otherwise.

For any Value on which is_string returns true, as_str is guaranteed to return the string slice.

let v = sexp!(((a "some string") (b #f)));

assert!(v["a"][1].is_string());

// The boolean `false` is not a string.
assert!(!v["b"][1].is_string());

pub fn as_str(&self) -> Option<&str>[src]

If the Value is a String, returns the associated str. Returns None otherwise.

let v = sexp!(((a "some string") (b #f)));

assert_eq!(v["a"][1].as_str(), Some("some string"));

// The boolean `false` is not a string.
assert_eq!(v["b"][1].as_str(), None);

// S-expression values are printed in S-expression
// representation, so strings are in quotes.
//    The value is: "some string"
println!("The value is: {}", v["a"][1]);

// Rust strings are printed without quotes.
//
//    The value is: some string
println!("The value is: {}", v["a"][1].as_str().unwrap());

pub fn is_symbol(&self) -> bool[src]

Returns true if the Value is a symbol. Returns false otherwise.

For any Value on which is_symbol returns true, as_symbol is guaranteed to return the string slice.

let v = sexp!((#:foo bar "baz"));

assert!(v[1].is_symbol());

// Keywords and strings are not symbols.
assert!(!v[0].is_symbol());
assert!(!v[2].is_symbol());

pub fn as_symbol(&self) -> Option<&str>[src]

If the Value is a symbol, returns the associated str. Returns None otherwise.

let v = sexp!(foo);

assert_eq!(v.as_symbol(), Some("foo"));

pub fn is_keyword(&self) -> bool[src]

Returns true if the Value is a keyword. Returns false otherwise.

For any Value on which is_keyword returns true, as_keyword is guaranteed to return the string slice.

let v = sexp!((#:foo bar "baz"));

assert!(v[0].is_keyword());

// Symbols and strings are not keywords.
assert!(!v[1].is_keyword());
assert!(!v[2].is_keyword());

pub fn as_keyword(&self) -> Option<&str>[src]

If the Value is a keyword, returns the associated str. Returns None otherwise.

let v = sexp!(#:foo);

assert_eq!(v.as_keyword(), Some("foo"));

pub fn as_name(&self) -> Option<&str>[src]

Get the name of a symbol or keyword, or the value of a string.

pub fn is_number(&self) -> bool[src]

Return true if the Value is a number.

pub fn as_number(&self) -> Option<&Number>[src]

For numbers, return a reference to them. For other values, return None.

pub fn as_atom(&self) -> Option<&Atom>[src]

Lossless conversion to an Atom.

Returns the Atom directly corresponding to this value, or None. This returns a Some only if the value itself is an atom, or it is an improper list of zero length.

pub fn rest(&self) -> Option<&Atom>[src]

For improper lists, return their non-list tail.

For proper lists and atoms, this will return None.

pub fn tail(&self) -> Value[src]

Return the tail of an improper list as a value.

This is the sloppy version of the rest method. It will, for improper lists, return a value constructed from their non-list tail, while for all other values, it will return the nil value.

pub fn is_i64(&self) -> bool[src]

Returns true if the Value is an integer between i64::MIN and i64::MAX.

For any Value on which is_i64 returns true, as_i64 is guaranteed to return the integer value.

let big = i64::max_value() as u64 + 10;
let v = sexp!(((a 64) (b ,big) (c 256.0)));

assert!(v["a"][1].is_i64());

// Greater than i64::MAX.
assert!(!v["b"][1].is_i64());

// Numbers with a decimal point are not considered integers.
assert!(!v["c"][1].is_i64());

pub fn is_u64(&self) -> bool[src]

Returns true if the Value is an integer between zero and u64::MAX.

For any Value on which is_u64 returns true, as_u64 is guaranteed to return the integer value.

let v = sexp!(((a 64) (b -64) (c 256.0)));

assert!(v["a"][1].is_u64());

// Negative integer.
assert!(!v["b"][1].is_u64());

// Numbers with a decimal point are not considered integers.
assert!(!v["c"][1].is_u64());

pub fn is_f64(&self) -> bool[src]

Returns true if the Value is a number that can be represented by f64.

For any Value on which is_f64 returns true, as_f64 is guaranteed to return the floating point value.

Currently this function returns true if and only if both is_i64 and is_u64 return false but this is not a guarantee in the future.

let v = sexp!(((a 256.0) (b 64) (c -64)));

assert!(v["a"][1].is_f64());

// Integers.
assert!(!v["b"][1].is_f64());
assert!(!v["c"][1].is_f64());

pub fn as_i64(&self) -> Option<i64>[src]

If the Value is an integer, represent it as i64 if possible. Returns None otherwise.

let big = i64::max_value() as u64 + 10;
let v = sexp!(((a 64) (b ,big) (c 256.0)));

assert_eq!(v["a"][1].as_i64(), Some(64));
assert_eq!(v["b"][1].as_i64(), None);
assert_eq!(v["c"][1].as_i64(), None);

pub fn as_u64(&self) -> Option<u64>[src]

If the Value is an integer, represent it as u64 if possible. Returns None otherwise.

let v = sexp!(((a 64) (b -64) (c 256.0)));

assert_eq!(v["a"][1].as_u64(), Some(64));
assert_eq!(v["b"][1].as_u64(), None);
assert_eq!(v["c"][1].as_u64(), None);

pub fn as_f64(&self) -> Option<f64>[src]

If the Value is a number, represent it as f64 if possible. Returns None otherwise.

let v = sexp!(((a 256.0) (b 64) (c -64)));

assert_eq!(v["a"][1].as_f64(), Some(256.0));
assert_eq!(v["b"][1].as_f64(), Some(64.0));
assert_eq!(v["c"][1].as_f64(), Some(-64.0));

pub fn is_boolean(&self) -> bool[src]

Returns true if the Value is a Boolean. Returns false otherwise.

For any Value on which is_boolean returns true, as_bool is guaranteed to return the boolean value.

let v = sexp!(((a #f) (b #nil)));

assert!(v["a"][1].is_boolean());

// The nil value is special, and not a boolean.
assert!(!v["b"][1].is_boolean());

pub fn as_bool(&self) -> Option<bool>[src]

If the Value is a Boolean, returns the associated bool. Returns None otherwise.

let v = sexp!(((a #f) (b "false")));

assert_eq!(v["a"][1].as_bool(), Some(false));

// The string `"false"` is a string, not a boolean.
assert_eq!(v["b"][1].as_bool(), None);

pub fn is_nil(&self) -> bool[src]

Returns true if the Value is a Nil atom. Returns false otherwise.

For any Value on which is_nil returns true, as_nil is guaranteed to return Some(()).

let v = sexp!(((a #nil) (b #f)));

assert!(v["a"][1].is_nil());

// The boolean `false` is not nil.
assert!(!v["b"][1].is_nil());

pub fn as_nil(&self) -> Option<()>[src]

If the Value is a Nil atom, returns (). Returns None otherwise.

let v = sexp!(((a #nil) (b #f)));

assert_eq!(v["a"][1].as_nil(), Some(()));

// The boolean `false` is not nil.
assert_eq!(v["b"][1].as_nil(), None);

pub fn is_list(&self) -> bool[src]

Returns true if the Value is a (proper) list.

pub fn is_improper_list(&self) -> bool[src]

Returns true if the Value is an improper list.

pub fn get<I: Index>(&self, index: I) -> Option<&Value>[src]

Index into a S-expression list. A string or Value value can be used to access a value in an association list, and a usize index can be used to access the n-th element of a list.

For indexing into association lists, the given string will match strings, symbols and keywords.

Returns None if the type of self does not match the type of the index, for example if the index is a string and self is not an association list. Also returns None if the given key does not exist in the map or the given index is not within the bounds of the list; note that the tail of an improper list is also considered out-of-bounds.

In Scheme terms, this method can be thought of a combination of assoc and list-ref, depending on the argument type. If you want to look up a number in an association list, use an Value value containing that number.

let alist = sexp!((("A" . 65) (B . 66) (#:C 67) (42 . "The answer")));
assert_eq!(*alist.get("A").unwrap(), sexp!(("A" . 65)));
assert_eq!(*alist.get("B").unwrap(), sexp!((B . 66)));
assert_eq!(*alist.get("C").unwrap(), sexp!((#:C 67)));
assert_eq!(*alist.get(sexp!(42)).unwrap(), sexp!((42 . "The answer")));

let list = sexp!(("A" "B" "C"));
assert_eq!(*list.get(2).unwrap(), sexp!("C"));

assert_eq!(list.get("A"), None);

Square brackets can also be used to index into a value in a more concise way. This returns the nil value in cases where get would have returned None. See Index for details.

let alist = sexp!((
    ("A" . ("a" "á" "à"))
    ("B" . ((b . 42) (c . 23)))
    ("C" . ("c" "ć" "ć̣" "ḉ"))
));
assert_eq!(alist["B"][1], sexp!((b . 42)));
assert_eq!(alist["C"][2], sexp!("ć"));

assert_eq!(alist["D"], sexp!(#nil));
assert_eq!(alist[0]["x"]["y"]["z"], sexp!(#nil));

Trait Implementations

impl Index for Value[src]

impl PartialEq<Value> for Value[src]

impl PartialEq<str> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<&'a str> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for str[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<Value> for &'a str[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<String> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for String[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<i8> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for i8[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i8> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i8> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<i16> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for i16[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i16> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i16> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<i32> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for i32[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i32> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i32> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<i64> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for i64[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i64> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<i64> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<u8> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for u8[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u8> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u8> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<u16> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for u16[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u16> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u16> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<u32> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for u32[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u32> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u32> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<u64> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for u64[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u64> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<u64> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<f32> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for f32[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<f32> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<f32> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<f64> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for f64[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<f64> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<f64> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<bool> for Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl PartialEq<Value> for bool[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<bool> for &'a Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a> PartialEq<bool> for &'a mut Value[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl Clone for Value[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl From<Atom> for Value[src]

impl From<u8> for Value[src]

impl From<u16> for Value[src]

impl From<u32> for Value[src]

impl From<u64> for Value[src]

impl From<i8> for Value[src]

impl From<i16> for Value[src]

impl From<i32> for Value[src]

impl From<f32> for Value[src]

impl From<f64> for Value[src]

impl From<bool> for Value[src]

impl<'_> From<&'_ str> for Value[src]

impl From<String> for Value[src]

impl From<Number> for Value[src]

impl<'a> From<Cow<'a, str>> for Value[src]

impl From<Vec<Value>> for Value[src]

impl Display for Value[src]

fn fmt(&self, f: &mut Formatter) -> Result[src]

Display an S-expression value as a string.

let value = sexp!(((city "London") (street "10 Downing Street")));

// Compact format:
//
// ((city "London") (street "10 Downing Street"))
let compact = format!("{}", value);
assert_eq!(compact,
    r#"((city "London") (street "10 Downing Street"))"#);

impl<I> Index<I> for Value where
    I: Index
[src]

type Output = Value

The returned type after indexing.

fn index(&self, index: I) -> &Value[src]

Index into a lexpr::Value using the syntax value[0] or value["k"].

Returns the nil value if the type of self does not match the type of the index, for example if the index is a string and self is not an association list. Also returns the nil value if the given key does not exist in the assication list or the given index is not within the bounds of the list.

Note that repeatedly indexing with a string is not possible, as the indexing operation returns the found association list entry, which is not an association list itself. This behavior, i.e. returning the whole entry including the key is due to the design decison of representing lists as Rust vectors.

Examples

let data = sexp!(((a . 42) (x . (y (z zz)))));

assert_eq!(data["x"], sexp!((x . (y (z zz)))));

assert_eq!(data["a"], sexp!((a . 42))); // returns nil for undefined values
assert_eq!(data["b"], sexp!(#nil)); // does not panic

impl Debug for Value[src]

Auto Trait Implementations

impl Send for Value

impl Sync for Value

Blanket Implementations

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.