Crate cvar

source ·
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

  • Interact with the configuration variables.

Structs

Enums

  • Enumerates derived interfaces for downcasting.
  • Property state.

Traits

Functions