pub struct Value { /* private fields */ }
Expand description
The Value
type. See the module level documentation for more.
Implementations§
Source§impl Value
impl Value
Sourcepub fn empty() -> Value
pub fn empty() -> Value
Returns the empty Value
, a value whose string representation is the empty
string.
TODO: This should really be a constant, but there’s way to build it as one unless I use lazy_static.
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
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");
Sourcepub fn try_as_str(&self) -> Option<&str>
pub fn try_as_str(&self) -> Option<&str>
Returns the value’s string representation if it is already ready. This is useful for implementing command line switches.
§Example
use molt::types::Value;
let value = Value::from(123);
assert_eq!(value.try_as_str(), None);
assert_eq!(value.as_str(), "123");
assert_eq!(value.try_as_str(), Some("123"));
let value_str = Value::from("123");
assert_eq!(value_str.try_as_str(), Some("123"));
Sourcepub fn as_bool(&self) -> Result<bool, Exception>
pub fn as_bool(&self) -> Result<bool, Exception>
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::Exception;
// 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());
Sourcepub fn get_bool(arg: &str) -> Result<bool, Exception>
pub fn get_bool(arg: &str) -> Result<bool, Exception>
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);
Sourcepub fn as_dict(&self) -> Result<Rc<MoltDict>, Exception>
pub fn as_dict(&self) -> Result<Rc<MoltDict>, Exception>
Tries to return the Value
as an Rc<MoltDict>
, parsing the
value’s string representation if necessary.
§Example
use std::rc::Rc;
use molt::types::Value;
use molt::types::MoltDict;
use molt::types::Exception;
let value = Value::from("abc 1234");
let dict: Rc<MoltDict> = value.as_dict()?;
assert_eq!(dict.len(), 1);
assert_eq!(dict.get(&Value::from("abc")), Some(&Value::from("1234")));
Sourcepub fn to_dict(&self) -> Result<MoltDict, Exception>
pub fn to_dict(&self) -> Result<MoltDict, Exception>
Tries to return the Value
as a MoltDict
, parsing the
value’s string representation if necessary.
Use as_dict
when simply referring to the dict’s content;
use this method when constructing a new dict from the old one.
§Example
use molt::types::Value;
use molt::types::MoltDict;
use molt::types::Exception;
let value = Value::from("abc 1234");
let dict: MoltDict = value.to_dict()?;
assert_eq!(dict.len(), 2);
assert_eq!(dict.get(&Value::from("abc")), Some(&Value::from("1234")));
Sourcepub fn as_int(&self) -> Result<MoltInt, Exception>
pub fn as_int(&self) -> Result<MoltInt, Exception>
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::Exception;
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);
Sourcepub fn get_int(arg: &str) -> Result<MoltInt, Exception>
pub fn get_int(arg: &str) -> Result<MoltInt, Exception>
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)?;
Sourcepub fn as_float(&self) -> Result<MoltFloat, Exception>
pub fn as_float(&self) -> Result<MoltFloat, Exception>
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::Exception;
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);
Sourcepub fn get_float(arg: &str) -> Result<MoltFloat, Exception>
pub fn get_float(arg: &str) -> Result<MoltFloat, Exception>
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)?;
Sourcepub fn as_list(&self) -> Result<Rc<MoltList>, Exception>
pub fn as_list(&self) -> Result<Rc<MoltList>, Exception>
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::Exception;
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"));
Sourcepub fn to_list(&self) -> Result<MoltList, Exception>
pub fn to_list(&self) -> Result<MoltList, Exception>
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::Exception;
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"));
Sourcepub fn as_var_name(&self) -> Rc<VarName>
pub fn as_var_name(&self) -> Rc<VarName>
Returns the Value
as an Rc<VarName>
, parsing the
value’s string representation if necessary. This type is usually hidden by the
Interp
’s var
and set_var
methods, which use it implicitly; however it is
available to extension authors if need be.
§Example
use molt::types::{Value, VarName};
let value = Value::from("my_var");
let var_name = value.as_var_name();
assert_eq!(var_name.name(), "my_var");
assert_eq!(var_name.index(), None);
let value = Value::from("my_array(1)");
let var_name = value.as_var_name();
assert_eq!(var_name.name(), "my_array");
assert_eq!(var_name.index(), Some("1"));
Sourcepub fn from_other<T>(value: T) -> Value
pub fn from_other<T>(value: T) -> Value
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:
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
.
Sourcepub fn as_other<T>(&self) -> Option<Rc<T>>
pub fn as_other<T>(&self) -> Option<Rc<T>>
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>,Exception>
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.
// 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();
}
Sourcepub fn as_copy<T>(&self) -> Option<T>
pub fn as_copy<T>(&self) -> Option<T>
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.
// 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§
Source§impl From<IndexMap<Value, Value>> for Value
impl From<IndexMap<Value, Value>> for Value
Source§fn from(dict: MoltDict) -> Self
fn from(dict: MoltDict) -> Self
Creates a new Value
whose data representation is a MoltDict
.
§Example
use molt::types::Value;
use molt::types::MoltDict;
use molt::dict::dict_new;
let mut dict: MoltDict = dict_new();
dict.insert(Value::from("abc"), Value::from("123"));
let value = Value::from(dict);
assert_eq!(value.as_str(), "abc 123");
Source§impl From<bool> for Value
impl From<bool> for Value
Source§fn from(flag: bool) -> Self
fn from(flag: bool) -> Self
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");
Source§impl From<f64> for Value
impl From<f64> for Value
Source§fn from(flt: MoltFloat) -> Self
fn from(flt: MoltFloat) -> Self
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");
Source§impl PartialEq for Value
impl PartialEq for Value
impl Eq for Value
Auto Trait Implementations§
impl Freeze for Value
impl !RefUnwindSafe for Value
impl !Send for Value
impl !Sync for Value
impl Unpin for Value
impl !UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.