[−][src]Enum lexpr::value::Value
Represents an S-expression value.
See the lexpr::value
module documentation for usage examples.
Variants
The special "nil" value.
This is kind of an oddball value. In traditional Lisps (e.g., Common
Lisp or Emacs Lisp) the empty list can be written as the symbol nil
,
while in Scheme, nil
is just a regular symbol. Furthermore,
traditional Lisps don't have a separate boolean data type, and represent
true and false by the symbols t
and nil
instead. The lexpr
parser
can be instructed to parse the nil
symbol as the Nil
value (see
NilSymbol::Special
), allowing to choose its representation when
converting to text again (see NilSyntax
). Note that the empty list,
when written as ()
or implicitly constructed as a list terminator, is
always parsed as Value::Null
, not Value::Nil
.
In addition to being useful for conversions between S-expression
variants, this value is also potentially returned when using the square
bracket indexing operator on Value
.
The empty list.
This value terminates a chain of cons cells forming a proper list.
Bool(bool)
A boolean value.
Number(Number)
A number.
Char(char)
A character.
A string.
A symbol.
A keyword.
A byte vector.
Cons(Cons)
Represents a Lisp "cons cell".
Cons cells are often used to form singly-linked lists.
let v = sexp!((a list 1 2 3)); assert!(v.is_cons()); assert_eq!(v[4], sexp!(3));
A Lisp vector.
Methods
impl Value
[src]
pub fn symbol(name: impl Into<Box<str>>) -> Self
[src]
Construct a symbol, given its name.
pub fn keyword(name: impl Into<Box<str>>) -> Self
[src]
Construct a keyword, given its name.
let value = Value::keyword("foo"); assert!(value.is_keyword()); assert_eq!(value.as_keyword().unwrap(), "foo");
pub fn string(s: impl Into<Box<str>>) -> Self
[src]
Construct a string.
let value = Value::string("foo"); assert!(value.is_string()); assert_eq!(value.as_str().unwrap(), "foo");
pub fn bytes(bv: impl Into<Box<[u8]>>) -> Self
[src]
Construct a byte vector.
let value = Value::bytes(b"foo" as &[u8]); assert!(value.is_bytes()); assert_eq!(value.as_bytes().unwrap(), b"foo");
pub fn cons<T, U>(car: T, cdr: U) -> Self where
T: Into<Value>,
U: Into<Value>,
[src]
T: Into<Value>,
U: Into<Value>,
Create a cons cell given its car
and cdr
fields.
let value = Value::cons(1, Value::Null); assert!(value.is_cons()); assert_eq!(value.as_pair().unwrap(), (&Value::from(1), &Value::Null));
Note that you can also construct a cons cell from a Rust pair via the
From
trait:
let value = Value::from((42, "answer")); assert!(value.is_cons()); assert_eq!(value.as_pair().unwrap(), (&Value::from(42), &Value::string("answer")));
pub fn list<I>(elements: I) -> Self where
I: IntoIterator,
I::Item: Into<Value>,
[src]
I: IntoIterator,
I::Item: Into<Value>,
Create a list value from elements convertible into Value
.
assert_eq!(Value::list(vec![1, 2, 3]), sexp!((1 2 3)));
pub fn is_list(&self) -> bool
[src]
Returns true if the value is a (proper) list.
pub fn is_dotted_list(&self) -> bool
[src]
Returns true if the value is a dotted (improper) list.
Note that all values that are not pairs are considered dotted lists.
let list = sexp!((1 2 3)); assert!(!list.is_dotted_list()); let dotted = sexp!((1 2 . 3)); assert!(dotted.is_dotted_list());
pub fn append<I, T>(elements: I, tail: T) -> Self where
I: IntoIterator,
I::Item: Into<Value>,
T: Into<Value>,
[src]
I: IntoIterator,
I::Item: Into<Value>,
T: Into<Value>,
Create a list value from elements convertible into Value
, using a
given value as a tail.
assert_eq!(Value::append(vec![1u32, 2], 3), sexp!((1 2 . 3))); assert_eq!(Value::append(vec![1u32, 2, 3], sexp!((4 5))), sexp!((1 2 3 4 5)));
pub fn vector<I>(elements: I) -> Self where
I: IntoIterator,
I::Item: Into<Value>,
[src]
I: IntoIterator,
I::Item: Into<Value>,
Create a vector value from elements convertible into Value
.
assert_eq!(Value::vector(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"].is_string()); // The boolean `false` is not a string. assert!(!v["b"].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"].as_str(), Some("some string")); // The boolean `false` is not a string. assert_eq!(v["b"].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"]); // Rust strings are printed without quotes. // // The value is: some string println!("The value is: {}", v["a"].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.
This is useful if symbols, keywords and strings need to be treated equivalently in some context.
let kw = sexp!(#:foo); assert_eq!(kw.as_name(), Some("foo")); let sym = sexp!(bar); assert_eq!(sym.as_name(), Some("bar")); let s = sexp!("baz"); assert_eq!(s.as_name(), Some("baz"));
pub fn is_bytes(&self) -> bool
[src]
Returns true if the value is a byte vector. Returns false otherwise.
For any Value on which is_bytes
returns true, as_bytes
is guaranteed
to return the byte slice.
let v = sexp!(((a . ,(b"some bytes" as &[u8])) (b . "string"))); assert!(v["a"].is_bytes()); // A string is not a byte vector. assert!(!v["b"].is_bytes());
pub fn as_bytes(&self) -> Option<&[u8]>
[src]
If the value is a byte vector, returns the associated byte
slice. Returns None
otherwise.
let v = sexp!(((a . ,(b"some bytes" as &[u8])) (b . "string"))); assert_eq!(v["a"].as_bytes(), Some(b"some bytes" as &[u8])); // A string is not a byte vector. assert_eq!(v["b"].as_bytes(), None);
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 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"].is_i64()); // Greater than i64::MAX. assert!(!v["b"].is_i64()); // Numbers with a decimal point are not considered integers. assert!(!v["c"].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"].is_u64()); // Negative integer. assert!(!v["b"].is_u64()); // Numbers with a decimal point are not considered integers. assert!(!v["c"].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"].is_f64()); // Integers. assert!(!v["b"].is_f64()); assert!(!v["c"].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"].as_i64(), Some(64)); assert_eq!(v["b"].as_i64(), None); assert_eq!(v["c"].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"].as_u64(), Some(64)); assert_eq!(v["b"].as_u64(), None); assert_eq!(v["c"].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"].as_f64(), Some(256.0)); assert_eq!(v["b"].as_f64(), Some(64.0)); assert_eq!(v["c"].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"].is_boolean()); // The nil value is special, and not a boolean. assert!(!v["b"].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"].as_bool(), Some(false)); // The string `"false"` is a string, not a boolean. assert_eq!(v["b"].as_bool(), None);
pub fn is_char(&self) -> bool
[src]
Returns true if the value is a character. Returns false otherwise.
pub fn as_char(&self) -> Option<char>
[src]
If the value is a character, returns the associated char
. Returns None
otherwise.
let v = sexp!(((a . 'c') (b . "c"))); assert_eq!(v["a"].as_char(), Some('c')); // The string `"c"` is a single-character string, not a character. assert_eq!(v["b"].as_char(), None);
pub fn is_nil(&self) -> bool
[src]
Returns true if the value is Nil
. 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"].is_nil()); // The boolean `false` is not nil. assert!(!v["b"].is_nil());
pub fn as_nil(&self) -> Option<()>
[src]
If the value is Nil
, returns ()
. Returns None
otherwise.
let v = sexp!(((a . #nil) (b . #f) (c . ()))); assert_eq!(v["a"].as_nil(), Some(())); // The boolean `false` is not nil. assert_eq!(v["b"].as_nil(), None); // Neither is the empty list. assert_eq!(v["c"].as_nil(), None);
pub fn is_null(&self) -> bool
[src]
Returns true if the value is Null
. Returns false otherwise.
pub fn as_null(&self) -> Option<()>
[src]
If the value is Null
, returns ()
. Returns None
otherwise.
pub fn is_cons(&self) -> bool
[src]
Returns true if the value is a cons cell. Returns False
otherwise.
pub fn as_cons(&self) -> Option<&Cons>
[src]
If the value is a cons cell, returns a reference to it. Returns None
otherwise.
pub fn as_cons_mut(&mut self) -> Option<&mut Cons>
[src]
If the value is a cons cell, returns a mutable reference to it. Returns
None
otherwise.
pub fn as_pair(&self) -> Option<(&Value, &Value)>
[src]
If the value is a cons cell, return references to its car
and cdr
fields.
let cell = sexp!((foo . bar)); assert_eq!(cell.as_pair(), Some((&sexp!(foo), &sexp!(bar)))); assert_eq!(sexp!("not-a-pair").as_pair(), None);
pub fn is_vector(&self) -> bool
[src]
Returns true if the value is a vector.
pub fn as_slice(&self) -> Option<&[Value]>
[src]
If the value is a vector, return a reference to its elements.
let v = sexp!(#(1 2 "three")); let slice: &[Value] = &[sexp!(1), sexp!(2), sexp!("three")]; assert_eq!(v.as_slice(), Some(slice));
pub fn as_slice_mut(&mut self) -> Option<&mut [Value]>
[src]
If the value is a vector, return a mutable reference to its elements.
let mut v = sexp!(#(1 2 "three")); v.as_slice_mut().unwrap()[2] = sexp!(3); let slice: &[Value] = &[sexp!(1), sexp!(2), sexp!(3)]; assert_eq!(v.as_slice(), Some(slice));
pub fn list_iter(&self) -> Option<ListIter>
[src]
If the value is a list, return an iterator over the list elements.
If the value is not either a cons cell or Null
, None
is returned.
Note that the returned iterator has special behavior for improper lists, yielding the
element after the dot after returning None
the first time.
use lexpr::sexp; let value = lexpr::from_str("(1 2 . 3)").unwrap(); let mut iter = value.list_iter().unwrap(); assert_eq!(iter.next(), Some(&sexp!(1))); assert_eq!(iter.next(), Some(&sexp!(2))); assert_eq!(iter.next(), None); assert_eq!(iter.next(), Some(&sexp!(3))); assert_eq!(iter.next(), None);
pub fn to_vec(&self) -> Option<Vec<Value>>
[src]
Attempts conversion to a vector, cloning the values.
For proper lists (including Value::Null
), this returns a vector of
values. If you want to handle improper list in a similar way, combine
as_cons
and the Cons::to_vec
method.
assert_eq!(sexp!((1 2 3)).to_vec(), Some(vec![sexp!(1), sexp!(2), sexp!(3)])); assert_eq!(sexp!(()).to_vec(), Some(vec![])); assert_eq!(sexp!((1 2 . 3)).to_vec(), None);
pub fn to_ref_vec(&self) -> Option<Vec<&Value>>
[src]
Attempts conversion to a vector, taking references to the values.
For proper lists (including Value::Null
), this returns a vector of
value references. If you want to handle improper list in a similar way,
combine as_cons
and the Cons::to_ref_vec
method.
assert_eq!(sexp!((1 2 3)).to_ref_vec(), Some(vec![&sexp!(1), &sexp!(2), &sexp!(3)])); assert_eq!(sexp!(()).to_ref_vec(), Some(vec![])); assert_eq!(sexp!((1 2 . 3)).to_ref_vec(), None);
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-ref
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!(65)); assert_eq!(alist.get("B").unwrap(), &sexp!(66)); assert_eq!(alist.get("C").unwrap(), &sexp!(67)); assert_eq!(alist.get(sexp!(42)).unwrap(), &sexp!("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"][0], sexp!((b . 42))); assert_eq!(alist["C"][1], sexp!("ć")); assert_eq!(alist["D"], sexp!(#nil)); assert_eq!(alist[0]["x"]["y"]["z"], sexp!(#nil));
Trait Implementations
impl<'a> AsRef<Value> for Ref<'a>
[src]
impl Clone for Value
[src]
impl Debug 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<'_> From<&'_ [u8]> for Value
[src]
impl<'_> From<&'_ str> for Value
[src]
impl<T, U> From<(T, U)> for Value where
T: Into<Value>,
U: Into<Value>,
[src]
T: Into<Value>,
U: Into<Value>,
impl From<Box<[Value]>> for Value
[src]
impl From<Box<[u8]>> for Value
[src]
impl From<Box<str>> for Value
[src]
impl From<Cons> for Value
[src]
impl<'a> From<Cow<'a, str>> for Value
[src]
impl From<Datum> for Value
[src]
impl From<Number> for Value
[src]
impl From<String> for Value
[src]
impl From<Vec<Value>> for Value
[src]
impl From<Vec<u8>> for Value
[src]
impl From<bool> for Value
[src]
impl From<char> for Value
[src]
impl From<f32> for Value
[src]
impl From<f64> for Value
[src]
impl From<i16> for Value
[src]
impl From<i32> for Value
[src]
impl From<i64> for Value
[src]
impl From<i8> for Value
[src]
impl From<u16> for Value
[src]
impl From<u32> for Value
[src]
impl From<u64> for Value
[src]
impl From<u8> for Value
[src]
impl FromStr for Value
[src]
type Err = Error
Parse an S-expression value from a string.
fn from_str(s: &str) -> Result<Self, Self::Err>
[src]
impl<I> Index<I> for Value where
I: Index,
[src]
I: Index,
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!((y (z zz)))); assert_eq!(data["a"], sexp!(42)); // returns nil for undefined values assert_eq!(data["b"], sexp!(#nil)); // does not panic
impl Index for Value
[src]
fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value>
[src]
impl<'a> PartialEq<&'a str> for Value
[src]
impl PartialEq<String> for Value
[src]
impl PartialEq<Value> for Value
[src]
impl PartialEq<Value> for str
[src]
impl PartialEq<Value> for u32
[src]
impl PartialEq<Value> for u64
[src]
impl PartialEq<Value> for f32
[src]
impl PartialEq<Value> for f64
[src]
impl PartialEq<Value> for bool
[src]
impl<'a> PartialEq<Value> for &'a str
[src]
impl PartialEq<Value> for String
[src]
impl PartialEq<Value> for i8
[src]
impl PartialEq<Value> for i16
[src]
impl PartialEq<Value> for i32
[src]
impl PartialEq<Value> for i64
[src]
impl PartialEq<Value> for u8
[src]
impl PartialEq<Value> for u16
[src]
impl PartialEq<bool> for Value
[src]
impl<'a> PartialEq<bool> for &'a Value
[src]
impl<'a> PartialEq<bool> for &'a mut Value
[src]
impl PartialEq<f32> for Value
[src]
impl<'a> PartialEq<f32> for &'a Value
[src]
impl<'a> PartialEq<f32> for &'a mut Value
[src]
impl PartialEq<f64> for Value
[src]
impl<'a> PartialEq<f64> for &'a Value
[src]
impl<'a> PartialEq<f64> for &'a mut Value
[src]
impl PartialEq<i16> for Value
[src]
impl<'a> PartialEq<i16> for &'a Value
[src]
impl<'a> PartialEq<i16> for &'a mut Value
[src]
impl PartialEq<i32> for Value
[src]
impl<'a> PartialEq<i32> for &'a Value
[src]
impl<'a> PartialEq<i32> for &'a mut Value
[src]
impl PartialEq<i64> for Value
[src]
impl<'a> PartialEq<i64> for &'a Value
[src]
impl<'a> PartialEq<i64> for &'a mut Value
[src]
impl PartialEq<i8> for Value
[src]
impl<'a> PartialEq<i8> for &'a Value
[src]
impl<'a> PartialEq<i8> for &'a mut Value
[src]
impl PartialEq<str> for Value
[src]
impl PartialEq<u16> for Value
[src]
impl<'a> PartialEq<u16> for &'a Value
[src]
impl<'a> PartialEq<u16> for &'a mut Value
[src]
impl PartialEq<u32> for Value
[src]
impl<'a> PartialEq<u32> for &'a Value
[src]
impl<'a> PartialEq<u32> for &'a mut Value
[src]
impl PartialEq<u64> for Value
[src]
impl<'a> PartialEq<u64> for &'a Value
[src]
impl<'a> PartialEq<u64> for &'a mut Value
[src]
impl PartialEq<u8> for Value
[src]
impl<'a> PartialEq<u8> for &'a Value
[src]
impl<'a> PartialEq<u8> for &'a mut Value
[src]
impl StructuralPartialEq for Value
[src]
Auto Trait Implementations
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,