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
#![recursion_limit="128"]

extern crate proc_macro;

use darling::FromMeta;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{self, DeriveInput, Fields, Data, DataStruct, Meta, MetaList, MetaNameValue};

type GenericError = Box<dyn std::error::Error + Send + Sync>;
type GenericResult<T> = Result<T, GenericError>;

#[cfg_attr(test, derive(Debug))]
struct Column {
    id: String,
    name: Option<String>,
    alignment: Option<String>,
}

const TABLE_ATTR_NAME: &str = "table";
const COLUMN_ATTR_NAME: &str = "column";

macro_rules! Err {
    ($($arg:tt)*) => (::std::result::Result::Err(format!($($arg)*).into()))
}

#[proc_macro_derive(StaticTable, attributes(table, column))]
pub fn static_table_derive(input: TokenStream) -> TokenStream {
    match static_table_derive_impl(input) {
        Ok(output) => output,
        Err(err) => panic!("{}", err),
    }
}

fn static_table_derive_impl(input: TokenStream) -> GenericResult<TokenStream> {
    let ast: DeriveInput = syn::parse(input)?;
    let span = Span::call_site();

    let table_name = get_table_params(&ast)?;
    let columns = get_table_columns(&ast)?;

    let mod_ident = quote!(crate::formatting::table);
    let table_ident = Ident::new(&table_name, span);
    let row_proxy_ident = Ident::new(&(table_name + "RowProxy"), span);
    let row_ident = &ast.ident;

    let field_idents = columns.iter().map(|column| {
        Ident::new(&column.id, span)
    });

    let columns_init_code = columns.iter().map(|column| {
        let name = column.name.as_ref().unwrap_or(&column.id);
        match column.alignment {
            Some(ref alignment) => {
                let alignment_ident = Ident::new(&alignment.to_uppercase(), span);
                quote!(#mod_ident::Column::new_aligned(
                    #name, #mod_ident::Alignment::#alignment_ident))
            },
            None => quote!(#mod_ident::Column::new(#name))
        }
    });

    let column_modify_code = columns.iter().enumerate().map(|(index, column)| {
        let rename_method_ident = Ident::new(&format!("rename_{}", column.id), span);
        let hide_method_ident = Ident::new(&format!("hide_{}", column.id), span);
        quote! {
            fn #rename_method_ident(&mut self, name: &'static str) {
                self.table.rename_column(#index, name);
            }

            fn #hide_method_ident(&mut self) {
                self.table.hide_column(#index);
            }
        }
    });

    let cell_set_code = columns.iter().enumerate().map(|(index, column)| {
        let method_ident = Ident::new(&format!("set_{}", column.id), span);
        quote! {
            fn #method_ident<C: ::std::convert::Into<#mod_ident::Cell>>(&mut self, cell: C) {
                self.row[#index] = cell.into();
            }
        }
    });

    Ok(quote! {
        struct #table_ident {
            table: #mod_ident::Table,
        }

        impl #table_ident {
            fn new() -> #table_ident {
                let columns = vec![#(#columns_init_code,)*];
                #table_ident {
                    table: #mod_ident::Table::new(columns),
                }
            }

            fn add_row(&mut self, row: #row_ident) -> #row_proxy_ident {
                let row = self.table.add_row(row.into());
                #row_proxy_ident {row: row}
            }

            fn add_empty_row(&mut self) -> #row_proxy_ident {
                let row = self.table.add_empty_row();
                #row_proxy_ident {row: row}
            }

            fn is_empty(&self) -> bool {
                self.table.is_empty()
            }

            #(#column_modify_code)*

            fn print(&self, title: &str) {
                self.table.print(title);
            }
        }

        struct #row_proxy_ident<'a> {
            row: &'a mut #mod_ident::Row,
        }

        impl<'a> #row_proxy_ident<'a> {
            #(#cell_set_code)*
        }

        impl<'a, 'b> ::core::iter::IntoIterator for &'a mut #row_proxy_ident<'b> {
            type Item = &'a mut #mod_ident::Cell;
            type IntoIter = ::std::slice::IterMut<'a, #mod_ident::Cell>;

            fn into_iter(self) -> Self::IntoIter {
                self.row.iter_mut()
            }
        }

        impl ::std::convert::Into<#mod_ident::Row> for #row_ident {
            fn into(self) -> #mod_ident::Row {
                vec![#(self.#field_idents.into(),)*]
            }
        }
    }.into())
}

fn get_table_params(ast: &DeriveInput) -> GenericResult<String> {
    #[derive(FromMeta)]
    struct TableParams {
        name: String,
    }

    let mut table_name = None;

    for attr in &ast.attrs {
        let meta = attr.parse_meta().map_err(|e| format!(
            "Failed to parse `{:#?}`: {}", attr, e))?;

        if match_attribute_name(&meta, COLUMN_ATTR_NAME) {
            return Err!("{:?} attribute is allowed on struct fields only", COLUMN_ATTR_NAME);
        } else if !match_attribute_name(&meta, TABLE_ATTR_NAME) {
            continue;
        }

        let params = TableParams::from_meta(&meta).map_err(|e| format!(
            "{:?} attribute validation error: {}", TABLE_ATTR_NAME, e))?;

        if table_name.replace(params.name).is_some() {
            return Err!("Duplicated {:?} attribute", TABLE_ATTR_NAME);
        }
    }

    Ok(table_name.unwrap_or_else(|| String::from("Table")))
}

fn get_table_columns(ast: &DeriveInput) -> GenericResult<Vec<Column>> {
    #[derive(FromMeta, Default)]
    struct ColumnParams {
        #[darling(default)]
        name: Option<String>,
        #[darling(default)]
        align: Option<String>,
    }

    let mut columns = Vec::new();

    let fields = match ast.data {
        Data::Struct(DataStruct{fields: Fields::Named(ref fields), ..}) => &fields.named,
        _ => return Err!("A struct with named fields is expected"),
    };

    for field in fields {
        let field_name = field.ident.as_ref()
            .ok_or_else(|| "A struct with named fields is expected")?.to_string();
        let mut field_params = None;

        for attr in &field.attrs {
            let meta = attr.parse_meta().map_err(|e| format!(
                "Failed to parse `{:#?}` on {:?} field: {}", attr, field_name, e))?;

            if match_attribute_name(&meta, TABLE_ATTR_NAME) {
                return Err!("{:?} attribute is allowed on struct definition only", TABLE_ATTR_NAME);
            } else if !match_attribute_name(&meta, COLUMN_ATTR_NAME) {
                continue;
            }

            let params = ColumnParams::from_meta(&meta).map_err(|e| format!(
                "{:?} attribute on {:?} field validation error: {}", COLUMN_ATTR_NAME, field_name, e))?;

            match params.align.as_deref() {
                Some("left") | Some("center") | Some("right") | None => {},
                _ => return Err!("Invalid alignment of {:?}: {:?}",
                                 field_name, params.align.unwrap()),
            };

            if field_params.replace(params).is_some() {
                return Err!("Duplicated {:?} attribute on {:?} field", COLUMN_ATTR_NAME, field_name);
            }
        }

        let column_params = field_params.unwrap_or_default();
        columns.push(Column {
            id: field_name,
            name: column_params.name,
            alignment: column_params.align,
        })
    }

    Ok(columns)
}

fn match_attribute_name(meta: &Meta, name: &str) -> bool {
    let path = match meta {
        Meta::Path(path) => path,
        Meta::List(MetaList{path, ..}) => path,
        Meta::NameValue(MetaNameValue{path, ..}) => path,
    };

    path.segments.len() == 1 && path.segments.first().unwrap().ident == name
}