Skip to main content

command_stream/commands/
exit.rs

1//! Virtual `exit` command implementation
2
3use crate::commands::CommandContext;
4use crate::utils::CommandResult;
5
6/// Execute the exit command
7///
8/// Exits with the specified code (default 0).
9/// Note: This doesn't actually exit the process, it returns the exit code.
10pub async fn exit(ctx: CommandContext) -> CommandResult {
11    let code: i32 = ctx.args.first().and_then(|s| s.parse().ok()).unwrap_or(0);
12
13    CommandResult::error_with_code("", code)
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    #[tokio::test]
21    async fn test_exit_default() {
22        let ctx = CommandContext::new(vec![]);
23        let result = exit(ctx).await;
24        assert_eq!(result.code, 0);
25    }
26
27    #[tokio::test]
28    async fn test_exit_with_code() {
29        let ctx = CommandContext::new(vec!["42".to_string()]);
30        let result = exit(ctx).await;
31        assert_eq!(result.code, 42);
32    }
33}