#[derive(Default)]
struct Foo {
int: i32,
float: f32,
string: String,
}
impl Foo {
fn before_int_changed(&mut self, _args: &[&str], console: &mut cvar::IConsole) {
self.string = self.int.to_string();
let _ = writeln!(console, "Before int has changed!");
}
fn after_float_changed(&mut self, _args: &[&str], console: &mut cvar::IConsole) {
self.string = self.float.to_string();
let _ = writeln!(console, "After float has changed!");
}
}
impl cvar::IVisit for Foo {
fn visit_mut(&mut self, f: &mut FnMut(&mut cvar::INode)) {
f(&mut cvar::Action("int", "", |args, console| self.before_int_changed(args, console)));
f(&mut cvar::Property("int", "", &mut self.int, 0));
f(&mut cvar::Property("float", "", &mut self.float, 0.0));
f(&mut cvar::Action("float", "", |args, console| self.after_float_changed(args, console)));
f(&mut cvar::Property("string", "", &mut self.string, String::new()));
}
}
#[derive(Default)]
struct Nested {
boolean: bool,
foo: Foo,
}
impl cvar::IVisit for Nested {
fn visit_mut(&mut self, f: &mut FnMut(&mut cvar::INode)) {
f(&mut cvar::Property("foo.bool", "", &mut self.boolean, false));
f(&mut cvar::List("foo", "", &mut self.foo));
}
}
fn main() {
let mut nested = Nested::default();
cvar::console::set(&mut nested, "foo.bool", "true").unwrap();
assert!(nested.boolean);
println!("Hit enter to list all the cvars and their values.");
println!("Assign value to cvar with `<name> <value>`.");
loop {
let mut line = String::new();
if read_line(&mut line) {
break;
}
let args: Vec<&str> = line.split_whitespace().collect();
if args.is_empty() {
cvar::console::walk(&mut nested, |path, node| {
match node.as_node_mut() {
cvar::NodeMut::Prop(prop) => {
println!("{} `{}`", path, prop.get());
},
cvar::NodeMut::List(_list) => (),
cvar::NodeMut::Action(_act) => {
println!("{}", path);
},
}
});
continue;
}
let path = args[0];
let args = &args[1..];
if !cvar::console::find(&mut nested, path, |node| {
match node.as_node_mut() {
cvar::NodeMut::Prop(prop) => {
if args.len() >= 1 {
if let Err(err) = prop.set(args[0]) {
println!("Cannot parse `{}`: {}.", args[0], err);
}
}
println!("{} `{}`", path, prop.get());
},
cvar::NodeMut::Action(act) => {
let mut console = cvar::IoConsole::stdout();
act.invoke(args, &mut console);
},
cvar::NodeMut::List(_) => {},
}
}) {
println!("Cannot find `{}`", path);
}
}
}
pub fn read_line(line: &mut String) -> bool {
use std::io;
print!(">>> ");
let _ = io::Write::flush(&mut io::stdout());
return io::stdin().read_line(line).is_err() || line.is_empty();
}