embedded_redis/commands/custom.rs
1//! Abstraction for arbitrary commands.
2//!
3//! [CustomCommand] in combination with [CommandBuilder] can be used for executing arbitrary commands,
4//! which high level logic is not abstracted yet by this crate.
5//!
6//! Response is not evaluated, so pure [Resp2Frame](redis_protocol::resp2::types::BytesFrame)
7//! or [Resp3Frame](redis_protocol::resp3::types::BytesFrame) is returned.
8//! The only exception is that error responses are intercepted and converted to [CommandErrors::ErrorResponse](crate::network::CommandErrors::ErrorResponse)
9//!
10//! *Please consider contributing command abstractions not supported yet.*
11//!
12//! # Basic usage
13//! The following Example demonstrates execution of [ECHO](<https://redis.io/commands/echo/>) command
14//! ```
15//! # async_std::task::block_on(async {
16//!# use core::str::FromStr;
17//!# use core::net::SocketAddr;
18//!# use std_embedded_nal_async::Stack;
19//!# use std_embedded_time::StandardClock;
20//!# use embedded_redis::commands::builder::CommandBuilder;
21//!# use embedded_redis::network::ConnectionHandler;
22//!# use redis_protocol::resp2::types::Resp2Frame;
23//!#
24//! let stack = Stack::default();
25//! let clock = StandardClock::default();
26//!
27//! let mut connection_handler = ConnectionHandler::resp2(SocketAddr::from_str("127.0.0.1:6379").unwrap());
28//! let client = connection_handler.connect(&stack, Some(&clock)).await.unwrap();
29//!
30//! let command = CommandBuilder::new("ECHO").arg_static("Hello World!").to_command();
31//! let response = client.send(command).await.unwrap();
32//! assert_eq!("Hello World!", response.to_string().unwrap());
33//! # });
34//! ```
35use crate::commands::builder::CommandBuilder;
36use crate::commands::{Command, ResponseTypeError};
37
38/// Abstraction for arbitrary commands.
39pub struct CustomCommand {
40 builder: CommandBuilder,
41}
42
43impl CustomCommand {
44 pub fn new(builder: CommandBuilder) -> Self {
45 CustomCommand { builder }
46 }
47}
48
49impl<F> Command<F> for CustomCommand
50where
51 F: From<CommandBuilder>,
52{
53 type Response = F;
54
55 fn encode(&self) -> F {
56 self.builder.clone().into()
57 }
58
59 fn eval_response(&self, frame: F) -> Result<Self::Response, ResponseTypeError> {
60 Ok(frame)
61 }
62}