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
// Copyright 2017 James Bendig. See the COPYRIGHT file at the top-level
// directory of this distribution.
//
// Licensed under:
//   the MIT license
//     <LICENSE-MIT or https://opensource.org/licenses/MIT>
//   or the Apache License, Version 2.0
//     <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>,
// at your option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(proc_macro)]
#![crate_type = "proc-macro"]
#![recursion_limit = "256"]

extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate syn;

use proc_macro::TokenStream;
use quote::Tokens;

fn str_to_tokens(input: &str) -> Tokens {
    let mut tokens = Tokens::new();
    tokens.append(input);
    tokens
}

enum ExtractAttributeError {
    BodyNotStruct,
    FieldNotFound,
    AttributeNotFound,
    AttributeNotNameValue,
    AttributeValueWrongType,
}

fn extract_attribute_value(ast: &syn::DeriveInput,field_ident: &'static str,attr_ident: &'static str) -> Result<syn::Lit,ExtractAttributeError> {
    if let syn::Body::Struct(ref data) = ast.body {
        for field in data.fields() {
            if field.ident.as_ref().expect("Field must have an identifier") != field_ident {
                continue;
            }

            for attr in &field.attrs {
                if attr.name() != attr_ident {
                    continue;
                }

                if let syn::MetaItem::NameValue(_,ref lit) = attr.value {
                    return Ok(lit.clone());
                }
                else {
                    return Err(ExtractAttributeError::AttributeNotNameValue);
                }
            }

            return Err(ExtractAttributeError::AttributeNotFound);
        }

        return Err(ExtractAttributeError::FieldNotFound);
    }

    Err(ExtractAttributeError::BodyNotStruct)
}

fn extract_attribute_byte_str(ast: &syn::DeriveInput,field_ident: &'static str,attr_ident: &'static str) -> Result<Vec<u8>,ExtractAttributeError> {
    let lit = try!(extract_attribute_value(ast,field_ident,attr_ident));

    if let syn::Lit::ByteStr(ref bytes,_) = lit {
       return Ok(bytes.clone());
    }

    Err(ExtractAttributeError::AttributeValueWrongType)
}

fn extract_attribute_int(ast: &syn::DeriveInput,field_ident: &'static str,attr_ident: &'static str) -> Result<u64,ExtractAttributeError> {
    let lit = try!(extract_attribute_value(ast,field_ident,attr_ident));

    if let syn::Lit::Int(value,_) = lit {
       return Ok(value);
    }

    Err(ExtractAttributeError::AttributeValueWrongType)
}

#[proc_macro_derive(BuildMessage,attributes(message_type))]
pub fn build_message(input: TokenStream) -> TokenStream {
    let source = input.to_string();
    let ast = syn::parse_derive_input(&source[..]).unwrap();

    let message_type = match extract_attribute_byte_str(&ast,"_message_type_gen","message_type") {
        Ok(bytes) => bytes,
        Err(ExtractAttributeError::BodyNotStruct) => panic!("#[derive(BuildMessage)] can only be used with structs"),
        Err(ExtractAttributeError::FieldNotFound) => panic!("#[derive(BuildMessage)] requires a _message_type_gen field to be specified"),
        Err(ExtractAttributeError::AttributeNotFound) => Vec::new(),
        Err(ExtractAttributeError::AttributeNotNameValue) |
        Err(ExtractAttributeError::AttributeValueWrongType) => panic!("#[derive(BuildMessage)] message_type attribute must be a byte string value like #[message_type=b\"1234\"]"),
    };
    let is_fixt_message = source.contains("sender_comp_id") && source.contains("target_comp_id");

    //Setup symbols.
    let message_name = ast.ident;
    let build_message_name = String::from("Build") + &message_name.to_string()[..];
    let mut message_type_header = "b\"35=".to_string();
    message_type_header += &String::from_utf8_lossy(&message_type[..]).into_owned()[..];
    message_type_header += "\\x01\"";

    //Convert symbols into tokens so quote's ToTokens trait doesn't quote them.
    let build_message_name = str_to_tokens(&build_message_name[..]);
    let message_type_header = str_to_tokens(&message_type_header[..]);

    let tokens = quote! {
        impl #message_name {
            fn msg_type_header() -> &'static [u8] {
                #message_type_header
            }
        }

        pub struct #build_message_name {
            cache: message::BuildMessageInternalCache,
        }

        impl #build_message_name {
            fn new() -> #build_message_name {
                #build_message_name {
                    cache: message::BuildMessageInternalCache {
                        fields_fix40: None,
                        fields_fix41: None,
                        fields_fix42: None,
                        fields_fix43: None,
                        fields_fix44: None,
                        fields_fix50: None,
                        fields_fix50sp1: None,
                        fields_fix50sp2: None,
                    },
                }
            }

            fn new_into_box() -> Box<message::BuildMessage + Send> {
                Box::new(#build_message_name::new())
            }
        }

        impl message::BuildMessage for #build_message_name {
            fn first_field(&self,version: message_version::MessageVersion) -> field_tag::FieldTag {
                #message_name::first_field(version)
            }

            fn field_count(&self,version: message_version::MessageVersion) -> usize {
                #message_name::field_count(version)
            }

            fn fields(&mut self,version: message_version::MessageVersion) -> message::FieldHashMap {
                fn get_or_set_fields(option_fields: &mut Option<message::FieldHashMap>,
                                     version: message_version::MessageVersion) -> message::FieldHashMap {
                    if option_fields.is_none() {
                        let fields = #message_name::fields(version);
                        *option_fields = Some(fields);
                    }

                    option_fields.as_ref().unwrap().clone()
                }

                match version {
                    message_version::MessageVersion::FIX40 => get_or_set_fields(&mut self.cache.fields_fix40,version),
                    message_version::MessageVersion::FIX41 => get_or_set_fields(&mut self.cache.fields_fix41,version),
                    message_version::MessageVersion::FIX42 => get_or_set_fields(&mut self.cache.fields_fix42,version),
                    message_version::MessageVersion::FIX43 => get_or_set_fields(&mut self.cache.fields_fix43,version),
                    message_version::MessageVersion::FIX44 => get_or_set_fields(&mut self.cache.fields_fix44,version),
                    message_version::MessageVersion::FIX50 => get_or_set_fields(&mut self.cache.fields_fix50,version),
                    message_version::MessageVersion::FIX50SP1 => get_or_set_fields(&mut self.cache.fields_fix50sp1,version),
                    message_version::MessageVersion::FIX50SP2 => get_or_set_fields(&mut self.cache.fields_fix50sp2,version),
                }
            }

            fn required_fields(&self,version: message_version::MessageVersion) -> message::FieldHashSet {
                #message_name::required_fields(version)
            }

            fn new_into_box(&self) -> Box<message::BuildMessage + Send> {
                #build_message_name::new_into_box()
            }

            fn build(&self) -> Box<message::Message + Send> {
                Box::new(#message_name::new())
            }
        }


        impl message::MessageBuildable for #message_name {
            fn builder(&self) -> Box<message::BuildMessage + Send> {
                #build_message_name::new_into_box()
            }

            fn builder_func(&self) -> fn() -> Box<message::BuildMessage + Send> {
                #build_message_name::new_into_box
            }
        }
    };
    let mut result = String::from(tokens.as_str());

    if is_fixt_message {
        let tokens = quote! {
            impl fixt::message::BuildFIXTMessage for #build_message_name {
                fn new_into_box(&self) -> Box<fixt::message::BuildFIXTMessage + Send> {
                    Box::new(#build_message_name::new())
                }

                fn build(&self) -> Box<fixt::message::FIXTMessage + Send> {
                    Box::new(#message_name::new())
                }
            }

            impl fixt::message::FIXTMessageBuildable for #message_name {
                fn builder(&self) -> Box<fixt::message::BuildFIXTMessage + Send> {
                    Box::new(#build_message_name::new())
                }
            }
        };
        result += tokens.as_str();
    }

    result.parse().unwrap()
}

#[proc_macro_derive(BuildField,attributes(tag))]
pub fn build_field(input: TokenStream) -> TokenStream {
    let source = input.to_string();
    let ast = syn::parse_derive_input(&source[..]).unwrap();

    let tag = match extract_attribute_int(&ast,"_tag_gen","tag") {
        Ok(bytes) => bytes,
        Err(ExtractAttributeError::BodyNotStruct) => panic!("#[derive(BuildField)] can only be used with structs"),
        Err(ExtractAttributeError::FieldNotFound) => panic!("#[derive(BuildField)] requires a _tag_gen field to be specified"),
        Err(ExtractAttributeError::AttributeNotFound) => panic!("#[derive(BuildField)] requires the _tag_gen field to have the tag attribute"),
        Err(ExtractAttributeError::AttributeNotNameValue) |
        Err(ExtractAttributeError::AttributeValueWrongType) => panic!("#[derive(BuildField)] tag attribute must be as an unsigned integer like #[tag=1234]"),
    };
    let tag = tag.to_string();

    let mut tag_bytes = "b\"".to_string();
    tag_bytes += &tag[..];
    tag_bytes += "\"";

    let field_name = ast.ident;
    let tag = str_to_tokens(&tag[..]);
    let tag_bytes = str_to_tokens(&tag_bytes[..]);

    let tokens = quote! {
        impl #field_name {
            fn tag_bytes() -> &'static [u8] {
                #tag_bytes
            }

            fn tag() -> field_tag::FieldTag {
                field_tag::FieldTag(#tag)
            }
        }
    };
    tokens.parse().unwrap()
}