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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
//! [![github]](https://github.com/markjansnl/corresponding-rs) [![crates-io]](https://crates.io/crates/corresponding-macros) [![docs-rs]](crate)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! <br>
//!
//! This crate provides the procedural macros used in the [corresponding] crate.
//!
//! [corresponding]: https://docs.rs/corresponding/
use proc_macro::TokenStream;
use quote::{ToTokens, __private::TokenTree};
use syn::{
parse_macro_input, parse_quote, AngleBracketedGenericArguments, GenericArgument, Ident, Item,
ItemMod, Path, PathArguments, PathSegment, Stmt, Type, TypePath,
};
#[derive(Debug)]
struct OptionType {
pub ident: Ident,
pub option: bool,
}
/// Use this macro on a module to generate [MoveCorresponding] implementations for all
/// structs in this module. For all structs deriving [Default] also the [From] trait will
/// be implemented.
///
/// # Example
///
/// ```no_run
/// use corresponding::derive_corresponding;
///
/// #[derive_corresponding]
/// mod my_mod {
/// #[derive(Debug, Default)]
/// pub struct A {
/// pub a: u8,
/// pub b: u8,
/// pub c: u8,
/// }
///
/// #[derive(Debug, Clone)]
/// pub struct B {
/// pub a: u8,
/// pub b: Option<u8>,
/// pub d: u8,
/// }
/// }
/// ```
///
/// This will implement [MoveCorresponding] for all combinations of structs within the crate
/// module. The generated implementations are zero cost abstractions and will look like:
///
/// ```no_run
/// # mod my_mod {
/// # #[derive(Default)]
/// # pub struct A {
/// # pub a: u8,
/// # pub b: u8,
/// # pub c: u8,
/// # }
/// #
/// # pub struct B {
/// # pub a: u8,
/// # pub b: Option<u8>,
/// # pub d: u8,
/// # }
/// # }
/// # use corresponding::MoveCorresponding;
/// # use my_mod::*;
/// impl MoveCorresponding<B> for A {
/// fn move_corresponding(&mut self, rhs: B) {
/// self.a = rhs.a;
/// if let Some(r) = rhs.b {
/// self.b = r;
/// }
/// }
/// }
///
/// impl MoveCorresponding<A> for B {
/// fn move_corresponding(&mut self, rhs: A) {
/// self.a = rhs.a;
/// self.b = Some(rhs.b);
/// }
/// }
/// ```
///
/// Because struct A derives [Default], it will also implement [From]. The generated
/// implementation looks like:
///
/// ```no_run
/// # mod my_mod {
/// # #[derive(Default)]
/// # pub struct A {
/// # pub a: u8,
/// # pub b: u8,
/// # pub c: u8,
/// # }
/// #
/// # pub struct B {
/// # pub a: u8,
/// # pub b: Option<u8>,
/// # pub d: u8,
/// # }
/// # }
/// # use corresponding::MoveCorresponding;
/// # impl MoveCorresponding<B> for A {
/// # fn move_corresponding(&mut self, rhs: B) {
/// # self.a = rhs.a;
/// # if let Some(r) = rhs.b {
/// # self.b = r;
/// # }
/// # }
/// # }
/// # use my_mod::*;
/// impl From<B> for A {
/// fn from(rhs: B) -> Self {
/// let mut lhs = A::default();
/// lhs.move_corresponding(rhs);
/// lhs
/// }
/// }
/// ```
///
/// [MoveCorresponding]: https://docs.rs/corresponding/trait.MoveCorresponding.html
///
#[proc_macro_attribute]
pub fn derive_corresponding(_metadata: TokenStream, input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as ItemMod);
if let Some((_, ref mut items)) = input.content {
let structs = get_structs(items);
for l in &structs {
for r in &structs {
if l != r {
items.push(generate_move_corresponding_impl(l, r));
if has_default_derive(l) {
items.push(generate_from_impl(l, r));
}
}
}
}
}
TokenStream::from(input.into_token_stream())
}
/// Get the structs at top level of the module
fn get_structs(items: &[Item]) -> Vec<syn::ItemStruct> {
items
.iter()
.cloned()
.filter_map(|item| match item {
Item::Struct(item_struct) => Some(item_struct),
_ => None,
})
.collect()
}
/// Generate the `impl MoveCorresponding<Right> for Left` from two ItemStructs
fn generate_move_corresponding_impl(l: &syn::ItemStruct, r: &syn::ItemStruct) -> Item {
let l_ident = &l.ident;
let r_ident = &r.ident;
// Prepare the statements
let mut statements: Vec<Stmt> = vec![];
for l_field in &l.fields {
for r_field in &r.fields {
let l_field_ident = &l_field.ident;
let r_field_ident = &r_field.ident;
if let Some(l_type) = get_type(&l_field.ty) {
if let Some(r_type) = get_type(&r_field.ty) {
if l_field_ident == r_field_ident && l_type.ident == r_type.ident {
match (l_type.option, r_type.option) {
(false, false) => statements.push(parse_quote! { self. #l_field_ident = rhs. #r_field_ident ; }),
(true, false) => statements.push(parse_quote! { self. #l_field_ident = Some ( rhs. #r_field_ident ) ; }),
(false, true) => statements.push(parse_quote! { if let Some ( r ) = rhs. #r_field_ident { self. #l_field_ident = r } ; }),
(true, true) => statements.push(parse_quote! { if rhs. #r_field_ident .is_some() { self. #l_field_ident = rhs. #r_field_ident } ; }),
}
}
}
}
}
}
// Generate the impl
parse_quote! {
impl ::corresponding::MoveCorresponding< #r_ident > for #l_ident {
#[inline]
fn move_corresponding(&mut self, rhs: #r_ident ) {
#(#statements)*
}
}
}
}
/// Check whether the given struct has `#[derive(Default)]` attribute
fn has_default_derive(l: &syn::ItemStruct) -> bool {
for attribute in l.clone().attrs {
if let Some(PathSegment { ident, .. }) = attribute.path.segments.first() {
if ident.to_string().as_str() == "derive" {
for token_tree in attribute.tokens {
if let TokenTree::Group(group) = token_tree {
for token_tree2 in group.stream() {
if let TokenTree::Ident(ident) = token_tree2 {
if ident.to_string().as_str() == "Default" {
return true;
}
}
}
}
}
}
}
}
false
}
/// Generate `impl From<Right> for Left`
/// Just construct a new object by using the Default trait
/// and copy the corresponding fields
fn generate_from_impl(l: &syn::ItemStruct, r: &syn::ItemStruct) -> Item {
let l_ident = &l.ident;
let r_ident = &r.ident;
parse_quote! {
impl ::std::convert::From< #r_ident > for #l_ident {
#[inline]
fn from(rhs: #r_ident ) -> Self {
use ::corresponding::MoveCorresponding;
let mut lhs = #l_ident ::default();
lhs.move_corresponding(rhs);
lhs
}
}
}
}
/// Get the type of a field
/// When the type is Option<T>: return type T and option: true
/// Else, for type T: return type T and option: false
/// If we don't know the type, return a None, so the field will not be copied over
fn get_type(ty: &syn::Type) -> Option<OptionType> {
if let Type::Path(TypePath {
path: Path { segments, .. },
..
}) = ty
{
if let Some(segment) = segments.first() {
if segment.ident.to_string().as_str() == "Option" {
if let PathArguments::AngleBracketed(AngleBracketedGenericArguments {
args, ..
}) = segment.arguments.clone()
{
if let Some(GenericArgument::Type(Type::Path(TypePath {
path: Path { segments, .. },
..
}))) = args.first()
{
if let Some(segment) = segments.first() {
return Some(OptionType {
ident: segment.ident.clone(),
option: true,
});
}
}
}
} else {
return Some(OptionType {
ident: segment.ident.clone(),
option: false,
});
}
}
}
None
}