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
//! Value Structs ("classes") derive macros for Rust
//!
//! A very simple derive macros to support strong type system and avoid bare types (like String)
//! for domain types using Rust structs with exactly one unnamed field as a immutable value type.
//!
//! This is similar approach to Haskell's `newtype` (https://wiki.haskell.org/Newtype) or Scala's `AnyVal`.
//!
//! e.g. to declare something like this:
//!
//! ```
//! use rvs_derive::ValueStruct;
//!
//! #[derive(ValueStruct)]
//! struct UserId(String);
//!
//! let uid : UserId = "my-uid".into();
//! ```
//!
//! `ValueStruct` generates for you:
//!  - `std::convert::From<>` instances automatically to help you to create your structs.
//!  - an inline `value()` function to access your field directly without using .0.
//!
//! There are different behaviour for different field types:
//! - for `std::string::String` it generates additional instance for `From<&str>`
//! - for scalar types `value()` the result type isn't a reference, for others it is.
//!

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::*;
use syn::*;


#[proc_macro_derive(ValueStruct)]
pub fn value_struct_macro(input: TokenStream) -> TokenStream {
    let item: syn::Item = syn::parse(input).expect("failed to parse input");
    let span = Span::call_site();
    match item {
        Item::Struct(ref struct_item) => match struct_item.fields {
            Fields::Unnamed(ref unnamed_fields) if unnamed_fields.unnamed.len() == 1 => {
                let field = unnamed_fields.unnamed.first().unwrap();
                let struct_name = &struct_item.ident;
                let field_type = &field.ty;
                let parsed_field_type = parse_field_type(field_type);

                let type_dependent_functions =
                    create_type_dependent_functions(&field_type, parsed_field_type.as_ref());
                let type_dependent_impls =
                    create_dependent_impls(&struct_name, &field_type, parsed_field_type.as_ref());

                let output = quote! {
                    #[allow(dead_code)]
                    impl #struct_name {
                        #type_dependent_functions
                    }

                    #type_dependent_impls
                };

                output.into()
            }
            _ => Error::new(
                span,
                "ValueStruct works only on structs with one unnamed field",
            )
            .to_compile_error()
            .into(),
        },
        _ => Error::new(span, "ValueStruct works only on structs")
            .to_compile_error()
            .into(),
    }
}

enum ParsedType {
    StringType,
    ScalarType
}

#[inline]
fn parse_field_type(field_type: &Type) -> Option<ParsedType> {
    match field_type {
        Type::Path(ref path) => {
            let full_type_path: &String = &path
                .path
                .segments
                .iter()
                .map(|s| s.ident.to_string())
                .collect::<Vec<String>>()
                .join("::");

            match full_type_path.as_str() {
                "String" | "std::string::String" => Some(ParsedType::StringType),
                "i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64"
                | "u128" | "usize" => Some(ParsedType::ScalarType),
                _ => None
            }
        }
        _ => None
    }
}

#[inline]
fn create_dependent_impls(
    struct_name: &Ident,
    field_type: &Type,
    parsed_field_type: Option<&ParsedType>,
) -> proc_macro2::TokenStream {
    match parsed_field_type {
        Some(ParsedType::ScalarType) => {
            quote! {
               impl std::convert::From<#field_type> for #struct_name {
                    fn from(value: #field_type) -> Self {
                        #struct_name(value)
                    }
               }
            }
        }
        Some(ParsedType::StringType) => {
            quote! {
                impl std::convert::From<std::string::String> for #struct_name {
                    fn from(value: String) -> Self {
                        #struct_name(value)
                    }
                }

                impl std::convert::From<&std::string::String> for #struct_name {
                    fn from(value: &String) -> Self {
                        #struct_name(value.clone())
                    }
                }

                impl std::convert::From<&str> for #struct_name {
                    fn from(value: &str) -> Self {
                        #struct_name(String::from(value))
                    }
                }
            }
        }
        _ => {
            quote! {
               impl std::convert::From<#field_type> for #struct_name {
                    fn from(value: #field_type) -> Self {
                        #struct_name(value)
                    }
               }

                impl std::convert::From<&#field_type> for #struct_name {
                    fn from(value: &#field_type) -> Self {
                        #struct_name(value.clone())
                    }
                }
            }
        }
    }
}

#[inline]
fn create_type_dependent_functions(
    field_type: &Type,
    parsed_field_type: Option<&ParsedType>,
) -> proc_macro2::TokenStream {
    match parsed_field_type {
        Some(ParsedType::ScalarType) => {
            quote! {
                #[inline]
                fn value(&self) -> #field_type {
                    self.0
                }
            }
        }
        _ => {
            quote! {
                #[inline]
                fn value(&self) -> &#field_type {
                    &self.0
                }
            }
        }
    }
}