redis_args_impl/lib.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
// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
use proc_macro::TokenStream;
mod from_redis_value;
mod to_redis_args;
/// Can be derived by structs or enums in order to allow conversion to redis args.
///
/// This can be used in different variants, either using a format string or the `serde` serialization.
///
/// # Format string
///
/// The format string is limited to plain `{name}` arguments, no extra formatting allowed.
/// This restriction is currently necessary, because detecting which fields should be
/// formatted would be significantly more difficult otherwise.
///
/// This variant can only be used with structs, either named or anonymous.
///
/// ## Examples
///
/// ### Struct with named fields
///
/// ```
/// # #[macro_use] extern crate redis_args;
/// #[derive(ToRedisArgs)]
/// #[to_redis_args(fmt = "path:to:id={id}:with:value:{value}")]
/// struct IdValue {
/// id: String,
/// value: u32,
/// count: usize,
/// }
/// ```
///
/// ### Struct with unnamed fields
///
/// ```
/// # #[macro_use] extern crate redis_args;
/// # #[macro_use] extern crate serde;
/// #[derive(ToRedisArgs)]
/// #[to_redis_args(fmt = "path:to:{0}:with:{1}")]
/// struct Example(u32, String);
/// ````
///
/// # Serde
///
/// Serializes to JSON using the serde serialization of any item. The item
/// must derive `serde::Serialize`.
///
/// ## Examples
///
/// ```
/// # #[macro_use] extern crate redis_args;
/// # use serde::Serialize;
/// #[derive(ToRedisArgs, Serialize)]
/// #[to_redis_args(serde)]
/// struct IdValue {
/// id: String,
/// value: u32,
/// count: usize,
/// }
/// ```
#[proc_macro_derive(ToRedisArgs, attributes(to_redis_args))]
pub fn derive_to_redis_args(input: TokenStream) -> TokenStream {
to_redis_args::to_redis_args(input)
}
/// Can be derived by structs or enums in order to allow conversion from redis values.
///
/// This can be used in different variants, either using `FromStr` or the `serde` deserialization.
///
/// # FromStr
///
/// The item must implement the [`std::str::FromStr`] trait.
///
/// ## Example
///
/// ```
/// # #[macro_use] extern crate redis_args;
/// #[derive(FromRedisValue)]
/// #[from_redis_value(FromStr)]
/// struct IdValue {
/// id: String,
/// value: u32,
/// count: usize,
/// }
///
/// # impl std::str::FromStr for IdValue {
/// # type Err = String;
/// # fn from_str(s: &str)->Result<Self, Self::Err> {
/// # unimplemented!()
/// # }
/// # }
/// ```
///
// # Serde
///
/// Deserializes from JSON using the serde deserialization of any item. The item
/// must derive `serde::Deserialize`.
///
/// ## Example
///
/// ```
/// # #[macro_use] extern crate redis_args;
/// # #[macro_use] extern crate serde;
/// # use serde::Deserialize;
/// #[derive(FromRedisValue, Deserialize)]
/// #[from_redis_value(serde)]
/// struct IdValue {
/// id: String,
/// value: u32,
/// count: usize,
/// }
/// ```
#[proc_macro_derive(FromRedisValue, attributes(from_redis_value))]
pub fn derive_from_redis_value(input: TokenStream) -> TokenStream {
from_redis_value::from_redis_value(input)
}