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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
extern crate proc_macro;
extern crate proc_macro2;
// Parsing rust code
extern crate syn;
// Creating rust code
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use syn::{ItemStruct, parse_macro_input};
use syn::parse::Parser;
use proc_macro2::{Ident as Ident2, TokenStream as TokenStream2};
use quote::ToTokens;

/// Initialises a struct to be used as a TableRow so it can be used as an entry inside of a
/// [Table](simple_tables_core::Table)
#[proc_macro_attribute]
pub fn table_row(_attrs: TokenStream, input: TokenStream) -> TokenStream {
    let item_struct = parse_macro_input!(input as ItemStruct);
    
    let struct_name = &item_struct.ident;
    
    let fields: Vec<(String, syn::Type)>;
    let mut ident_fields: Vec<(Ident2, syn::Type)> = Vec::new();
    if let syn::Fields::Named(ref _fields) = item_struct.fields {
        let _fields = &_fields.named;
        fields = _fields.iter().map(|field| {
            if let Some(ident) = &field.ident {
                let field_name: String = ident.to_string();
                let field_type = &field.ty;
                let entry = (field_name, field_type.clone());
                
                ident_fields.push((ident.clone(), field_type.clone()));
                
                entry
            } else {
                panic!("Only named fields are supported.")
                // syn::Error::into_compile_error("Only named fields are supported."); // TODO
            }
        }).collect();
    } else {
        panic!("The row struct has no fields.");
    }
    
    let mut field_names: Vec<String> = Vec::new();
    let mut field_types: Vec<syn::Type> = Vec::new();
    
    // (field_names, field_types) = fields.iter().unzip(); // TODO
    fields.iter()
        .for_each(|field| {
        let (_name, _type) = field;
        field_names.push(_name.to_owned());
        field_types.push(_type.to_owned());
    });
    let field_types_strings: Vec<String> = field_types.iter().map(|v| v.to_token_stream().to_string()).collect();
    
    let field_len = fields.iter().count();
    let mut get_field_str_elements: Vec<proc_macro2::TokenStream> = Vec::new();
    for ident_field in ident_fields {
        let ident = ident_field.0;
        let field = quote!( self.#ident );
        get_field_str_elements.push(field);
    }
    let get_field_str = quote!(
        fn get_field_str(&self) -> Vec<String> {
            vec![ #(#get_field_str_elements.to_string(),)* ]
        }
    );
    TokenStream::from (
        quote! (
            use simple_tables::core::TableRow as TableRowTrait;
            
            #[derive(Debug, Clone)]
            #item_struct
            
            impl #struct_name {
                const FIELDS: [&'static str; #field_len] = [#(#field_names),*];
                // const TYPES: [FieldType; #field_len] = [#(#field_types),*];
                const TYPES: [&'static str; #field_len] = [#(#field_types_strings),*];
                
                #get_field_str
            }
            
            impl TableRowTrait for #struct_name {
                fn get_fields() -> Vec<&'static str> {
                    Self::FIELDS.to_vec()
                }
                fn get_field_types() -> Vec<&'static str> {
                    Self::TYPES.to_vec()
                }
                fn field_count() -> usize {
                    return #field_len;
                }
            }
        )
    )
}

/// Initialises a struct to be a Table that holds information about a [table row](macro@crate::table_row).
///
/// # Examples
/// ```rust
/// #[table_row]
/// struct TableRow {
///     id: i32,
///     name: String,
///     email: String
/// }
///
/// #[table(rows = TableRow)]
/// struct Table {}
/// ```
#[proc_macro_attribute]
pub fn table(attrs: TokenStream, input: TokenStream) -> TokenStream {
    let mut item_struct = parse_macro_input!(input as ItemStruct);
    
    // parse the attributes
    // # Attributes:
    // - rows: Ident — '=': Punct — TableRowStruct: Ident
    // - uid: Ident — '=': Punct — "FieldName": Literal (kind: Str)
    let mut current_attr: Option<&str> = None;
    let mut table_row_struct: Option<Ident2> = None;
    let mut uid_field_name: Option<String> = None;
    attrs.into_iter().for_each(|token| {
        match token {
            // https://doc.rust-lang.org/proc_macro/enum.TokenTree.html
            proc_macro::TokenTree::Group(group) => panic!("Unexpected attribute: {}", group),
            proc_macro::TokenTree::Ident(ident) => {
                match &ident.to_string().as_str() {
                    &"rows" =>  current_attr = Some("rows"),
                    &"uid" => current_attr = Some("uid"),
                    val => {
                        if current_attr == Some("rows") {
                            table_row_struct = Some(Ident2::new(val, proc_macro2::Span::call_site()));
                        }  else {
                            panic!("Unexpected token: {}", val);
                        }
                    }
                }
            },
            proc_macro::TokenTree::Punct(punct) => {
                if punct.as_char() == '=' && current_attr.is_some() {
                    // ignored (TODO: enforce syntax)
                } else if punct.as_char() == ',' {
                    if current_attr == None {
                        panic!("Unexpected character: {}", punct.as_char());
                    } else {
                        current_attr = None;
                    }
                } else {
                    panic!("Unkown character: {}", punct);
                }
            },
            proc_macro::TokenTree::Literal(literal) => {
                if current_attr == Some("uid") {
                    // Remove "\"" at begin and end using trim_matches
                    uid_field_name = Some(literal.to_string().trim_matches(|c| c == '\"').to_string());
                }
            }
        }
    });
    
    if let Some(table_row_struct) = table_row_struct {
        let field_to_add = quote!(rows: Vec<#table_row_struct>);
        let struct_name = &item_struct.ident;
        // add field to struct
        if let syn::Fields::Named(ref mut fields) = item_struct.fields {
            fields.named.push(
                syn::Field::parse_named
                    .parse2(field_to_add)
                    .unwrap(),
            );
        }
        
        let uid_code: TokenStream2;
        if let Some(uid) = uid_field_name {
            uid_code = quote!(const UID: &'static str = #uid;);
        } else {
            uid_code = quote!();
        }
        
        let to_string = quote!(
            // The names of the fields
                    let field_names = dev_simple_tables_core_table_row_type::get_fields();
                    // All cells
                    let mut row_values: Vec<Vec<String>> = Vec::new();
                    for row in &self.rows {
                        row_values.push(row.get_field_str());
                    }
                    // The sizes of the columns
                    let mut column_sizes: Vec<usize> = vec![0; field_names.len()];
                    row_values.iter().for_each(|(row_val)| {
                        row_val.iter().enumerate().for_each(|(col, col_val)| {
                            let len = col_val.to_string().chars().count();
                            if column_sizes[col] < len {
                                column_sizes[col] = len;
                            }
                        });
                    });
                    
                    let mut top_line: String = String::from("+-");
                    let mut headers: String = String::from("| ");
                    let mut bottom_line: String = String::from("+=");
                    let mut actual_column_sizes: Vec<usize> = column_sizes.clone();
                    let total_columns = column_sizes.len();
                    column_sizes.into_iter().enumerate().for_each(|(col, col_size)| {
                        let mut local_col_size = col_size.clone();
                        let field_name = field_names[col];
                        let field_len = field_name.chars().count();
                        // Hanlde case when cells are smaller than the title
                        let left_over = if field_len > local_col_size {
                            local_col_size = field_len;
                            actual_column_sizes[col] = field_len;
                            0
                        } else {
                            local_col_size - field_len
                        };
                        top_line.push_str(format!("{}-+", "-".repeat(local_col_size)).as_str());
                        headers.push_str(format!("{}{} |", field_name, " ".repeat(left_over)).as_str());
                        bottom_line.push_str(format!("{}=+", "=".repeat(local_col_size)).as_str());
                        if col != total_columns - 1 {
                            top_line.push_str("-");
                            headers.push_str(" ");
                            bottom_line.push_str("=");
                        }
                    });
                    
                    // Adding the cells to the formatted table
                    let mut cells: String = String::from("| ");
                    row_values.into_iter().enumerate().for_each(|(row, row_val)| {
                        if row != 0 {
                            cells.push_str("\n| ");
                        }
                        row_val.into_iter().enumerate().for_each(|(col, cell_val)| {
                            let left_over = actual_column_sizes[col] - cell_val.chars().count();
                            cells.push_str(format!("{}{} |", cell_val, " ".repeat(left_over)).as_str());
                            if col != total_columns - 1 {
                            cells.push_str(" ");
                        }
                        });
                        // Add horizontal line to bottom
                        cells.push_str(format!("\n{}", top_line).as_str());
                    });
        );
        let impl_to_string = quote!(
            impl ToString for #struct_name {
                fn to_string(&self) -> String {
                    #to_string
                    format!("{}\n{}\n{}\n{}", top_line, headers, bottom_line, cells)
                }
            }
        );
        
        let output = quote! (
            #[automatically_derived]
            #item_struct
            
            impl #struct_name {
                #uid_code
            }
            
            type dev_simple_tables_core_table_row_type = #table_row_struct; // TODO: append the name of the struct to this type to avoid collisions
            
            impl simple_tables::core::Table<#table_row_struct> for #struct_name {
                fn new() -> #struct_name {
                    #struct_name { rows: Vec::new() }
                }
                
                fn from_vec(vec: &Vec<#table_row_struct>) -> #struct_name {
                    #struct_name { rows: vec.to_vec() }
                }
                
                fn get_rows(&self) -> &Vec<#table_row_struct> {
                    &self.rows
                }
                
                fn get_rows_mut(&mut self) -> &mut Vec<#table_row_struct> {
                    &mut self.rows
                }
                
                fn push(&mut self, row: #table_row_struct) {
                    self.rows.push(row);
                }
                
                fn insert_top(&mut self, row: #table_row_struct) {
                    self.rows.insert(0, row);
                }
                
                fn insert(&mut self, i: usize, row: #table_row_struct) {
                    self.rows.insert(i, row);
                }
            }
            
            #impl_to_string
            
            impl std::fmt::Debug for #struct_name {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    #to_string
                    let output = format!("{}\n{}\n{}\n{}", top_line, headers, bottom_line, cells);
                    write!(f, "{}", output)
                }
            }
        );
    
        TokenStream::from(output)
    } else {
        panic!("Please specify a struct to use as the data type for the table rows. \
        e.g. `#[table(rows = TableRowStruct)]`. Refer to the `table` macro documentation for more info.")
    }
}

// https://mbuffett.com/posts/incomplete-macro-walkthrough/