-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.rs
164 lines (143 loc) · 4.28 KB
/
diff.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::cell::RefCell;
use std::rc::Rc;
use std::{cell::Ref, collections::HashMap};
use std::any::Any;
use std::default::Default;
use std::iter::Map;
use hey_listen::sync::{
ParallelDispatcher, ParallelListener, ParallelDispatchResult,
};
use ez_logging::println;
#[macro_use]
extern crate ez_logging;
#[derive(Clone, PartialEq)]
enum Variant {
Int(i32),
Uint(usize),
String(String),
Vector2(f32,f32),
Vector3(f32,f32),
Vector2i(i32,i32),
Vector3i(i32,i32),
} // why do we have this again?
// #[derive(Clone)]
// enum Event {
// Variant,
// }
struct Listener {}
// impl ParallelListener<Event> for Listener {
// fn on_event(&self, _event: &Event) -> Option<ParallelDispatchResult> {
// println!("Listener Registered");
// None
// }
// }
// Move enums and implementations outside of impl blocks
#[derive(Debug)]
enum Error {
InvalidName(String),
NameTaken(String),
FailedToCreateComponent(String)
}
struct Event<Type> { // Generic
name: String,
data: Type,
}
// struct Dispa`tcher {
// events:Vec<Event<dyn Any>>
// }
#[derive(Debug)]
enum ComponentStatus {
Active, // Captures propogated events
Inactive, // Captures propgated events but does not interact
Disabled, // Does not capture events
}
#[derive(Default)]
pub struct Object {
pub name: String,
pub components: HashMap<usize, Rc<RefCell<Box<Component>>>> // and so it begins
}
pub struct Component {
name: String,
status: ComponentStatus,
}
impl Object {
fn add_component(&mut self, new_component: Component) -> Result<Rc<RefCell<Box<Component>>>, Error> {
let id = self.components.len() + 1;
let new_ref = Rc::new(RefCell::new(Box::new(new_component)));
self.components.insert(id, Rc::clone(&new_ref));
Ok(new_ref)
}
// We may decide to remove this later
fn create_component() {
// Implementation pending
}
}
impl Component {
fn new(name: &str) -> Self where Self: Sized {
let new_component = Component {
status: ComponentStatus::Active,
name: String::from(name)
};
// dispatcher.dispatch_event(&Event::Variant);
return new_component;
}
fn ready(&mut self) {
// Implementation pending
println!("{} is ready!",self.name)
}
fn process(&mut self) {
// Implementation pending
}
fn on_event(&mut self, event: &(dyn Any + Send + Sync)) {
// Implementation pending
}
async fn setup(&self) {
}
}
impl ComponentStatus {
fn set(&mut self, new_status: ComponentStatus) -> ComponentStatus {
println!("Set status to {:#?}",new_status);
match new_status {
ComponentStatus::Active => ComponentStatus::Active,
ComponentStatus::Inactive => ComponentStatus::Inactive,
ComponentStatus::Disabled => ComponentStatus::Disabled,
}
}
fn get(&self) -> ComponentStatus { // this is so stupid - caz from liveshare
match self {
ComponentStatus::Active => return ComponentStatus::Active,
ComponentStatus::Inactive => return ComponentStatus::Inactive,
ComponentStatus::Disabled => return ComponentStatus::Disabled,
}
}
fn is_active(&self) -> bool {
match self {
ComponentStatus::Active => true,
_ => false
}
}
}
fn main() {
ez_logging::init();
println!("Hello, world!");
let test: Component = Component::new("name");
let test2: Component = Component::new("name2");
let mut obj: Object = Object::default();
let new_reference = obj.add_component(test).unwrap();
new_reference.borrow_mut().status.set(ComponentStatus::Active);
new_reference.borrow_mut().status.set(ComponentStatus::Disabled);
match new_reference.borrow().status.get() {
ComponentStatus::Active => println!("test"),
_ => panic!()
}
if new_reference.borrow().status.is_active() {
println!("new reference is active")
} else {
panic!("reference not active")
}
new_reference.borrow_mut().name = String::from("sdf");
new_reference.borrow_mut().name = String::from("sdf");
let ref2 = obj.add_component(test2).unwrap();
ref2.borrow_mut().ready();
new_reference.borrow_mut().ready();
}