pub struct ScriptStruct {
pub name: String,
pub fields: Vec<ScriptField>,
}Expand description
A struct parameter: a name, and fields in declaration order, each with the value it starts with.
A script sets the fields it cares about and the rest take their defaults.
candela writes it as a struct literal (ns::Name { field: v, ..Default::default() }); Rhai and Lua pass a map and leave out the keys
they do not set. Either way the body reads one map with every field
present, so it never handles a missing key.
use lumen_script::{ScriptStruct, ScriptTy, ScriptValue};
let opts = ScriptStruct::new("Options")
.field_default("dir", ScriptTy::Str, "out")
.field("verbose", ScriptTy::Bool);
let ScriptValue::Map(full) = opts.default_value() else { unreachable!() };
assert_eq!(full["dir"], ScriptValue::Str("out".into()));
assert_eq!(full["verbose"], ScriptValue::Bool(false));Fields§
§name: StringThe type name. candela declares it in the function’s namespace, so a
script names it ns::Name.
fields: Vec<ScriptField>The fields, in the order they are declared.
Implementations§
Source§impl ScriptStruct
impl ScriptStruct
Sourcepub fn field(self, name: impl Into<String>, ty: ScriptTy) -> Self
pub fn field(self, name: impl Into<String>, ty: ScriptTy) -> Self
Add a field that starts as its type’s empty value.
Sourcepub fn field_default(
self,
name: impl Into<String>,
ty: ScriptTy,
default: impl Into<ScriptValue>,
) -> Self
pub fn field_default( self, name: impl Into<String>, ty: ScriptTy, default: impl Into<ScriptValue>, ) -> Self
Add a field that starts as default.
Sourcepub fn default_value(&self) -> ScriptValue
pub fn default_value(&self) -> ScriptValue
The value with every field at its default.
Sourcepub fn complete(&self, value: &ScriptValue) -> ScriptValue
pub fn complete(&self, value: &ScriptValue) -> ScriptValue
value with every field it leaves out set to its default, nested
structs included. A value that is not a map comes back unchanged; an
empty list is read as an empty map, since an empty Lua table cannot say
which of the two it is.
Trait Implementations§
Source§impl Clone for ScriptStruct
impl Clone for ScriptStruct
Source§fn clone(&self) -> ScriptStruct
fn clone(&self) -> ScriptStruct
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ScriptStruct
impl Debug for ScriptStruct
Source§impl<'de> Deserialize<'de> for ScriptStruct
impl<'de> Deserialize<'de> for ScriptStruct
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for ScriptStruct
impl PartialEq for ScriptStruct
Source§fn eq(&self, other: &ScriptStruct) -> bool
fn eq(&self, other: &ScriptStruct) -> bool
self and other values to be equal, and is used by ==.