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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! Interpreter which transforms expressions into the desired output

use crate::ast::{self, Expression, Name, Style, Tree};
use crate::color::*;
use crate::git::Stats;

use std::{fmt, io};

/// Various types of Interpreter errors
#[derive(Debug)]
pub enum InterpreterErr {
    UnexpectedArgs { exp: Expression },
    WriteError(io::Error),
}

impl From<io::Error> for InterpreterErr {
    fn from(e: io::Error) -> Self {
        InterpreterErr::WriteError(e)
    }
}

type State = Result<(StyleContext, bool), InterpreterErr>;

/// The interpreter which transforms a gist expression using the provided stats
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct Interpreter {
    stats: Stats,
    allow_color: bool,
    bash_prompt: bool,
    command_queue: Vec<WriteCommand>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum WriteCommand {
    WriteContext(StyleContext),
    WriteStr(&'static str),
    WriteString(String),
}

impl Interpreter {
    /// Create a new Interpreter with the given stats
    pub fn new(stats: Stats, allow_color: bool, bash_prompt: bool) -> Interpreter {
        Interpreter {
            stats,
            allow_color,
            bash_prompt,
            command_queue: Vec::with_capacity(32),
        }
    }

    /// Evaluate an expression tree and return the resulting formatted `String`
    pub fn evaluate<W: io::Write>(&mut self, exps: &Tree, w: &mut W) -> Result<(), InterpreterErr> {
        if self.allow_color {
            if self.bash_prompt {
                self.command_queue
                    .push(WriteCommand::WriteStr("\u{01}\x1B[0m\u{02}"));
            } else {
                self.command_queue.push(WriteCommand::WriteStr("\x1B[0m"));
            }
        }

        let (_, wrote) = self.interpret_tree(w, &exps, StyleContext::default())?;

        if wrote && self.allow_color {
            if self.bash_prompt {
                write!(w, "\u{01}\x1B[0m\u{02}")?;
            } else {
                write!(w, "\x1B[0m")?;
            }
        }

        self.command_queue.clear();

        Ok(())
    }

    #[inline(always)]
    fn write_queue<W: io::Write>(&mut self, w: &mut W) -> Result<(), InterpreterErr> {
        for command in self.command_queue.drain(..) {
            use WriteCommand::*;
            match command {
                WriteString(s) => write!(w, "{}", s)?,
                WriteContext(c) => c.write_to(w, self.bash_prompt)?,
                WriteStr(s) => write!(w, "{}", s)?,
            }
        }

        Ok(())
    }

    fn interpret_tree<W: io::Write>(
        &mut self,
        w: &mut W,
        exps: &Tree,
        context: StyleContext,
    ) -> State {
        let mut wrote = false;
        for e in exps.clone().0 {
            let (_, wrote_now) = self.interpret(w, &e, context)?;
            wrote = wrote_now | wrote;
        }
        Ok((context, wrote))
    }

    fn interpret<W: io::Write>(&mut self, w: &mut W, exp: &Expression, ctx: StyleContext) -> State {
        use ast::Expression::{Format, Group, Literal, Named};

        match exp {
            Named { ref name, ref sub } => self.interpret_named(w, *name, sub, ctx),
            Group {
                ref l,
                ref r,
                ref sub,
            } => {
                if sub.0.len() > 0 {
                    let len = self.command_queue.len();
                    self.command_queue
                        .push(WriteCommand::WriteString(l.to_string()));
                    if let (_, true) = self.interpret_tree(w, &sub, ctx)? {
                        write!(w, "{}", r)?;
                        Ok((ctx, true))
                    } else {
                        while self.command_queue.len() > len {
                            self.command_queue.pop();
                        }
                        Ok((ctx, false))
                    }
                } else {
                    Ok((ctx, false))
                }
            }
            Literal(ref literal) => {
                self.write_queue(w)?;
                write!(w, "{}", literal)?;
                Ok((ctx, true))
            }
            Format { ref style, ref sub } => self.interpret_format(w, style, sub, ctx),
        }
    }

    #[inline(always)]
    fn optional_prefix<W: io::Write, V1: fmt::Display + Empty, V2: fmt::Display>(
        &mut self,
        w: &mut W,
        sub: &Tree,
        val: V1,
        prefix: V2,
        ctx: StyleContext,
    ) -> State {
        if val.is_empty() {
            return Ok((ctx, false));
        }

        self.write_queue(w)?;

        match sub.0.len() {
            0 => write!(w, "{}{}", prefix, val)?,
            _ => {
                let (_, wrote) = self.interpret_tree(w, sub, ctx)?;
                if wrote {
                    write!(w, "{}", val)?;
                } else {
                    write!(w, "{}{}", prefix, val)?;
                }
            }
        }
        Ok((ctx, true))
    }

    #[inline(always)]
    fn interpret_literal<W: io::Write>(
        &mut self,
        w: &mut W,
        sub: &Tree,
        literal: &str,
        context: StyleContext,
    ) -> State {
        match sub.0.len() {
            0 => {
                write!(w, "{}", literal)?;
                Ok((context, true))
            }
            _ => Err(InterpreterErr::UnexpectedArgs {
                exp: Expression::Named {
                    name: Name::Quote,
                    sub: sub.clone(),
                },
            }),
        }
    }

    #[inline(always)]
    fn interpret_named<W: io::Write>(
        &mut self,
        w: &mut W,
        name: Name,
        sub: &Tree,
        ctx: StyleContext,
    ) -> State {
        use ast::Name::*;
        match name {
            Branch => self.optional_prefix(w, sub, self.stats.branch.clone(), "", ctx),
            Remote => self.optional_prefix(w, sub, self.stats.remote.clone(), "", ctx),
            Ahead => self.optional_prefix(w, sub, self.stats.ahead, "+", ctx),
            Behind => self.optional_prefix(w, sub, self.stats.behind, "-", ctx),
            Conflict => self.optional_prefix(w, sub, self.stats.conflicts, "U", ctx),
            Added => self.optional_prefix(w, sub, self.stats.added_staged, "A", ctx),
            Untracked => self.optional_prefix(w, sub, self.stats.untracked, "?", ctx),
            Modified => self.optional_prefix(w, sub, self.stats.modified_staged, "M", ctx),
            Unstaged => self.optional_prefix(w, sub, self.stats.modified, "M", ctx),
            Deleted => self.optional_prefix(w, sub, self.stats.deleted, "D", ctx),
            DeletedStaged => self.optional_prefix(w, sub, self.stats.deleted_staged, "D", ctx),
            Renamed => self.optional_prefix(w, sub, self.stats.renamed, "R", ctx),
            Stashed => self.optional_prefix(w, sub, self.stats.stashes, "H", ctx),
            Backslash => self.interpret_literal(w, sub, "\\", ctx),
            Quote => self.interpret_literal(w, sub, "'", ctx),
        }
    }

    fn interpret_format<W: io::Write>(
        &mut self,
        w: &mut W,
        style: &[Style],
        sub: &Tree,
        mut context: StyleContext,
    ) -> State {
        let prev = context;
        let len = self.command_queue.len();

        context.extend(style);
        self.command_queue.push(WriteCommand::WriteContext(context));
        if let (_, true) = self.interpret_tree(w, sub, context)? {
            prev.write_difference(w, &context, self.bash_prompt)?;
            Ok((context, true))
        } else {
            while self.command_queue.len() > len {
                self.command_queue.pop();
            }
            Ok((context, false))
        }
    }
}

/// Trait which determines what is empty in the eyes of the Interpreter
///
/// The interpreter simply ignores the macros which correspond to "empty" values.
trait Empty {
    fn is_empty(&self) -> bool;
}

impl Empty for u16 {
    fn is_empty(&self) -> bool {
        *self == 0
    }
}

impl Empty for str {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

impl Empty for String {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

impl<T> Empty for Vec<T> {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

#[cfg(test)]
mod test {

    use super::*;
    use crate::git::Stats;
    use ast;
    use ast::{Expression, Name, Tree};
    use proptest::arbitrary::any;
    use proptest::collection::vec;
    use proptest::strategy::Strategy;

    proptest! {
        #[test]
        fn empty_stats_empty_result(
            name in ast::arb_name()
                .prop_filter("Backslash is never empty".to_owned(),
                             |n| *n != Name::Backslash)
                .prop_filter("Quote is never empty".to_owned(),
                             |n| *n != Name::Quote)
        ) {

            let stats: Stats = Default::default();

            let mut interpreter = Interpreter::new(stats, false, false);

            let exp = Expression::Named { name, sub: Tree::new() };

            let mut output = Vec::new();
            match interpreter.evaluate(&Tree(vec![exp.clone()]), &mut output) {
                Ok(()) => {
                    println!("interpreted {} as {} ({:?})", exp, String::from_utf8_lossy(&output), output);
                    assert!(output.is_empty())
                },
                Err(e) => {
                    println!("{:?}", e);
                    panic!("Error in proptest")
                }
            }
        }

        #[test]
        fn empty_group_empty_result(
            name in ast::arb_name()
                .prop_filter("Backslash is never empty".to_owned(),
                             |n| *n != Name::Backslash)
                .prop_filter("Quote is never empty".to_owned(),
                             |n| *n != Name::Quote)
        ) {
            let stats = Stats::default();
            let interior = Expression::Named { name, sub: Tree::new(), };
            let exp = Expression::Group {
                l: "{".to_string(),
                r: "}".to_string(),
                sub: Tree(vec![interior]),
            };

            let mut interpreter = Interpreter::new(stats, false, false);

            let mut output = Vec::with_capacity(32);
            match interpreter.evaluate(&Tree(vec![exp.clone()]), &mut output) {
                Ok(()) => {
                    println!(
                        "interpreted {} as \"{}\" ({:?})",
                        exp,
                        String::from_utf8(output.clone()).unwrap(),
                        output
                    );
                    prop_assert!(output.is_empty());
                }
                Err(e) => {
                    println!("{:?} printing {}", e,  String::from_utf8(output).unwrap());
                    prop_assert!(false, "Failed to interpret tree");
                }
            }
        }

        #[test]
        fn empty_format_empty_result(
            name in ast::arb_name()
                .prop_filter("Backslash is never empty".to_owned(),
                             |n| *n != Name::Backslash)
                .prop_filter("Quote is never empty".to_owned(),
                             |n| *n != Name::Quote),
            style in vec(ast::arb_style(), 1..10),
            bash_prompt in any::<bool>()
        ) {
            let stats = Stats::default();
            let interior = Expression::Named { name, sub: Tree::new(), };
            let exp = Expression::Format {
                style,
                sub: Tree(vec![interior]),
            };

            let mut interpreter = Interpreter::new(stats, true, bash_prompt);
            let mut output = Vec::with_capacity(32);
            match interpreter.evaluate(&Tree(vec![exp.clone()]), &mut output) {
                Ok(()) => {
                    println!(
                        "interpreted {} as {} ({:?})",
                        exp,
                        String::from_utf8(output.clone()).unwrap(),
                        output
                    );
                    prop_assert!(output.is_empty());
                }
                Err(e) => {
                    println!("{:?} printing {}", e,  String::from_utf8(output.clone()).unwrap());
                    prop_assert!(false, "Failed to interpret tree");
                }
            }
        }
    }
}