Expand description
Configuration Variables allow humans to interactively change the state of the program.
Let’s use an example to see how we can make it interactive. The following snippet defines our program state with a user name and a method to greet the user:
pub struct User {
name: String,
}
impl User {
pub fn greet(&self, writer: &mut dyn cvar::IWrite) {
let _ = writeln!(writer, "Hello, {}!", self.name);
}
}
Implement the IVisit
trait to make this structure available for interactivity:
impl cvar::IVisit for User {
fn visit(&mut self, f: &mut dyn FnMut(&mut dyn cvar::INode)) {
f(&mut cvar::Property("name", &mut self.name, &String::new()));
f(&mut cvar::Action("greet!", |_args, writer| self.greet(writer)));
}
}
That’s it! Create an instance of the structure to interact with:
let mut user = User {
name: String::new(),
};
Given unique access, interact with the instance with a stringly typed API:
let mut writer = String::new();
// Give the user a name
cvar::console::set(&mut user, "name", "World", &mut writer);
assert_eq!(user.name, "World");
// Greet the user, the message is printed to the writer
cvar::console::invoke(&mut user, "greet!", "", &mut writer);
assert_eq!(writer, "Hello, World!\n");
This example is extremely basic, for more complex scenarios see the examples.
Modules§
- console
- Interact with the configuration variables.
Structs§
- Action
- Action node.
- Clamped
Prop - Property node with its value clamped.
- IoWriter
- Io writer.
- List
- List node.
- Null
Writer - Null writer.
- Owned
Prop - Property node which owns its variable.
- Property
- Property node.
- Read
Only Prop - Read-only property node.
- Visit
- Node visitor from closure.
Enums§
Traits§
- IAction
- Action node interface.
- IList
- List of child nodes.
- INode
- Node interface.
- IProperty
- Property node interface.
- IValue
- Property values.
- IVisit
- Node visitor.
- IWrite
- Console interface for actions to writer output to.