Skip to main content

slash_lib/builtins/
read.rs

1use slash_lang::parser::ast::Arg;
2
3use crate::command::{MethodDef, SlashCommand};
4use crate::executor::{CommandOutput, ExecutionError, PipeValue};
5
6/// `/read(path)` — read a file's contents to stdout.
7///
8/// Pure `std::fs::read`. No shell, no subprocess.
9pub struct Read;
10
11impl SlashCommand for Read {
12    fn name(&self) -> &str {
13        "read"
14    }
15
16    fn methods(&self) -> &[MethodDef] {
17        &[]
18    }
19
20    fn execute(
21        &self,
22        primary: Option<&str>,
23        _args: &[Arg],
24        _input: Option<&PipeValue>,
25    ) -> Result<CommandOutput, ExecutionError> {
26        let path = primary.ok_or_else(|| {
27            ExecutionError::Runner("/read requires a path: /read(file.txt)".into())
28        })?;
29
30        let content = std::fs::read(path)
31            .map_err(|e| ExecutionError::Runner(format!("/read({}): {}", path, e)))?;
32
33        Ok(CommandOutput {
34            stdout: Some(content),
35            stderr: None,
36            success: true,
37        })
38    }
39}