error.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. use crate::value::Value;
  2. #[derive(Debug, Eq, PartialEq)]
  3. pub enum Error {
  4. CommandNotFound(String),
  5. InvalidArgsCount(String),
  6. Protocol(String, String),
  7. WrongArgument(String, String),
  8. Syntax,
  9. NotANumber,
  10. WrongType,
  11. }
  12. impl From<Error> for Value {
  13. fn from(value: Error) -> Value {
  14. let err_type = match value {
  15. Error::WrongType => "WRONGTYPE",
  16. _ => "ERR",
  17. };
  18. let err_msg = match value {
  19. Error::CommandNotFound(x) => format!("unknown command `{}`", x),
  20. Error::InvalidArgsCount(x) => format!("wrong number of arguments for '{}' command", x),
  21. Error::Protocol(x, y) => format!("Protocol error: expected '{}', got '{}'", x, y),
  22. Error::NotANumber => "value is not an integer or out of range".to_owned(),
  23. Error::Syntax => "syntax error".to_owned(),
  24. Error::WrongArgument(x, y) => format!(
  25. "Unknown subcommand or wrong number of arguments for '{}'. Try {} HELP.",
  26. y, x
  27. ),
  28. Error::WrongType => {
  29. "Operation against a key holding the wrong kind of value".to_owned()
  30. }
  31. };
  32. Value::Err(err_type.to_string(), err_msg)
  33. }
  34. }