logo
Expand description

Rust interface to the sciter::value.

Sciter Value holds superset of JSON objects.

It can contain as pure JSON objects (numbers, strings, maps and arrays) as internal objects like DOM elements, proxies of script functions, objects and arrays.

Basic usage

You can create an empty (undefined) Sciter value with Value::new():

use sciter::Value;

let v = Value::new();
assert!(v.is_undefined());
assert!(!v.is_null());

Or explicitly create Value of the specified type:

use sciter::Value;

let v = Value::null();
assert!(v.is_null());

let v = Value::symbol("hello");
assert!(v.is_symbol());
assert!(v.is_string());

let v = Value::error("hello");
assert!(v.is_error_string());
assert!(v.is_string());

// allocate a new array with 4 empty elements
let v = Value::array(4);
assert!(v.is_array());
assert!(v.len() == 4);

// allocate a new value with map type
let v = Value::map();
assert!(v.is_map());
assert!(v.len() == 0);

Also there is conversion from Rust types:

use sciter::Value;

let v = Value::from(true);
assert!(v.is_bool());

let v = Value::from(1);
assert!(v.is_int());

let v = Value::from(1.0);
assert!(v.is_float());

let v = Value::from("hello");
assert!(v.is_string());

let v = Value::from(b"123".as_ref());
assert!(v.is_bytes());

And from a sequence of objects:

use sciter::Value;

let v: Value = ["1","2","3"].iter().cloned().collect();
assert!(v.is_array());
assert_eq!(v.len(), 3);

assert_eq!(v[0], Value::from("1"));

And also there is a couple of macros for container literals with specific type which are just shorthands for manual construction using the set_item() and push() methods:

let map = vmap! {
  "one" => 1,
  "two" => 2.0,
  "three" => "",
};
assert!(map.is_map());
assert_eq!(map.len(), 3);

let array = varray![1, 2.0, "three"];
assert!(array.is_array());
assert_eq!(array.len(), 3);

To access its contents you should use one of to_ methods:

use sciter::Value;

let v = Value::from(4);
assert_eq!(v.to_int(), Some(4));

Note that there are two functions that convert Value to JSON and back:

use sciter::Value;

let mut v: Value = "[1, 2, 3, 4]".parse().unwrap();
let json_str = v.into_string();

Array access:

use sciter::Value;

let mut v: Value = "[10, 20]".parse().unwrap();
assert_eq!(v[0], Value::from(10));

// explicit arguments:
v.set(1, Value::from(21));
v.set(2, Value::from(22));

// implicit arguments:
v.set(1, 21);
v.set(2, 22);

assert_eq!(v.len(), 3);

assert!(v.get(0).is_int());

Map access:

use sciter::Value;

let mut v: Value = "{one: 1, two: 2}".parse().unwrap();
assert_eq!(v["one"], 1.into());
assert_eq!(v.get_item("one"), 1.into());
assert_eq!(v[Value::from("one")], Value::from(1));

v.set_item("three", 3);
assert!(v.get_item("one").is_int());

.

Structs

sciter::value wrapper.

Enums

Type identifier of the value.

Traits

Helper trait