forked from aembke/fred.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.rs
152 lines (143 loc) · 5.32 KB
/
options.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{
error::RedisError, interfaces::*, modules::inner::RedisClientInner, protocol::command::RedisCommand, types::Options,
};
use std::{fmt, ops::Deref, sync::Arc};
/// A client interface used to customize command configuration options.
///
/// See [Options](crate::types::Options) for more information.
///
/// ```rust
/// # use fred::prelude::*;
/// # use std::time::Duration;
/// async fn example() -> Result<(), RedisError> {
/// let client = RedisClient::default();
/// client.init().await?;
///
/// let options = Options {
/// max_redirections: Some(3),
/// max_attempts: Some(1),
/// timeout: Some(Duration::from_secs(10)),
/// ..Default::default()
/// };
/// let foo: Option<String> = client.with_options(&options).get("foo").await?;
///
/// // reuse the options bindings
/// let with_options = client.with_options(&options);
/// let foo: () = with_options.get("foo").await?;
/// let bar: () = with_options.get("bar").await?;
///
/// // combine with other client types
/// let pipeline = client.pipeline().with_options(&options);
/// let _: () = pipeline.get("foo").await?;
/// let _: () = pipeline.get("bar").await?;
/// // custom options will be applied to each command
/// println!("results: {:?}", pipeline.all::<i64>().await?);
///
/// Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct WithOptions<C: ClientLike> {
pub(crate) client: C,
pub(crate) options: Options,
}
impl<C: ClientLike> WithOptions<C> {
/// Read the options that will be applied to commands.
pub fn options(&self) -> &Options {
&self.options
}
}
impl<C: ClientLike> Deref for WithOptions<C> {
type Target = C;
fn deref(&self) -> &Self::Target {
&self.client
}
}
impl<C: ClientLike> fmt::Debug for WithOptions<C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WithOptions")
.field("client", &self.client.id())
.field("options", &self.options)
.finish()
}
}
impl<C: ClientLike> ClientLike for WithOptions<C> {
#[doc(hidden)]
fn inner(&self) -> &Arc<RedisClientInner> {
self.client.inner()
}
#[doc(hidden)]
fn change_command(&self, command: &mut RedisCommand) {
self.client.change_command(command);
self.options.apply(command);
}
#[doc(hidden)]
fn send_command<T>(&self, command: T) -> Result<(), RedisError>
where
T: Into<RedisCommand>,
{
let mut command: RedisCommand = command.into();
self.options.apply(&mut command);
self.client.send_command(command)
}
}
#[cfg(feature = "i-acl")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-acl")))]
impl<C: AclInterface> AclInterface for WithOptions<C> {}
#[cfg(feature = "i-client")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-client")))]
impl<C: ClientInterface> ClientInterface for WithOptions<C> {}
#[cfg(feature = "i-cluster")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-cluster")))]
impl<C: ClusterInterface> ClusterInterface for WithOptions<C> {}
#[cfg(feature = "i-pubsub")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-pubsub")))]
impl<C: PubsubInterface> PubsubInterface for WithOptions<C> {}
#[cfg(feature = "i-config")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-config")))]
impl<C: ConfigInterface> ConfigInterface for WithOptions<C> {}
#[cfg(feature = "i-geo")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-geo")))]
impl<C: GeoInterface> GeoInterface for WithOptions<C> {}
#[cfg(feature = "i-hashes")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-hashes")))]
impl<C: HashesInterface> HashesInterface for WithOptions<C> {}
#[cfg(feature = "i-hyperloglog")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-hyperloglog")))]
impl<C: HyperloglogInterface> HyperloglogInterface for WithOptions<C> {}
#[cfg(feature = "i-keys")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-keys")))]
impl<C: KeysInterface> KeysInterface for WithOptions<C> {}
#[cfg(feature = "i-lists")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-lists")))]
impl<C: ListInterface> ListInterface for WithOptions<C> {}
#[cfg(feature = "i-memory")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-memory")))]
impl<C: MemoryInterface> MemoryInterface for WithOptions<C> {}
#[cfg(feature = "i-server")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-server")))]
impl<C: AuthInterface> AuthInterface for WithOptions<C> {}
#[cfg(feature = "i-server")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-server")))]
impl<C: ServerInterface> ServerInterface for WithOptions<C> {}
#[cfg(feature = "i-slowlog")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-slowlog")))]
impl<C: SlowlogInterface> SlowlogInterface for WithOptions<C> {}
#[cfg(feature = "i-sets")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-sets")))]
impl<C: SetsInterface> SetsInterface for WithOptions<C> {}
#[cfg(feature = "i-sorted-sets")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-sorted-sets")))]
impl<C: SortedSetsInterface> SortedSetsInterface for WithOptions<C> {}
#[cfg(feature = "i-streams")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-streams")))]
impl<C: StreamsInterface> StreamsInterface for WithOptions<C> {}
#[cfg(feature = "i-scripts")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-scripts")))]
impl<C: FunctionInterface> FunctionInterface for WithOptions<C> {}
#[cfg(feature = "i-redis-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-redis-json")))]
impl<C: RedisJsonInterface> RedisJsonInterface for WithOptions<C> {}
#[cfg(feature = "i-time-series")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-time-series")))]
impl<C: TimeSeriesInterface> TimeSeriesInterface for WithOptions<C> {}