@@ -3,6 +3,7 @@ use quote::quote;
33use syn:: ItemFn ;
44
55mod command;
6+ mod redis_value;
67
78/// This proc macro allow to specify that the follow function is a Redis command.
89/// The macro accept the following arguments that discribe the command properties:
@@ -134,3 +135,91 @@ pub fn module_changed_event_handler(_attr: TokenStream, item: TokenStream) -> To
134135 } ;
135136 gen. into ( )
136137}
138+
139+ /// The macro auto generate a [From] implementation that can convert the struct into [RedisValue].
140+ ///
141+ /// Example:
142+ ///
143+ /// ```rust,no_run,ignore
144+ /// #[derive(RedisValue)]
145+ /// struct RedisValueDeriveInner {
146+ /// i: i64,
147+ /// }
148+ ///
149+ /// #[derive(RedisValue)]
150+ /// struct RedisValueDerive {
151+ /// i: i64,
152+ /// f: f64,
153+ /// s: String,
154+ /// u: usize,
155+ /// v: Vec<i64>,
156+ /// v2: Vec<RedisValueDeriveInner>,
157+ /// hash_map: HashMap<String, String>,
158+ /// hash_set: HashSet<String>,
159+ /// ordered_map: BTreeMap<String, RedisValueDeriveInner>,
160+ /// ordered_set: BTreeSet<String>,
161+ /// }
162+ ///
163+ /// #[command(
164+ /// {
165+ /// flags: [ReadOnly, NoMandatoryKeys],
166+ /// arity: -1,
167+ /// key_spec: [
168+ /// {
169+ /// notes: "test redis value derive macro",
170+ /// flags: [ReadOnly, Access],
171+ /// begin_search: Index({ index : 0 }),
172+ /// find_keys: Range({ last_key : 0, steps : 0, limit : 0 }),
173+ /// }
174+ /// ]
175+ /// }
176+ /// )]
177+ /// fn redis_value_derive(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
178+ /// Ok(RedisValueDerive {
179+ /// i: 10,
180+ /// f: 1.1,
181+ /// s: "s".to_owned(),
182+ /// u: 20,
183+ /// v: vec![1, 2, 3],
184+ /// v2: vec![
185+ /// RedisValueDeriveInner { i: 1 },
186+ /// RedisValueDeriveInner { i: 2 },
187+ /// ],
188+ /// hash_map: HashMap::from([("key".to_owned(), "val`".to_owned())]),
189+ /// hash_set: HashSet::from(["key".to_owned()]),
190+ /// ordered_map: BTreeMap::from([("key".to_owned(), RedisValueDeriveInner { i: 10 })]),
191+ /// ordered_set: BTreeSet::from(["key".to_owned()]),
192+ /// }
193+ /// .into())
194+ /// }
195+ /// ```
196+ ///
197+ /// The [From] implementation generates a [RedisValue::OrderMap] such that the fields names
198+ /// are the map keys and the values are the result of running [Into] function on the field
199+ /// value and convert it into a [RedisValue].
200+ ///
201+ /// The code above will generate the following reply (in resp3):
202+ ///
203+ /// ```bash
204+ /// 127.0.0.1:6379> redis_value_derive
205+ /// 1# "f" => (double) 1.1
206+ /// 2# "hash_map" => 1# "key" => "val"
207+ /// 3# "hash_set" => 1~ "key"
208+ /// 4# "i" => (integer) 10
209+ /// 5# "ordered_map" => 1# "key" => 1# "i" => (integer) 10
210+ /// 6# "ordered_set" => 1~ "key"
211+ /// 7# "s" => "s"
212+ /// 8# "u" => (integer) 20
213+ /// 9# "v" =>
214+ /// 1) (integer) 1
215+ /// 2) (integer) 2
216+ /// 3) (integer) 3
217+ /// 10# "v2" =>
218+ /// 1) 1# "i" => (integer) 1
219+ /// 2) 1# "i" => (integer) 2
220+ /// ```
221+ ///
222+ #[ proc_macro_derive( RedisValue ) ]
223+ pub fn redis_value ( item : TokenStream ) -> TokenStream {
224+ redis_value:: redis_value ( item)
225+ }
0 commit comments