larastvel-core 0.2.0

Larastvel framework core - inspired by Laravel
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, RwLock};

use crate::foundation::Application;

/// Error type for console command execution.
#[derive(Debug, Clone)]
pub struct ConsoleError(pub String);

impl fmt::Display for ConsoleError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

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

impl From<String> for ConsoleError {
    fn from(s: String) -> Self {
        ConsoleError(s)
    }
}

/// A registered CLI command — the Rust equivalent of a Laravel `Artisan`
/// console command.
///
/// # Example
///
/// ```rust,ignore
/// use larastvel_core::console::Command;
///
/// struct InspireCommand;
///
/// impl Command for InspireCommand {
///     fn name(&self) -> &'static str {
///         "inspire"
///     }
///     fn description(&self) -> &'static str {
///         "Display an inspiring quote"
///     }
///     fn handle(&self, _app: &Application, _args: &[String]) -> Result<(), ConsoleError> {
///         println!("“Simplicity is the ultimate sophistication.” — Leonardo da Vinci");
///         Ok(())
///     }
/// }
/// ```
pub trait Command: Send + Sync {
    /// The command name used on the CLI (e.g. `"make:model"`, `"serve"`).
    fn name(&self) -> &'static str;

    /// A short description shown in `list` / `--help`.
    fn description(&self) -> &'static str;

    /// Execute the command with the given arguments (excluding the binary and
    /// command name).
    fn handle(&self, app: &Application, args: &[String]) -> Result<(), ConsoleError>;
}

/// The console kernel — registers and dispatches CLI commands.
///
/// Analogous to `App\Console\Kernel` in Laravel. Supports loading commands
/// from a closure, which mirrors the `routes/console.php` pattern.
///
/// # Example
///
/// ```rust,ignore
/// use larastvel_core::console::{ConsoleKernel, Command};
/// use std::sync::Arc;
///
/// let app = larastvel_core::Application::new(None);
/// let kernel = app.console_kernel();
///
/// kernel.add_command(Arc::new(MyCommand));
/// kernel.call("my-command", &[]).await?;
/// ```
pub struct ConsoleKernel {
    app: Application,
    commands: Arc<RwLock<HashMap<&'static str, Arc<dyn Command>>>>,
    loaded: Arc<RwLock<bool>>,
}

impl ConsoleKernel {
    /// Create a new kernel bound to the given application.
    pub fn new(app: Application) -> Self {
        Self {
            app,
            commands: Arc::new(RwLock::new(HashMap::new())),
            loaded: Arc::new(RwLock::new(false)),
        }
    }

    /// Register a command.
    ///
    /// If a command with the same `name()` already exists it is overwritten.
    pub fn add_command(&self, cmd: Arc<dyn Command>) {
        let name = cmd.name();
        self.commands.write().unwrap().insert(name, cmd);
    }

    /// Register multiple commands at once.
    pub fn add_commands(&self, cmds: Vec<Arc<dyn Command>>) {
        let mut map = self.commands.write().unwrap();
        for cmd in cmds {
            map.insert(cmd.name(), cmd);
        }
    }

    /// Returns `true` if a command with the given `name` has been registered.
    pub fn has_command(&self, name: &str) -> bool {
        self.commands.read().unwrap().contains_key(name)
    }

    /// Returns the number of registered commands.
    pub fn command_count(&self) -> usize {
        self.commands.read().unwrap().len()
    }

    /// Returns a list of all registered command names.
    pub fn command_names(&self) -> Vec<String> {
        self.commands
            .read()
            .unwrap()
            .keys()
            .map(|k| k.to_string())
            .collect()
    }

    /// Returns the command with the given name, if registered.
    pub fn get_command(&self, name: &str) -> Option<Arc<dyn Command>> {
        self.commands.read().unwrap().get(name).cloned()
    }

    /// Load commands via a closure — the Rust equivalent of defining
    /// Artisan commands in `routes/console.php`.
    ///
    /// The closure receives this kernel so commands can be registered
    /// with [`add_command`](Self::add_command) or
    /// [`add_commands`](Self::add_commands).
    ///
    /// The closure is guaranteed to run at most once.
    pub fn load(&self, f: impl FnOnce(&Self)) {
        let mut loaded = self.loaded.write().unwrap();
        if !*loaded {
            f(self);
            *loaded = true;
        }
    }

    /// Execute a command by name with the given arguments.
    ///
    /// Returns `Ok(())` on success, or `Err(message)` on failure.
    pub fn call(&self, name: &str, args: &[String]) -> Result<(), ConsoleError> {
        let cmd = self
            .commands
            .read()
            .unwrap()
            .get(name)
            .cloned()
            .ok_or_else(|| format!("Unknown command: {}", name))?;
        cmd.handle(&self.app, args)
    }

    /// Return the underlying application.
    pub fn app(&self) -> &Application {
        &self.app
    }
}

impl Clone for ConsoleKernel {
    fn clone(&self) -> Self {
        Self {
            app: self.app.clone(),
            commands: self.commands.clone(),
            loaded: self.loaded.clone(),
        }
    }
}

// ============================================================================
// Built-in stub commands
// ============================================================================

macro_rules! stub_command {
    ($name:ident, $cmd:expr, $desc:expr) => {
        pub struct $name;
        impl Command for $name {
            fn name(&self) -> &'static str {
                $cmd
            }
            fn description(&self) -> &'static str {
                $desc
            }
            fn handle(&self, _app: &Application, _args: &[String]) -> Result<(), ConsoleError> {
                println!("[stub] {} — {}", $cmd, $desc);
                Ok(())
            }
        }
    };
}

stub_command!(
    ServeCommand,
    "serve",
    "Start the Larastvel development server"
);
stub_command!(RouteListCommand, "route:list", "List all registered routes");
stub_command!(MakeModelCommand, "make:model", "Create a new model class");
stub_command!(
    MakeControllerCommand,
    "make:controller",
    "Create a new controller class"
);
stub_command!(
    MakeMigrationCommand,
    "make:migration",
    "Create a new migration file"
);
stub_command!(MigrateCommand, "migrate", "Run the database migrations");
stub_command!(DbSeedCommand, "db:seed", "Seed the database with records");
stub_command!(
    KeyGenerateCommand,
    "key:generate",
    "Generate a new application key"
);

// ============================================================================
// TESTS
// ============================================================================

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

    struct GreetCommand;
    impl Command for GreetCommand {
        fn name(&self) -> &'static str {
            "greet"
        }
        fn description(&self) -> &'static str {
            "Greet someone"
        }
        fn handle(&self, _app: &Application, args: &[String]) -> Result<(), ConsoleError> {
            let name = args.first().map(|s| s.as_str()).unwrap_or("world");
            println!("Hello, {}!", name);
            Ok(())
        }
    }

    struct FailCommand;
    impl Command for FailCommand {
        fn name(&self) -> &'static str {
            "fail"
        }
        fn description(&self) -> &'static str {
            "Always fails"
        }
        fn handle(&self, _app: &Application, _args: &[String]) -> Result<(), ConsoleError> {
            Err("Something went wrong".to_string().into())
        }
    }

    #[test]
    fn test_add_and_has_command() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        kernel.add_command(Arc::new(GreetCommand));
        assert!(kernel.has_command("greet"));
        assert!(!kernel.has_command("nonexistent"));
    }

    #[test]
    fn test_command_count() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        assert_eq!(kernel.command_count(), 0);
        kernel.add_command(Arc::new(GreetCommand));
        assert_eq!(kernel.command_count(), 1);
        kernel.add_command(Arc::new(FailCommand));
        assert_eq!(kernel.command_count(), 2);
    }

    #[test]
    fn test_command_names() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        kernel.add_command(Arc::new(GreetCommand));
        kernel.add_command(Arc::new(FailCommand));
        let mut names = kernel.command_names();
        names.sort();
        assert_eq!(names, vec!["fail", "greet"]);
    }

    #[test]
    fn test_get_command_returns_arc() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        kernel.add_command(Arc::new(GreetCommand));
        let cmd = kernel.get_command("greet");
        assert!(cmd.is_some());
        assert_eq!(cmd.unwrap().name(), "greet");
    }

    #[test]
    fn test_get_command_nonexistent() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        assert!(kernel.get_command("nope").is_none());
    }

    #[test]
    fn test_call_unknown_command() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        let result = kernel.call("unknown", &[]);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().0, "Unknown command: unknown");
    }

    #[test]
    fn test_call_success() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        kernel.add_command(Arc::new(GreetCommand));
        let result = kernel.call("greet", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_call_failure() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        kernel.add_command(Arc::new(FailCommand));
        let result = kernel.call("fail", &[]);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().0, "Something went wrong");
    }

    #[test]
    fn test_call_with_args() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        kernel.add_command(Arc::new(GreetCommand));
        let result = kernel.call("greet", &["Alice".to_string()]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_add_commands_bulk() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        kernel.add_commands(vec![Arc::new(GreetCommand), Arc::new(FailCommand)]);
        assert_eq!(kernel.command_count(), 2);
        assert!(kernel.has_command("greet"));
        assert!(kernel.has_command("fail"));
    }

    #[test]
    fn test_last_command_wins_on_name_collision() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);

        struct CmdA;
        impl Command for CmdA {
            fn name(&self) -> &'static str {
                "same"
            }
            fn description(&self) -> &'static str {
                "first"
            }
            fn handle(&self, _app: &Application, _args: &[String]) -> Result<(), ConsoleError> {
                Ok(())
            }
        }

        struct CmdB;
        impl Command for CmdB {
            fn name(&self) -> &'static str {
                "same"
            }
            fn description(&self) -> &'static str {
                "second"
            }
            fn handle(&self, _app: &Application, _args: &[String]) -> Result<(), ConsoleError> {
                Ok(())
            }
        }

        kernel.add_command(Arc::new(CmdA));
        kernel.add_command(Arc::new(CmdB));
        let cmd = kernel.get_command("same").unwrap();
        assert_eq!(cmd.description(), "second");
    }

    #[test]
    fn test_load_runs_once() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);
        let counter = std::sync::atomic::AtomicUsize::new(0);

        kernel.load(|k| {
            counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            k.add_command(Arc::new(GreetCommand));
        });

        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert!(kernel.has_command("greet"));

        // Second call should be a no-op
        kernel.load(|_k| {
            counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        });

        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[test]
    fn test_stub_commands_are_registrable() {
        let app = Application::new(None);
        let kernel = ConsoleKernel::new(app);

        let stubs: Vec<Arc<dyn Command>> = vec![
            Arc::new(ServeCommand),
            Arc::new(RouteListCommand),
            Arc::new(MakeModelCommand),
            Arc::new(MakeControllerCommand),
            Arc::new(MakeMigrationCommand),
            Arc::new(MigrateCommand),
            Arc::new(DbSeedCommand),
            Arc::new(KeyGenerateCommand),
        ];

        for stub in &stubs {
            kernel.add_command(stub.clone());
        }

        assert_eq!(kernel.command_count(), 8);
        assert!(kernel.has_command("serve"));
        assert!(kernel.has_command("route:list"));
        assert!(kernel.has_command("make:model"));
        assert!(kernel.has_command("migrate"));
        assert!(kernel.has_command("key:generate"));

        for stub in &stubs {
            let result = kernel.call(stub.name(), &[]);
            assert!(result.is_ok(), "stub {} failed: {:?}", stub.name(), result);
        }
    }
}