1use super::*;
8
9
10#[derive(Debug, Clone, Copy)]
18pub struct EventDispatcher (pub(crate) crate::context::ContextHandle);
19impl EventDispatcher {
20
21 pub fn dispatch (self, event: Event) {
26 let r = unsafe {FREDispatchStatusEventAsync(self.0.as_ptr(), event.code.as_ptr(), event.level.as_ptr())};
27 debug_assert!(r.is_ok());
28 }
29
30 pub fn debug (self, message: impl AsRef<str>) {
35 let s = message.as_ref();
36 let code = match UCStr::try_from(s) {
37 Ok(s) => s,
38 Err(e) => format!(
39 "String contains an unexpected NUL byte at index {} (length: {})\n{}",
40 e.nul_position(),
41 s.len(),
42 std::backtrace::Backtrace::capture(),
43 ).try_into().unwrap(),
44 };
45 let evt = Event { level: EventLevel::Debug, code };
46 self.dispatch(evt);
47 }
48}
49unsafe impl Send for EventDispatcher {}
50
51
52#[derive(Debug, Clone)]
55pub struct Event {
56 pub code: UCStr,
57 pub level: EventLevel,
58}
59impl Event {
60 pub fn new (code: UCStr, level: Option<EventLevel>) -> Self {
61 let level = level.unwrap_or_default();
62 Self { code, level }
63 }
64}
65
66
67#[derive(Debug, Clone)]
68pub enum EventLevel {
69 Status,
70 Warning,
71 Error,
72 Debug,
73 Custom(UCStr)
74}
75impl EventLevel {
76 pub fn as_ptr (&self) -> FREStr {
77 const STATUS: FREStr = c"status".as_ptr() as FREStr;
78 const WARNING: FREStr = c"warning".as_ptr() as FREStr;
79 const ERROR: FREStr = c"error".as_ptr() as FREStr;
80 const DEBUG: FREStr = c"debug".as_ptr() as FREStr;
81 match self {
82 Self::Status => STATUS,
83 Self::Warning => WARNING,
84 Self::Error => ERROR,
85 Self::Debug => DEBUG,
86 Self::Custom(s) => s.as_ptr(),
87 }
88 }
89}
90impl Default for EventLevel {
91 fn default() -> Self {Self::Status}
92}