redis_args_impl/
lib.rs

1// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use proc_macro::TokenStream;
6
7mod from_redis_value;
8mod to_redis_args;
9
10/// Can be derived by structs or enums in order to allow conversion to redis args.
11///
12/// This can be used in different variants, either using a format string or the `serde` serialization.
13///
14/// # Format string
15///
16/// The format string is limited to plain `{name}` arguments, no extra formatting allowed.
17/// This restriction is currently necessary, because detecting which fields should be
18/// formatted would be significantly more difficult otherwise.
19///
20/// This variant can only be used with structs, either named or anonymous.
21///
22/// ## Examples
23///
24/// ### Struct with named fields
25///
26/// ```
27/// # #[macro_use] extern crate redis_args;
28/// #[derive(ToRedisArgs)]
29/// #[to_redis_args(fmt = "path:to:id={id}:with:value:{value}")]
30/// struct IdValue {
31///    id: String,
32///    value: u32,
33///    count: usize,
34/// }
35/// ```
36///
37/// ### Struct with unnamed fields
38///
39/// ```
40/// # #[macro_use] extern crate redis_args;
41/// # #[macro_use] extern crate serde;
42/// #[derive(ToRedisArgs)]
43/// #[to_redis_args(fmt = "path:to:{0}:with:{1}")]
44/// struct Example(u32, String);
45/// ````
46///
47/// # Serde
48///
49/// Serializes to JSON using the serde serialization of any item. The item
50/// must derive `serde::Serialize`.
51///
52/// ## Examples
53///
54/// ```
55/// # #[macro_use] extern crate redis_args;
56/// # use serde::Serialize;
57/// #[derive(ToRedisArgs, Serialize)]
58/// #[to_redis_args(serde)]
59/// struct IdValue {
60///     id: String,
61///     value: u32,
62///     count: usize,
63/// }
64/// ```
65#[proc_macro_derive(ToRedisArgs, attributes(to_redis_args))]
66pub fn derive_to_redis_args(input: TokenStream) -> TokenStream {
67    to_redis_args::to_redis_args(input)
68}
69
70/// Can be derived by structs or enums in order to allow conversion from redis values.
71///
72/// This can be used in different variants, either using `FromStr` or the `serde` deserialization.
73///
74/// # FromStr
75///
76/// The item must implement the [`std::str::FromStr`] trait.
77///
78/// ## Example
79///
80/// ```
81/// # #[macro_use] extern crate redis_args;
82/// #[derive(FromRedisValue)]
83/// #[from_redis_value(FromStr)]
84/// struct IdValue {
85///    id: String,
86///    value: u32,
87///    count: usize,
88/// }
89///
90/// # impl std::str::FromStr for IdValue {
91/// #     type Err = String;
92/// #     fn from_str(s: &str)->Result<Self, Self::Err> {
93/// #         unimplemented!()
94/// #     }
95/// # }
96/// ```
97///
98// # Serde
99///
100/// Deserializes from JSON using the serde deserialization of any item. The item
101/// must derive `serde::Deserialize`.
102///
103/// ## Example
104///
105/// ```
106/// # #[macro_use] extern crate redis_args;
107/// # #[macro_use] extern crate serde;
108/// # use serde::Deserialize;
109/// #[derive(FromRedisValue, Deserialize)]
110/// #[from_redis_value(serde)]
111/// struct IdValue {
112///     id: String,
113///     value: u32,
114///     count: usize,
115/// }
116/// ```
117#[proc_macro_derive(FromRedisValue, attributes(from_redis_value))]
118pub fn derive_from_redis_value(input: TokenStream) -> TokenStream {
119    from_redis_value::from_redis_value(input)
120}