first commit
This commit is contained in:
64
src/objects/event/rhai.rs
Normal file
64
src/objects/event/rhai.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use crate::objects::Event;
|
||||
use rhai::{CustomType, Engine, TypeBuilder};
|
||||
|
||||
impl CustomType for Event {
|
||||
fn build(mut builder: TypeBuilder<Self>) {
|
||||
builder
|
||||
.with_name("Event")
|
||||
.with_fn("new", |ns: String, title: String| Event::new(ns, title))
|
||||
.with_fn("set_description", |event: &mut Event, desc: String| {
|
||||
event.description = Some(desc);
|
||||
event.base_data.update_modified();
|
||||
})
|
||||
.with_fn("set_location", |event: &mut Event, location: String| {
|
||||
event.location = Some(location);
|
||||
event.base_data.update_modified();
|
||||
})
|
||||
.with_fn("set_category", |event: &mut Event, category: String| {
|
||||
event.category = Some(category);
|
||||
event.base_data.update_modified();
|
||||
})
|
||||
.with_fn("set_all_day", |event: &mut Event, all_day: bool| {
|
||||
event.all_day = all_day;
|
||||
event.base_data.update_modified();
|
||||
})
|
||||
.with_fn("get_id", |event: &mut Event| event.base_data.id.clone())
|
||||
.with_fn("get_title", |event: &mut Event| event.title.clone())
|
||||
.with_fn("to_json", |event: &mut Event| {
|
||||
serde_json::to_string_pretty(event).unwrap_or_default()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Register Event API in Rhai engine
|
||||
pub fn register_event_api(engine: &mut Engine) {
|
||||
engine.build_type::<Event>();
|
||||
|
||||
// Register builder-style constructor
|
||||
engine.register_fn("event", |ns: String, title: String| Event::new(ns, title));
|
||||
|
||||
// Register chainable methods that return Self
|
||||
engine.register_fn("description", |mut event: Event, desc: String| {
|
||||
event.description = Some(desc);
|
||||
event.base_data.update_modified();
|
||||
event
|
||||
});
|
||||
|
||||
engine.register_fn("location", |mut event: Event, location: String| {
|
||||
event.location = Some(location);
|
||||
event.base_data.update_modified();
|
||||
event
|
||||
});
|
||||
|
||||
engine.register_fn("category", |mut event: Event, category: String| {
|
||||
event.category = Some(category);
|
||||
event.base_data.update_modified();
|
||||
event
|
||||
});
|
||||
|
||||
engine.register_fn("all_day", |mut event: Event, all_day: bool| {
|
||||
event.all_day = all_day;
|
||||
event.base_data.update_modified();
|
||||
event
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user