[][src]Struct molt::value::Value

pub struct Value { /* fields omitted */ }

The Value type. See the module level documentation for more.

Methods

impl Value[src]

pub fn empty() -> Value[src]

Returns the empty Value, a value whose string representation is the empty string.

TODO: This should really be a constant.

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

Returns the value's string representation as a reference-counted string.

Note: This is the standard way of retrieving a Value's string rep, as unlike to_string it doesn't create a new String.

Example

use molt::types::Value;
let value = Value::from(123);
assert_eq!(value.as_str(), "123");

pub fn as_bool(&self) -> Result<bool, ResultCode>[src]

Tries to return the Value as a bool, parsing the value's string representation if necessary.

Boolean Strings

The following are valid boolean strings, regardless of case: true, false, on, off, yes, no, 1, 0. Note that other numeric strings are not valid boolean strings.

Numeric Values

Non-negative numbers are interpreted as true; zero is interpreted as false.

Example

use molt::types::Value;
use molt::types::ResultCode;
// All of the following can be interpreted as booleans.
let value = Value::from(true);
let flag = value.as_bool()?;
assert_eq!(flag, true);

let value = Value::from("no");
let flag = value.as_bool()?;
assert_eq!(flag, false);

let value = Value::from(5);
let flag = value.as_bool()?;
assert_eq!(flag, true);

let value = Value::from(0);
let flag = value.as_bool()?;
assert_eq!(flag, false);

let value = Value::from(1.1);
let flag = value.as_bool()?;
assert_eq!(flag, true);

let value = Value::from(0.0);
let flag = value.as_bool()?;
assert_eq!(flag, false);

// Numeric strings can not, unless evaluated as expressions.
let value = Value::from("123");
assert!(value.as_bool().is_err());

pub fn get_bool(arg: &str) -> Result<bool, ResultCode>[src]

Converts a string argument into a boolean, returning an error on failure.

Molt accepts the following strings as Boolean values:

  • true: true, yes, on, 1
  • false: false, no, off, 0

Parsing is case-insensitive, and leading and trailing whitespace are ignored.

This method does not evaluate expressions; use molt::expr to evaluate boolean expressions.

Example

let arg = "yes";
let flag = Value::get_bool(arg)?;
assert!(flag);

pub fn as_int(&self) -> Result<MoltInt, ResultCode>[src]

Tries to return the Value as a MoltInt, parsing the value's string representation if necessary.

Integer Syntax

Molt accepts decimal integer strings, and hexadecimal integer strings with a 0x prefix. Strings may begin with a unary "+" or "-". Hex digits may be in upper or lower case.

Example

use molt::types::Value;
use molt::types::MoltInt;
use molt::types::ResultCode;

let value = Value::from(123);
let int = value.as_int()?;
assert_eq!(int, 123);

let value = Value::from("OxFF");
let int = value.as_int()?;
assert_eq!(int, 255);

pub fn get_int(arg: &str) -> Result<MoltInt, ResultCode>[src]

Converts a string argument into a MoltInt, returning an error on failure.

Molt accepts decimal integer strings, and hexadecimal integer strings with a 0x prefix. Strings may begin with a unary "+" or "-". Leading and trailing whitespace is ignored.

Example

let arg = "1";
let int = Value::get_int(arg)?;

pub fn as_float(&self) -> Result<MoltFloat, ResultCode>[src]

Tries to return the Value as a MoltFloat, parsing the value's string representation if necessary.

Floating-Point Syntax

Molt accepts the same floating-point strings as Rust's standard numeric parser.

Example

use molt::types::Value;
use molt::types::MoltFloat;
use molt::types::ResultCode;

let value = Value::from(12.34);
let flt = value.as_float()?;
assert_eq!(flt, 12.34);

let value = Value::from("23.45");
let flt = value.as_float()?;
assert_eq!(flt, 23.45);

pub fn get_float(arg: &str) -> Result<MoltFloat, ResultCode>[src]

Converts an string argument into a MoltFloat, returning an error on failure.

Molt accepts any string acceptable to str::parse<f64> as a valid floating point string. Leading and trailing whitespace is ignored, and parsing is case-insensitive.

Example

let arg = "1e2";
let val = Value::get_float(arg)?;

pub fn as_list(&self) -> Result<Rc<MoltList>, ResultCode>[src]

Tries to return the Value as an Rc<MoltList>, parsing the value's string representation if necessary.

Example

use std::rc::Rc;
use molt::types::Value;
use molt::types::MoltList;
use molt::types::ResultCode;

let value = Value::from("1234 abc");
let list: Rc<MoltList> = value.as_list()?;
assert_eq!(list.len(), 2);

assert_eq!(list[0], Value::from("1234"));
assert_eq!(list[1], Value::from("abc"));

pub fn to_list(&self) -> Result<MoltList, ResultCode>[src]

Tries to return the Value as a MoltList, parsing the value's string representation if necessary.

Use as_list when simply referring to the list's content; use this method when constructing a new list from the old one.

Example

use molt::types::Value;
use molt::types::MoltList;
use molt::types::ResultCode;

let value = Value::from("1234 abc");
let list: MoltList = value.to_list()?;
assert_eq!(list.len(), 2);

assert_eq!(list[0], Value::from("1234"));
assert_eq!(list[1], Value::from("abc"));

pub fn from_other<T: 'static>(value: T) -> Value where
    T: Display + Debug
[src]

Creates a new Value containing the given value of some user type.

The user type must meet certain constraints; see the module level documentation for details on how to define an external type for use with Molt.

Example

Suppose we have a type HexColor that meets the constraints; we can create a Value containing one as follows. Notice that the Value ownership of its input:

This example is not tested
let color: HexColor::new(0x11u8, 0x22u8, 0x33u8);
let value = Value::from_other(color);

// Retrieve the value's string rep.
assert_eq!(value.as_str(), "#112233");

See Value::as_other and Value::as_copy for examples of how to retrieve a MyType value from a Value.

pub fn as_other<T: 'static>(&self) -> Option<Rc<T>> where
    T: Display + Debug + FromStr
[src]

Tries to interpret the Value as a value of external type T, parsing the string representation if necessary.

The user type must meet certain constraints; see the module level documentation for details on how to define an external type for use with Molt.

Return Value

The value is returned as an Rc<T>, as this allows the client to use the value freely and clone it efficiently if needed.

This method returns Option<Rc<T>> rather than Result<Rc<T>,ResultCode> because it is up to the caller to provide a meaningful error message. It is normal for externally defined types to wrap this function in a function that does so; see the module level documentation for an example.

Example

Suppose we have a type HexColor that meets the constraints; we can create a Value containing one and retrieve it as follows.

This example is not tested
// Just a normal Molt string
let value = Value::from("#112233");

// Retrieve it as an Option<Rc<HexColor>>:
let color = value.as_other::<HexColor>()

if color.is_some() {
    let color = color.unwrap();
    let r = *color.red();
    let g = *color.green();
    let b = *color.blue();
}

pub fn as_copy<T: 'static>(&self) -> Option<T> where
    T: Display + Debug + FromStr + Copy
[src]

Tries to interpret the Value as a value of type T, parsing the string representation if necessary, and returning a copy.

The user type must meet certain constraints; and in particular it must implement Copy. See the module level documentation for details on how to define an external type for use with Molt.

This method returns Option rather than Result because it is up to the caller to provide a meaningful error message. It is normal for externally defined types to wrap this function in a function that does so.

Example

Suppose we have a type HexColor that meets the normal external type constraints and also supports copy; we can create a Value containing one and retrieve it as follows.

This example is not tested
// Just a normal Molt string
let value = Value::from("#112233");

// Retrieve it as an Option<HexColor>:
let color = value.as_copy::<HexColor>()

if color.is_some() {
    let color = color.unwrap();
    let r = color.red();
    let g = color.green();
    let b = color.blue();
}

Trait Implementations

impl Eq for Value[src]

impl Clone for Value[src]

impl PartialEq<Value> for Value[src]

impl From<String> for Value[src]

fn from(str: String) -> Self[src]

Creates a new Value from the given String.

Example

use molt::types::Value;
let string = String::from("My New String");
let value = Value::from(string);
assert_eq!(value.as_str(), "My New String");

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

fn from(str: &str) -> Self[src]

Creates a new Value from the given string slice.

Example

use molt::types::Value;
let value = Value::from("My String Slice");
assert_eq!(value.as_str(), "My String Slice");

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

fn from(str: &String) -> Self[src]

Creates a new Value from the given string reference.

Example

use molt::types::Value;
let value = Value::from("My String Slice");
assert_eq!(value.as_str(), "My String Slice");

impl From<bool> for Value[src]

fn from(flag: bool) -> Self[src]

Creates a new Value whose data representation is a bool. The value's string representation will be "1" or "0".

Example

use molt::types::Value;
let value = Value::from(true);
assert_eq!(value.as_str(), "1");

let value = Value::from(false);
assert_eq!(value.as_str(), "0");

impl From<i64> for Value[src]

fn from(int: MoltInt) -> Self[src]

Creates a new Value whose data representation is a MoltInt.

Example

use molt::types::Value;

let value = Value::from(123);
assert_eq!(value.as_str(), "123");

impl From<f64> for Value[src]

fn from(flt: MoltFloat) -> Self[src]

Creates a new Value whose data representation is a MoltFloat.

String Representation

The string representation of the value will be however Rust's format! macro formats floating point numbers by default. Note: this isn't quite what we want; Standard TCL goes to great lengths to ensure that the formatted string will yield exactly the same floating point number when it is parsed. Rust will format the number 5.0 as 5, turning it into a integer if parsed naively. So there is more work to be done here.

Example

use molt::types::Value;

let value = Value::from(12.34);
assert_eq!(value.as_str(), "12.34");

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

fn from(list: MoltList) -> Self[src]

Creates a new Value whose data representation is a MoltList.

Example

use molt::types::Value;

let list = vec![Value::from(1234), Value::from("abc")];
let value = Value::from(list);
assert_eq!(value.as_str(), "1234 abc");

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

fn from(list: &[Value]) -> Self[src]

Creates a new Value whose data representation is a MoltList.

Example

use molt::types::Value;

let values = [Value::from(1234), Value::from("abc")];
let value = Value::from(&values[..]);
assert_eq!(value.as_str(), "1234 abc");

impl Debug for Value[src]

impl Display for Value[src]

Auto Trait Implementations

impl !Send for Value

impl Unpin for Value

impl !Sync for Value

impl !UnwindSafe for Value

impl !RefUnwindSafe for Value

Blanket Implementations

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

type Owned = T

The resulting type after obtaining ownership.

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

impl<T> From<T> for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

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

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

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