shi/command/
echo.rs

1use std::fmt::Debug;
2use std::marker::PhantomData;
3
4use super::BaseCommand;
5use crate::error::ShiError;
6use crate::Result;
7
8#[derive(Debug)]
9/// EchoCommand is likely not very useful. It is here mostly for letting users scaffold their
10/// command hierarchies without needing to actually implement or come up with the actual commands
11/// they'd like to have eventually.
12///
13/// As the name suggests, this command simply echos back whatever arguments it receives.
14pub struct EchoCommand<S> {
15    phantom: PhantomData<S>,
16}
17
18impl<S> Default for EchoCommand<S> {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl<S> EchoCommand<S> {
25    /// Creates a new EchoCommand.
26    pub fn new() -> EchoCommand<S> {
27        EchoCommand {
28            phantom: PhantomData,
29        }
30    }
31}
32
33impl<S> BaseCommand for EchoCommand<S> {
34    type State = S;
35
36    fn name(&self) -> &str {
37        "echo"
38    }
39
40    fn validate_args(&self, args: &[String]) -> Result<()> {
41        if args.is_empty() {
42            return Err(ShiError::NoArgs);
43        }
44
45        Ok(())
46    }
47
48    fn execute(&self, _: &mut S, args: &[String]) -> Result<String> {
49        // TODO: We should probably not expose the data type here, and instead return a joined
50        // string.
51        Ok(format!("ECHO: '{:?}'", args))
52    }
53}