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
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(TermObject)]
pub fn term_object_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
        impl TermObject for #name {
            fn position(&self) -> Location {
                self.position
            }
            fn set_position(&mut self, location: &Location) {
                self.position.0 = location.0;
                self.position.1 = location.1;
            }
            fn size(&self) -> Size {
                self.size
            }
            fn set_size(&mut self, size: &Location) {
                self.size.0 = size.0;
                self.size.1 = size.1;
            }
        }
    };
    gen.into()
}

#[proc_macro_derive(Shape)]
pub fn shape_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
        impl Shape for #name {
            fn ascii_for(&self, _location: &Location) -> char {
                ' '
            }
        }
    };
    gen.into()
}

#[proc_macro_derive(Appearance)]
pub fn appearance_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
        impl Appearance for #name {
            fn style_for(&self, _location: &Location) -> ansi_term::Style {
                ansi_term::Style::default()
            }
        }
    };

    gen.into()
}

#[proc_macro_derive(EventHandler)]
pub fn event_handler_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
        impl EventHandler for #name {
            fn on_event(&mut self, event: termion::event::Event) -> io::Result<()> {
                match event {
                    termion::event::Event::Key(termion::event::Key::Ctrl('c')) => {
                        Err(std::io::Error::from(std::io::ErrorKind::Interrupted))
                    }
                    _ => Ok(()),
                }
            }
        }
    };
    gen.into()
}

#[proc_macro_derive(Paint)]
pub fn paint_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
       impl Paint for #name {
           fn paint(
               &self,
               stdout: &mut termion::raw::RawTerminal<std::io::Stdout>,
               location: Location,
           ) -> io::Result<()> {
               let ch = self.ascii_for(&location);
               let style = self.style_for(&location);

               write!(
                   stdout,
                   "{}{}",
                   termion::cursor::Goto(location.0, location.1),
                   style.paint(ch.to_string())
               )
           }
       }
    };
    gen.into()
}

#[proc_macro_derive(Clear)]
pub fn clear_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
        impl Clear for #name {
            fn clear(
                &self,
                stdout: &mut termion::raw::RawTerminal<std::io::Stdout>,
                location: Location,
            ) -> std::io::Result<()> {
                write!(stdout, "{} ", termion::cursor::Goto(location.0, location.1))
            }
        }
    };
    gen.into()
}

#[proc_macro_derive(Render)]
pub fn render_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
        impl Render for #name {
            fn render<R: std::io::Read>(
                &mut self,
                stdout: &mut termion::raw::RawTerminal<std::io::Stdout>,
                events: &mut termion::input::Events<R>,
            ) -> std::io::Result<()> {
                loop {
                    self.paint_all(stdout)?;
                    if let Some(result) = events.next() {
                        let event = result?;
                        if let Err(err) = self.on_event(event) {
                            match err.kind() {
                                std::io::ErrorKind::Interrupted => {
                                    break self.clear_all(stdout);
                                }
                                _ => {
                                    self.clear_all(stdout)?;
                                    break Err(err);
                                }
                            }
                        }
                    }
                }
            }
        }
    };
    gen.into()
}

#[proc_macro_derive(BorderedShape)]
pub fn pordered_shape_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = input.ident;

    let gen = quote! {
        impl Shape for #name {
            fn ascii_for(&self, location: &Location) -> char {
                if self.is_top_left_corner(location) {
                    '┌'
                } else if self.is_top_right_corner(location) {
                    '┐'
                } else if self.is_bottom_left_corner(location) {
                    '└'
                } else if self.is_bottom_right_corner(location) {
                    '┘'
                } else if self.is_right_boundary(location) || self.is_left_boundary(location) {
                    '│'
                } else if self.is_top_boundary(location) || self.is_bottom_boundary(location) {
                    '─'
                } else {
                    ' '
                }
            }
        }
    };
    gen.into()
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_add() {
        assert_eq!(2 + 2, 4);
    }
}