client.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use crate::{connection::Connection, error::Error, option, value::Value};
  2. use bytes::Bytes;
  3. pub fn client(conn: &mut Connection, args: &[Bytes]) -> Result<Value, Error> {
  4. let sub = unsafe { std::str::from_utf8_unchecked(&args[1]) }.to_string();
  5. let expected = match sub.to_lowercase().as_str() {
  6. "setname" => 3,
  7. _ => 2,
  8. };
  9. if args.len() != expected {
  10. return Err(Error::WrongArgument(
  11. "client".to_owned(),
  12. sub.to_uppercase(),
  13. ));
  14. }
  15. match sub.to_lowercase().as_str() {
  16. "id" => Ok((conn.id() as i64).into()),
  17. "info" => Ok(conn.info().as_str().into()),
  18. "getname" => Ok(option!(conn.name().to_owned())),
  19. "setname" => {
  20. let name = unsafe { std::str::from_utf8_unchecked(&args[2]) }.to_string();
  21. conn.set_name(name);
  22. Ok(Value::OK)
  23. }
  24. _ => Err(Error::WrongArgument(
  25. "client".to_owned(),
  26. sub.to_uppercase(),
  27. )),
  28. }
  29. }
  30. pub fn echo(_conn: &Connection, args: &[Bytes]) -> Result<Value, Error> {
  31. Ok(Value::Blob(args[1].to_owned()))
  32. }
  33. pub fn ping(_conn: &Connection, args: &[Bytes]) -> Result<Value, Error> {
  34. match args.len() {
  35. 1 => Ok(Value::String("PONG".to_owned())),
  36. 2 => Ok(Value::Blob(args[1].to_owned())),
  37. _ => Err(Error::InvalidArgsCount("ping".to_owned())),
  38. }
  39. }