Enum atoms::Value
[−]
[src]
pub enum Value<Sym> { Data(Box<Value<Sym>>), Code(Box<Value<Sym>>), Str(String), Symbol(Sym), Int(i64), Float(f64), Cons(Box<Value<Sym>>, Box<Value<Sym>>), Nil, }
A single value with a variable representation of symbols.
use atoms::Value; // Represent symbols as `String` let int = Value::<String>::int(12); let float = Value::<String>::float(13.0); let string = Value::<String>::string("fourteen"); // Symbols may not always be valid let symbol = Value::<String>::symbol("fifteen").unwrap(); // A list let cons = Value::<String>::cons( int, Value::<String>::cons( float, Value::<String>::cons( string, Value::<String>::cons( symbol, Value::<String>::nil() ) ) ) );
Variants
Data(Box<Value<Sym>>)
S-expression in data mode
Code(Box<Value<Sym>>)
S-expression in code mode
Str(String)
A quoted UTF-8 string value
Symbol(Sym)
An unquoted, case-sensitive symbol
Int(i64)
An integer value
Float(f64)
A floating point value
Cons(Box<Value<Sym>>, Box<Value<Sym>>)
A Cons cell
Nil
A Nil value
Methods
impl<Sym: FromStr> Value<Sym>
[src]
fn auto<T>(value: T) -> Value<Sym> where
T: AutoValue<Sym>,
T: AutoValue<Sym>,
Automatically convert value
This automatically creates the most sensible value for the types:
i64
f64
String
use atoms::StringValue; assert_eq!(StringValue::auto(13), StringValue::int(13)); assert_eq!(StringValue::auto(3.1415), StringValue::float(3.1415)); assert_eq!(StringValue::auto("Text"), StringValue::string("Text"));
fn symbol(s: &str) -> Option<Value<Sym>>
Create a new symbol from a string.
use atoms::StringValue; let symbol = StringValue::symbol("symbol").unwrap(); assert_eq!(symbol.to_string(), "symbol");
Depending on the type used to represent the symbol, this may fail to
produce a symbol and return a None
. This will be presented as an error
by the parser.
fn string<S: Into<String>>(s: S) -> Value<Sym>
Create a new string
use atoms::StringValue; let string = StringValue::string("string"); assert_eq!(string.to_string(), "\"string\"");
fn int<I: Into<i64>>(i: I) -> Value<Sym>
Create a new string
use atoms::StringValue; let int = StringValue::int(42); assert_eq!(int.to_string(), "42");
fn float<F: Into<f64>>(f: F) -> Value<Sym>
Create a new float
use atoms::StringValue; let float = StringValue::float(13.0); assert_eq!(float.to_string(), "13.0");
fn list<V: Into<Value<Sym>>>(source_vec: Vec<V>) -> Value<Sym>
Shorthand from creating cons-cell lists out of Vec
s.
use atoms::StringValue; let ints = vec![ StringValue::int(1), StringValue::int(2), StringValue::int(3), StringValue::int(4), StringValue::int(5), StringValue::int(6) ]; let list = StringValue::list(ints); assert_eq!(list.to_string(), "(1 2 3 4 5 6)");
fn into_list<A, V: Into<Value<Sym>>, F>(
source_vec: Vec<A>,
map: F
) -> Value<Sym> where
F: Fn(&A) -> V,
source_vec: Vec<A>,
map: F
) -> Value<Sym> where
F: Fn(&A) -> V,
Shorthand to convert a vec into a cons-cell list with a given map.
use atoms::StringValue; let ints = vec![1, 2, 3, 4, 5, 6]; let list = StringValue::into_list(ints, |c| StringValue::int(*c)); assert_eq!(list.to_string(), "(1 2 3 4 5 6)");
fn cons<V: Into<Value<Sym>>>(left: V, right: V) -> Value<Sym>
Create a cons cell.
use atoms::StringValue; let cons = StringValue::cons( StringValue::int(12), StringValue::string("13") ); assert_eq!(cons.to_string(), "(12 . \"13\")");
fn final_cons<V: Into<Value<Sym>>>(left: V) -> Value<Sym>
Create a cons cell with only a left element.
This creates a cons cell with the right element being a nil. This is useful it you are manually constructing lists.
use atoms::StringValue; let cons = StringValue::final_cons( StringValue::int(12), ); assert_eq!(cons.to_string(), "(12)");
fn nil() -> Value<Sym>
Create a nil.
use atoms::StringValue; assert_eq!(StringValue::nil().to_string(), "()");
fn is_nil(&self) -> bool
Check if a value is a nil
use atoms::StringValue; assert!(StringValue::nil().is_nil()); assert!(!StringValue::final_cons(StringValue::nil()).is_nil());
fn code<V: Into<Value<Sym>>>(value: V) -> Value<Sym>
Mark a value as code
When using mixed-mode data, this marks an s-expression as code.
use atoms::StringValue; StringValue::code(StringValue::symbol("map").unwrap()).to_string();
fn data<V: Into<Value<Sym>>>(value: V) -> Value<Sym>
Mark a value as data
When using mixed-mode data, this marks an s-expression as data.
use atoms::StringValue; assert_eq!( StringValue::data(StringValue::symbol("apple").unwrap()).to_string(), "'apple" )
fn is_cons(&self) -> bool
Check if a value is a cons
use atoms::StringValue; assert!(StringValue::cons( StringValue::nil(), StringValue::nil() ).is_cons()); assert!(!StringValue::nil().is_cons());
fn is_list(&self) -> bool
Check if a value is a valid list.
A value is a list if:
* it is a Value::Nil
, or
* it is a Value::Cons
with a rightmost element this is a list.
use atoms::StringValue; // `Nil` is a valid list assert!(StringValue::nil().is_list()); // `final_cons` ensures we get a valid list assert!(StringValue::cons( StringValue::int(12), StringValue::final_cons( StringValue::float(13.0) ) ).is_list()); // Manually terminated lists are valid assert!(StringValue::cons( StringValue::int(12), StringValue::cons( StringValue::float(13.0), StringValue::nil() ) ).is_list()); // These are not lists assert!(!StringValue::int(12).is_list()); assert!(!StringValue::float(12.0).is_list()); assert!(!StringValue::string("12").is_list()); assert!(!StringValue::symbol("sym").unwrap().is_list()); assert!(!StringValue::cons( StringValue::nil(), StringValue::symbol("sym").unwrap() ).is_list());
fn is_data(&self) -> bool
Returns if is a wrapped data value
use atoms::StringValue; assert!( StringValue::data(StringValue::symbol("apple").unwrap()).is_data() ); assert!( !StringValue::code(StringValue::symbol("apple").unwrap()).is_data() ); assert!( !StringValue::symbol("apple").unwrap().is_code() );
fn is_code(&self) -> bool
Returns if is a wrapped code value
use atoms::StringValue; assert!( StringValue::code(StringValue::symbol("map").unwrap()).is_code() ); assert!( !StringValue::data(StringValue::symbol("map").unwrap()).is_code() ); assert!( !StringValue::symbol("map").unwrap().is_code() );
fn unwrap(self) -> Value<Sym>
Unwrap code and data values.
Code and data values are really only tagging their contents. To get the
actual value of any Value
, you can always unwrap
it. Note that
unwrap
only unwraps a single layer, to completey unwrap an entire
s-expression use unwrap_all
.
use atoms::StringValue; assert_eq!( StringValue::code(StringValue::symbol("inner").unwrap()).unwrap(), StringValue::symbol("inner").unwrap() ); assert_eq!( StringValue::data(StringValue::symbol("inner").unwrap()).unwrap(), StringValue::symbol("inner").unwrap() ); assert_eq!( StringValue::symbol("inner").unwrap().unwrap(), StringValue::symbol("inner").unwrap() );
fn unwrap_full(self) -> Value<Sym>
Fully unwrap tree. Unwraps all data and code values.
This will recursively unwrap an entire s-expression, removing any
information about data or code. To only unwrap the outermost layer,
use unwrap
.
use atoms::StringValue; assert_eq!( StringValue::code(StringValue::list(vec![ StringValue::data(StringValue::auto(14)), StringValue::data(StringValue::auto(13.000)), StringValue::auto("twelve"), ])).unwrap_full(), StringValue::list(vec![ StringValue::auto(14), StringValue::auto(13.000), StringValue::auto("twelve"), ]) );
fn is_multimode(&self) -> bool
Is a multimode data s-expression
This will return true if the given value is explicitly tagged as data and contains children at any level that are code.
use atoms::StringValue; assert!( StringValue::data(StringValue::list(vec![ StringValue::code(StringValue::auto(14)), StringValue::code(StringValue::auto(13.000)), StringValue::auto("twelve"), ])).is_multimode() ); assert!( !StringValue::data(StringValue::list(vec![ StringValue::auto(14), StringValue::auto(13.000), StringValue::auto("twelve"), ])).is_multimode() );
Trait Implementations
impl<Sym: PartialEq> PartialEq for Value<Sym>
[src]
fn eq(&self, __arg_0: &Value<Sym>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Value<Sym>) -> bool
This method tests for !=
.
impl<Sym: Clone> Clone for Value<Sym>
[src]
fn clone(&self) -> Value<Sym>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<Sym: PartialOrd> PartialOrd for Value<Sym>
[src]
fn partial_cmp(&self, __arg_0: &Value<Sym>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Value<Sym>) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Value<Sym>) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &Value<Sym>) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Value<Sym>) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<Sym> AsRef<Value<Sym>> for Value<Sym>
[src]
fn as_ref(&self) -> &Self
Performs the conversion.
impl<Sym> Display for Value<Sym> where
Sym: ToString + FromStr,
[src]
Sym: ToString + FromStr,
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
Formats the value using the given formatter. Read more
impl<Sym> Debug for Value<Sym> where
Sym: ToString + FromStr,
[src]
Sym: ToString + FromStr,