ry-core 0.8.2

Core trait and module registry for Ry-Dit game engine
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! RyDit Core - Trait y Registro para módulos
//!
//! Proporciona la interfaz común que todos los módulos deben implementar.
//!
//! # Versión
//! **v0.8.2** - Sistema Universal Ry (metadata + hot reload hooks)

use serde_json::Value;
use std::collections::HashMap;

/// Resultado de operación de módulo
pub type ModuleResult = Result<Value, ModuleError>;

/// Error de módulo
#[derive(Debug, Clone)]
pub struct ModuleError {
    pub code: String,
    pub message: String,
}

impl std::fmt::Display for ModuleError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "[{}] {}", self.code, self.message)
    }
}

impl std::error::Error for ModuleError {}

/// Metadata de un módulo (v0.8.2+)
///
/// Información descriptiva para carga dinámica y sistema de plugins
#[derive(Debug, Clone, Default)]
pub struct ModuleMetadata {
    /// Nombre del módulo
    pub name: &'static str,
    /// Versión del módulo
    pub version: &'static str,
    /// Autores del módulo
    pub authors: Vec<&'static str>,
    /// Descripción del módulo
    pub description: &'static str,
    /// Licencia (ej: "MIT", "Apache-2.0")
    pub license: &'static str,
    /// Dependencias de otros módulos
    pub dependencies: Vec<&'static str>,
}

impl ModuleMetadata {
    /// Crea una nueva metadata vacía
    pub fn new() -> Self {
        Self::default()
    }

    /// Establece el nombre del módulo
    pub fn with_name(mut self, name: &'static str) -> Self {
        self.name = name;
        self
    }

    /// Establece la versión del módulo
    pub fn with_version(mut self, version: &'static str) -> Self {
        self.version = version;
        self
    }

    /// Establece los autores del módulo
    pub fn with_authors(mut self, authors: Vec<&'static str>) -> Self {
        self.authors = authors;
        self
    }

    /// Establece la descripción del módulo
    pub fn with_description(mut self, description: &'static str) -> Self {
        self.description = description;
        self
    }

    /// Establece la licencia del módulo
    pub fn with_license(mut self, license: &'static str) -> Self {
        self.license = license;
        self
    }

    /// Establece las dependencias del módulo
    pub fn with_dependencies(mut self, deps: Vec<&'static str>) -> Self {
        self.dependencies = deps;
        self
    }
}

/// Trait que todos los módulos deben implementar
///
/// # Ejemplo
/// ```rust
/// use ry_core::{RyditModule, ModuleResult, ModuleMetadata};
/// use serde_json::Value;
/// use std::collections::HashMap;
///
/// struct MiModulo;
///
/// impl RyditModule for MiModulo {
///     fn name(&self) -> &'static str { "mi_modulo" }
///     fn version(&self) -> &'static str { "1.0.0" }
///     fn register(&self) -> HashMap<&'static str, &'static str> {
///         let mut cmds = HashMap::new();
///         cmds.insert("saludar", "Saluda al usuario");
///         cmds
///     }
///     fn execute(&self, command: &str, params: Value) -> ModuleResult {
///         match command {
///             "saludar" => Ok(Value::String("Hola!".to_string())),
///             _ => Err(ry_core::ModuleError {
///                 code: "UNKNOWN_COMMAND".to_string(),
///                 message: format!("Comando desconocido: {}", command),
///             }),
///         }
///     }
/// }
/// ```
pub trait RyditModule: Send + Sync {
    /// Nombre único del módulo
    fn name(&self) -> &'static str;

    /// Versión del módulo
    fn version(&self) -> &'static str;

    /// Registro de comandos disponibles
    /// Retorna: HashMap<nombre_comando, descripción>
    fn register(&self) -> HashMap<&'static str, &'static str>;

    /// Ejecuta un comando con parámetros
    ///
    /// # Arguments
    /// * `command` - Nombre del comando
    /// * `params` - Parámetros JSON
    fn execute(&self, command: &str, params: Value) -> ModuleResult;

    /// Metadata del módulo (v0.8.2+)
    ///
    /// Proporciona información descriptiva para el sistema de plugins
    /// y carga dinámica. Por defecto retorna metadata básica.
    fn metadata(&self) -> ModuleMetadata {
        ModuleMetadata {
            name: self.name(),
            version: self.version(),
            authors: vec![],
            description: "",
            license: "MIT",
            dependencies: vec![],
        }
    }

    /// Hook llamado antes de recargar el módulo (hot reload)
    ///
    /// Permite limpiar recursos o guardar estado antes de una recarga.
    /// Por defecto no hace nada.
    fn on_reload(&mut self) {}

    /// Hook llamado al descargar el módulo
    ///
    /// Permite limpiar recursos asignados.
    /// Por defecto no hace nada.
    fn on_unload(&mut self) {}
}

/// Permite registrar `Box<dyn RyditModule>` directamente
/// Necesario para carga dinámica (libloading) donde el módulo
/// ya viene como `Box<dyn RyditModule>`
impl RyditModule for Box<dyn RyditModule> {
    fn name(&self) -> &'static str {
        self.as_ref().name()
    }
    fn version(&self) -> &'static str {
        self.as_ref().version()
    }
    fn register(&self) -> HashMap<&'static str, &'static str> {
        self.as_ref().register()
    }
    fn execute(&self, command: &str, params: Value) -> ModuleResult {
        self.as_ref().execute(command, params)
    }
    fn metadata(&self) -> ModuleMetadata {
        self.as_ref().metadata()
    }
    fn on_reload(&mut self) {
        self.as_mut().on_reload()
    }
    fn on_unload(&mut self) {
        self.as_mut().on_unload()
    }
}

/// Registro de módulos disponibles (v0.8.2+)
///
/// Soporta carga dinámica, hot reload y metadata de módulos
#[derive(Default)]
pub struct ModuleRegistry {
    modules: HashMap<String, Box<dyn RyditModule>>,
}

impl ModuleRegistry {
    /// Crea un nuevo registro vacío
    pub fn new() -> Self {
        Self {
            modules: HashMap::new(),
        }
    }

    /// Registra un módulo
    pub fn register<M: RyditModule + 'static>(&mut self, module: M) {
        let name = module.name().to_string();
        self.modules.insert(name, Box::new(module));
    }

    /// Obtiene un módulo por nombre
    pub fn get(&self, name: &str) -> Option<&dyn RyditModule> {
        self.modules.get(name).map(|b| b.as_ref())
    }

    /// Obtiene un módulo mutable por nombre (para hot reload)
    pub fn get_mut(&mut self, name: &str) -> Option<&mut Box<dyn RyditModule>> {
        self.modules.get_mut(name)
    }

    /// Lista todos los módulos registrados
    pub fn list(&self) -> Vec<&str> {
        self.modules.keys().map(|s| s.as_str()).collect()
    }

    /// Lista todos los módulos con su metadata (v0.8.2+)
    pub fn list_with_metadata(&self) -> Vec<(&str, ModuleMetadata)> {
        self.modules
            .values()
            .map(|m| (m.name(), m.metadata()))
            .collect()
    }

    /// Recarga un módulo (hot reload) (v0.8.2+)
    ///
    /// Llama al hook `on_reload()` del módulo.
    pub fn reload(&mut self, name: &str) {
        if let Some(module) = self.modules.get_mut(name) {
            module.on_reload();
        }
    }

    /// Descarga un módulo (v0.8.2+)
    ///
    /// Llama al hook `on_unload()` y luego remueve el módulo.
    pub fn unload(&mut self, name: &str) {
        if let Some(mut module) = self.modules.remove(name) {
            module.on_unload();
        }
    }

    /// Verifica si un módulo está registrado
    pub fn contains(&self, name: &str) -> bool {
        self.modules.contains_key(name)
    }

    /// Obtiene el número de módulos registrados
    pub fn len(&self) -> usize {
        self.modules.len()
    }

    /// Verifica si el registro está vacío
    pub fn is_empty(&self) -> bool {
        self.modules.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Módulo de prueba para tests
    struct TestModule;

    impl RyditModule for TestModule {
        fn name(&self) -> &'static str {
            "test"
        }

        fn version(&self) -> &'static str {
            "1.0.0"
        }

        fn register(&self) -> HashMap<&'static str, &'static str> {
            let mut cmds = HashMap::new();
            cmds.insert("ping", "Test ping command");
            cmds.insert("echo", "Test echo command");
            cmds
        }

        fn execute(&self, command: &str, params: Value) -> ModuleResult {
            match command {
                "ping" => Ok(Value::String("pong".to_string())),
                "echo" => Ok(params),
                _ => Err(ModuleError {
                    code: "UNKNOWN_COMMAND".to_string(),
                    message: format!("Unknown command: {}", command),
                }),
            }
        }

        fn metadata(&self) -> ModuleMetadata {
            ModuleMetadata::new()
                .with_name("test")
                .with_version("1.0.0")
                .with_description("Módulo de prueba para tests")
                .with_license("MIT")
        }
    }

    #[test]
    fn test_module_registry() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        assert_eq!(registry.list().len(), 1);
        assert!(registry.get("test").is_some());
        assert!(registry.get("unknown").is_none());
    }

    #[test]
    fn test_module_execute_ping() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        let module = registry.get("test").unwrap();
        let result = module.execute("ping", Value::Null).unwrap();
        assert_eq!(result, Value::String("pong".to_string()));
    }

    #[test]
    fn test_module_execute_echo() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        let module = registry.get("test").unwrap();
        let input = Value::String("hello".to_string());
        let result = module.execute("echo", input.clone()).unwrap();
        assert_eq!(result, input);
    }

    #[test]
    fn test_module_error() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        let module = registry.get("test").unwrap();
        let result = module.execute("unknown", Value::Null);
        assert!(result.is_err());

        let err = result.unwrap_err();
        assert_eq!(err.code, "UNKNOWN_COMMAND");
    }

    #[test]
    fn test_module_metadata() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        let module = registry.get("test").unwrap();
        let metadata = module.metadata();

        assert_eq!(metadata.name, "test");
        assert_eq!(metadata.version, "1.0.0");
        assert_eq!(metadata.description, "Módulo de prueba para tests");
        assert_eq!(metadata.license, "MIT");
    }

    #[test]
    fn test_module_registry_with_metadata() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        let list = registry.list_with_metadata();
        assert_eq!(list.len(), 1);

        let (name, metadata) = &list[0];
        assert_eq!(*name, "test");
        assert_eq!(metadata.name, "test");
    }

    #[test]
    fn test_module_reload() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        // El reload debería funcionar (on_reload por defecto no hace nada)
        registry.reload("test");
        assert!(registry.contains("test"));

        // Reload en módulo inexistente no hace nada
        registry.reload("nonexistent");
        assert!(!registry.contains("nonexistent"));
    }

    #[test]
    fn test_module_unload() {
        let mut registry = ModuleRegistry::new();
        registry.register(TestModule);

        assert!(registry.contains("test"));

        // Unload debería funcionar
        registry.unload("test");
        assert!(!registry.contains("test"));

        // Unload de módulo inexistente no hace nada
        registry.unload("nonexistent");
        assert!(!registry.contains("nonexistent"));
    }

    #[test]
    fn test_module_registry_len() {
        let mut registry = ModuleRegistry::new();
        assert!(registry.is_empty());

        registry.register(TestModule);
        assert_eq!(registry.len(), 1);
        assert!(!registry.is_empty());

        registry.unload("test");
        assert!(registry.is_empty());
    }
}