[−][src]Crate cvar
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, console: &mut cvar::IConsole) { let _ = writeln!(console, "Hello, {}!", self.name); } }
Implement the IVisit
trait to make this structure available for interactivity:
impl cvar::IVisit for User { fn visit_mut(&mut self, f: &mut FnMut(&mut cvar::INode)) { f(&mut cvar::Property("name", "description", &mut self.name, String::new())); f(&mut cvar::Action("greet!", "description", |_args, console| self.greet(console))); } }
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:
// Give the user a name cvar::console::set(&mut user, "name", "World").unwrap(); assert_eq!(user.name, "World"); // Greet the user, the message is printed to the console string let mut console = String::new(); cvar::console::invoke(&mut user, "greet!", &[""], &mut console); assert_eq!(console, "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. |
ClampedProp | Property node with its value clamped. |
IoConsole | Io console for actions. |
List | List node. |
NullConsole | Null console for actions. |
OwnedProp | Property node which owns its variable. |
Property | Property node. |
ReadOnlyProp | Read-only property node. |
VisitMut | Node visitor from closure. |
Enums
NodeMut | Enumerates derived interfaces for downcasting. |
PropState | Property state. |
Traits
IAction | Action node interface. |
IConsole | Console interface for actions to write output to. |
IList | List of child nodes. |
INode | Node interface. |
IProperty | Property node interface. |
IVisit | Node visitor. |
Functions
Action | |
ClampedProp | |
List | |
OwnedProp | |
Property | |
ReadOnlyProp |