1use std::{fmt::Display, str::FromStr};
2
3use crate::{command::Command, error::Error};
4
5#[derive(Debug)]
6pub struct Request {
7 command: Command,
8 args: Vec<String>,
9}
10
11impl Into<Command> for Request {
12 fn into(self) -> Command {
13 self.command
14 }
15}
16
17impl From<Command> for Request {
18 fn from(command: Command) -> Self {
19 Self::new(command, &Vec::<String>::new())
20 }
21}
22
23impl Display for Request {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 write!(f, "{}", self.command)?;
26
27 for arg in &self.args {
28 write!(f, " {}", arg)?;
29 }
30
31 Ok(())
32 }
33}
34
35impl FromStr for Request {
36 type Err = Error;
37
38 fn from_str(s: &str) -> Result<Self, Self::Err> {
39 let command: Command = s.parse()?;
40
41 Ok(command.into())
42 }
43}
44
45impl Request {
46 pub fn new<A: Display>(command: Command, args: &Vec<A>) -> Self {
47 Self {
48 command: command.into(),
49 args: args.iter().map(|arg| arg.to_string()).collect(),
50 }
51 }
52
53 pub fn add_arg<A: Display>(&mut self, arg: A) {
54 self.args.push(arg.to_string())
55 }
56
57 pub fn command(&self) -> &Command {
58 &self.command
59 }
60}