muzzman_lib/
session.rs

1use std::ops::Range;
2use std::path::PathBuf;
3
4use serde::{Deserialize, Serialize};
5
6use crate::prelude::*;
7
8use bytes_kman::TBytes;
9
10#[derive(Clone, Debug, Serialize, Deserialize, bytes_kman::Bytes)]
11pub enum SessionError {
12    InvalidSession,
13    ElementDoNotExist,
14    InsufficientPermissions,
15    InvalidLocation,
16    ServerTimeOut,
17    CannotConnectToServer,
18    ServerInvalidIndentification,
19    InvalidElementStatus,
20    LocationAllreadyExist,
21    InvalidModule,
22    CannotInstallModule(String),
23    AlreadySubscribed,
24    AlreadyUnsubscribed,
25    IsNotElement,
26    IsNotLocation,
27    RawModule(RawLibraryError),
28    Custom(String),
29}
30
31pub type Actions = Vec<(String, MRef, Vec<(String, Value)>)>;
32
33pub trait TSession {
34    //
35    // Module
36    //
37
38    fn load_module(&self, path: PathBuf) -> Result<MRef, SessionError>;
39    fn remove_module(&self, id: ModuleId) -> Result<MRow, SessionError>;
40
41    fn register_action(
42        &self,
43        module_id: &ModuleId,
44        name: String,
45        values: Vec<(String, Value)>,
46        callback: fn(MRef, values: Vec<Type>),
47    ) -> Result<(), SessionError>;
48    fn remove_action(&self, module_id: &ModuleId, name: String) -> Result<(), SessionError>;
49    fn get_actions(&self, range: Range<usize>) -> Result<Actions, SessionError>;
50    fn get_actions_len(&self) -> Result<usize, SessionError>;
51    fn run_action(
52        &self,
53        module_id: &ModuleId,
54        name: String,
55        data: Vec<Type>,
56    ) -> Result<(), SessionError>;
57
58    fn get_modules_len(&self) -> Result<usize, SessionError>;
59    fn get_modules(&self, range: Range<usize>) -> Result<Vec<MRef>, SessionError>;
60
61    fn module_get_name(&self, module_id: &ModuleId) -> Result<String, SessionError>;
62    fn module_set_name(&self, module_id: &ModuleId, name: String) -> Result<(), SessionError>;
63    fn module_get_default_name(&self, module_id: &ModuleId) -> Result<String, SessionError>;
64
65    fn module_get_desc(&self, module_id: &ModuleId) -> Result<String, SessionError>;
66    fn module_set_desc(&self, module_id: &ModuleId, desc: String) -> Result<(), SessionError>;
67    fn module_get_default_desc(&self, module_id: &ModuleId) -> Result<String, SessionError>;
68
69    fn module_get_proxy(&self, module_id: &ModuleId) -> Result<usize, SessionError>;
70    fn module_set_proxy(&self, module_id: &ModuleId, proxy: usize) -> Result<(), SessionError>;
71
72    fn module_get_settings(&self, module_id: &ModuleId) -> Result<Data, SessionError>;
73    fn module_set_settings(&self, module_id: &ModuleId, data: Data) -> Result<(), SessionError>;
74
75    fn module_get_element_settings(&self, module_id: &ModuleId) -> Result<Data, SessionError>;
76    fn module_set_element_settings(
77        &self,
78        module_id: &ModuleId,
79        data: Data,
80    ) -> Result<(), SessionError>;
81
82    fn module_init_location(
83        &self,
84        module_id: &ModuleId,
85        location_id: &LocationId,
86        data: FileOrData,
87    ) -> Result<(), SessionError>;
88
89    fn module_init_element(
90        &self,
91        module_id: &ModuleId,
92        element_id: &ElementId,
93    ) -> Result<(), SessionError>;
94
95    fn module_accept_url(&self, module_id: &ModuleId, url: String) -> Result<bool, SessionError>;
96
97    fn module_accept_extension(
98        &self,
99        module_id: &ModuleId,
100        filename: &str,
101    ) -> Result<bool, SessionError>;
102
103    fn module_accepted_protocols(&self, module_id: &ModuleId) -> Result<Vec<String>, SessionError>;
104
105    fn module_step_element(
106        &self,
107        module_id: &ModuleId,
108        element_id: &ElementId,
109        control_flow: ControlFlow,
110        storage: Storage,
111    ) -> Result<(ControlFlow, Storage), SessionError>;
112
113    fn module_step_location(
114        &self,
115        module_id: &ModuleId,
116        location_id: &LocationId,
117        control_flow: ControlFlow,
118        storage: Storage,
119    ) -> Result<(ControlFlow, Storage), SessionError>;
120
121    //
122    // End Module
123    //
124
125    //
126    // Element
127    //
128
129    fn create_element(&self, name: &str, location_id: &LocationId) -> Result<ERef, SessionError>;
130    fn move_element(
131        &self,
132        element: &ElementId,
133        location_id: &LocationId,
134    ) -> Result<(), SessionError>;
135    fn destroy_element(&self, element_id: ElementId) -> Result<ERow, SessionError>;
136
137    fn element_get_name(&self, element_id: &ElementId) -> Result<String, SessionError>;
138    fn element_set_name(&self, element_id: &ElementId, name: &str) -> Result<(), SessionError>;
139
140    fn element_get_desc(&self, element_id: &ElementId) -> Result<String, SessionError>;
141    fn element_set_desc(&self, element_id: &ElementId, desc: &str) -> Result<(), SessionError>;
142
143    fn element_get_meta(&self, element_id: &ElementId) -> Result<String, SessionError>;
144    fn element_set_meta(&self, element_id: &ElementId, meta: &str) -> Result<(), SessionError>;
145
146    fn element_get_url(&self, element_id: &ElementId) -> Result<Option<String>, SessionError>;
147    fn element_set_url(
148        &self,
149        element_id: &ElementId,
150        url: Option<String>,
151    ) -> Result<(), SessionError>;
152
153    fn element_get_element_data(&self, element_id: &ElementId) -> Result<Data, SessionError>;
154    fn element_set_element_data(
155        &self,
156        element_id: &ElementId,
157        data: Data,
158    ) -> Result<(), SessionError>;
159
160    fn element_get_module_data(&self, element_id: &ElementId) -> Result<Data, SessionError>;
161    fn element_set_module_data(
162        &self,
163        element_id: &ElementId,
164        data: Data,
165    ) -> Result<(), SessionError>;
166
167    fn element_get_module(&self, element_id: &ElementId) -> Result<Option<MRef>, SessionError>;
168    fn element_set_module(
169        &self,
170        element: &ElementId,
171        module: Option<ModuleId>,
172    ) -> Result<(), SessionError>;
173
174    fn element_get_statuses(&self, element_id: &ElementId) -> Result<Vec<String>, SessionError>;
175    fn element_set_statuses(
176        &self,
177        element: &ElementId,
178        statuses: Vec<String>,
179    ) -> Result<(), SessionError>;
180
181    fn element_get_status(&self, element_id: &ElementId) -> Result<usize, SessionError>;
182    fn element_set_status(&self, element_id: &ElementId, status: usize)
183        -> Result<(), SessionError>;
184
185    fn element_get_data(&self, element_id: &ElementId) -> Result<FileOrData, SessionError>;
186    fn element_set_data(
187        &self,
188        element_id: &ElementId,
189        data: FileOrData,
190    ) -> Result<(), SessionError>;
191
192    fn element_get_progress(&self, element_id: &ElementId) -> Result<f32, SessionError>;
193    fn element_set_progress(
194        &self,
195        element_id: &ElementId,
196        progress: f32,
197    ) -> Result<(), SessionError>;
198
199    fn element_get_should_save(&self, element_id: &ElementId) -> Result<bool, SessionError>;
200    fn element_set_should_save(
201        &self,
202        element: &ElementId,
203        should_save: bool,
204    ) -> Result<(), SessionError>;
205
206    fn element_get_enabled(&self, element_id: &ElementId) -> Result<bool, SessionError>;
207    fn element_set_enabled(
208        &self,
209        element_id: &ElementId,
210        enabled: bool,
211        storage: Option<Storage>,
212    ) -> Result<(), SessionError>;
213
214    fn element_resolv_module(&self, element_id: &ElementId) -> Result<bool, SessionError>;
215
216    /// Blocking the current thread until is done!
217    fn element_wait(&self, element_id: &ElementId) -> Result<(), SessionError>;
218
219    fn element_get_element_info(&self, element_id: &ElementId)
220        -> Result<ElementInfo, SessionError>;
221    fn element_notify(&self, element_id: &ElementId, event: Event) -> Result<(), SessionError>;
222
223    fn element_emit(&self, element_id: &ElementId, event: Event) -> Result<(), SessionError>;
224    fn element_subscribe(&self, element_id: &ElementId, _ref: ID) -> Result<(), SessionError>;
225    fn element_unsubscribe(&self, element_id: &ElementId, _ref: ID) -> Result<(), SessionError>;
226
227    //
228    // End Element
229    //
230
231    //
232    // Location
233    //
234
235    fn create_location(&self, name: &str, location_id: &LocationId) -> Result<LRef, SessionError>;
236    fn get_locations_len(&self, location_id: &LocationId) -> Result<usize, SessionError>;
237    fn get_locations(
238        &self,
239        location_id: &LocationId,
240        range: Range<usize>,
241    ) -> Result<Vec<LRef>, SessionError>;
242    fn destroy_location(&self, location_id: LocationId) -> Result<LRow, SessionError>;
243    fn get_default_location(&self) -> Result<LRef, SessionError>;
244    fn move_location(&self, location_id: &LocationId, to: &LocationId) -> Result<(), SessionError>;
245
246    fn location_get_name(&self, location_id: &LocationId) -> Result<String, SessionError>;
247    fn location_set_name(&self, location_id: &LocationId, name: &str) -> Result<(), SessionError>;
248
249    fn location_get_desc(&self, location_id: &LocationId) -> Result<String, SessionError>;
250    fn location_set_desc(&self, location_id: &LocationId, desc: &str) -> Result<(), SessionError>;
251
252    fn location_get_path(&self, location_id: &LocationId) -> Result<PathBuf, SessionError>;
253    fn location_set_path(
254        &self,
255        location_id: &LocationId,
256        path: PathBuf,
257    ) -> Result<(), SessionError>;
258
259    fn location_get_where_is(
260        &self,
261        location_id: &LocationId,
262    ) -> Result<WhereIsLocation, SessionError>;
263    fn location_set_where_is(
264        &self,
265        location_id: &LocationId,
266        where_is: WhereIsLocation,
267    ) -> Result<(), SessionError>;
268
269    fn location_get_should_save(&self, location_id: &LocationId) -> Result<bool, SessionError>;
270    fn location_set_should_save(
271        &self,
272        location_id: &LocationId,
273        should_save: bool,
274    ) -> Result<(), SessionError>;
275
276    fn location_get_elements_len(&self, location_id: &LocationId) -> Result<usize, SessionError>;
277    fn location_get_elements(
278        &self,
279        location_id: &LocationId,
280        range: Range<usize>,
281    ) -> Result<Vec<ERef>, SessionError>;
282
283    fn location_get_location_info(
284        &self,
285        location_id: &LocationId,
286    ) -> Result<LocationInfo, SessionError>;
287    fn location_notify(&self, location_id: &LocationId, event: Event) -> Result<(), SessionError>;
288
289    fn location_emit(&self, location_id: &LocationId, event: Event) -> Result<(), SessionError>;
290    fn location_subscribe(&self, location_id: &LocationId, _ref: ID) -> Result<(), SessionError>;
291    fn location_unsubscribe(&self, location_id: &LocationId, _ref: ID) -> Result<(), SessionError>;
292
293    //
294    // End Location
295    //
296
297    fn get_module_ref(&self, id: &ModuleId) -> Result<MRef, SessionError>;
298    fn get_element_ref(&self, id: &ElementId) -> Result<ERef, SessionError>;
299    fn get_location_ref(&self, id: &LocationId) -> Result<LRef, SessionError>;
300
301    fn c(&self) -> Box<dyn TSession>;
302}