暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.rs 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. pub fn parse(message: &str) -> Result<Command, ParseError> {
  2. let m: String = String::from(message.trim_right());
  3. let mut line = m.lines();
  4. match line.next() {
  5. Some(x) => {
  6. let mut str = x.split_whitespace();
  7. match str.next() {
  8. Some("STAGE") => {
  9. let msg = m[6..].trim_left();
  10. Ok(Command::Stage(msg.to_string()))
  11. }
  12. Some("CONTROL") => {
  13. let cmd = m[8..].trim_left();
  14. Ok(Command::Control(cmd.to_string()))
  15. }
  16. Some("RETRIEVE") => Ok(Command::Retrieve),
  17. Some(_) => Err(ParseError(ErrorType::UnknownCommand)),
  18. None => Err(ParseError(ErrorType::EmptyString)),
  19. }
  20. }
  21. None => Err(ParseError(ErrorType::EmptyString)),
  22. }
  23. }
  24. #[derive(Debug, PartialEq)]
  25. pub enum Command {
  26. Stage(String),
  27. Control(String),
  28. Retrieve,
  29. }
  30. #[derive(Debug, PartialEq)]
  31. pub struct ParseError(pub ErrorType);
  32. #[derive(Debug, PartialEq)]
  33. pub enum ErrorType {
  34. UnknownCommand,
  35. EmptyString,
  36. }