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

S-expression in data mode

S-expression in code mode

A quoted UTF-8 string value

An unquoted, case-sensitive symbol

An integer value

A floating point value

A Cons cell

A Nil value

Methods

impl<Sym: FromStr> Value<Sym>
[src]

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"));

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.

Create a new string

use atoms::StringValue;
let string = StringValue::string("string");
assert_eq!(string.to_string(), "\"string\"");

Create a new string

use atoms::StringValue;
let int = StringValue::int(42);
assert_eq!(int.to_string(), "42");

Create a new float

use atoms::StringValue;
let float = StringValue::float(13.0);
assert_eq!(float.to_string(), "13.0");

Shorthand from creating cons-cell lists out of Vecs.

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)");

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)");

Create a cons cell.

use atoms::StringValue;
let cons = StringValue::cons(
    StringValue::int(12),
    StringValue::string("13")
);
assert_eq!(cons.to_string(), "(12 . \"13\")");

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)");

Create a nil.

use atoms::StringValue;
assert_eq!(StringValue::nil().to_string(), "()");

Check if a value is a nil

use atoms::StringValue;
assert!(StringValue::nil().is_nil());
assert!(!StringValue::final_cons(StringValue::nil()).is_nil());

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

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"
)

Check if a value is a cons

use atoms::StringValue;
assert!(StringValue::cons(
    StringValue::nil(),
    StringValue::nil()
).is_cons());
assert!(!StringValue::nil().is_cons());

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

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

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

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

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"),
    ])
);

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]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<Sym: Clone> Clone for Value<Sym>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<Sym: PartialOrd> PartialOrd for Value<Sym>
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

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]

Performs the conversion.

impl<Sym> Display for Value<Sym> where
    Sym: ToString + FromStr
[src]

Formats the value using the given formatter. Read more

impl<Sym> Debug for Value<Sym> where
    Sym: ToString + FromStr
[src]

Formats the value using the given formatter.