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
//! # diff-ba-rs
//!
//! diff-ba-rs is a library to get the differences of variables caused by procedures.

/// Module that provides a set of functions used inside macros.
///
/// This module is available externally but is not intended to be used.
pub mod _diff_ba_impl {
    trait Paint {
        fn red(&self) -> String;
        fn green(&self) -> String;
    }
    impl Paint for str {
        fn red(&self) -> String {
            format!("\u{1b}[31m{}\u{1b}[0m", self)
        }
        fn green(&self) -> String {
            format!("\u{1b}[32m{}\u{1b}[0m", self)
        }
    }

    pub fn _dbg(before: &str, after: &str) {
        for diff in diff::lines(before, after) {
            match diff {
                diff::Result::Left(l) => println!("{}", format!("- {l}").red()),
                diff::Result::Both(l, _) => println!("{}", format!("  {l}")),
                diff::Result::Right(r) => println!("{}", format!("+ {r}").green()),
            }
        }
    }
}

/// Module that provides a set of macros.
pub mod diff_ba {
    /// Prints the procedural change difference
    /// and returns the final expression in the procedural block.
    ///
    /// This macro works with a `Debug` implementation of the given expression type.
    ///
    /// An example:
    ///
    /// ```rust
    /// use diff_ba_rs::prelude::*;
    ///
    /// let mut a = 2;
    /// let b =  diff_ba::dbg!(&a, {
    ///     a *= 2;
    ///     a + 1
    /// });
    /// // prints:
    /// // ```
    /// // - 2
    /// // + 4
    /// // ```
    /// assert_eq!(b, 5);
    /// ```
    #[macro_export]
    macro_rules! dbg_ {
        ($val:expr, $b:block) => {{
            let before = format!("{:#?}", $val);
            let value = $b;
            let after = format!("{:#?}", $val);
            _diff_ba_impl::_dbg(&before, &after);
            value
        }};
    }
    pub use dbg_ as dbg;
}

/// Prelude module to import when using this crate.
pub mod prelude {
    pub use crate::{_diff_ba_impl, diff_ba};
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;

    #[test]
    fn test_dbg_nothing() {
        let mut v = Vec::new();
        v.push("foo");
        v.push("bar");
        let result = diff_ba::dbg!(&v, { v.join(", ") });
        assert_eq!(result, "foo, bar");
    }

    #[test]
    fn test_dbg_add() {
        let mut v = Vec::new();
        v.push("foo");
        v.push("bar");
        let result = diff_ba::dbg!(&v, {
            v.push("baz");
            v.join(", ")
        });
        assert_eq!(result, "foo, bar, baz");
    }

    #[test]
    fn test_dbg_sub() {
        let mut v = Vec::new();
        v.push("foo");
        v.push("bar");
        let result = diff_ba::dbg!(&v, {
            v.pop();
            v.join(", ")
        });
        assert_eq!(result, "foo");
    }

    #[test]
    fn test_dbg_mix() {
        let mut v = Vec::new();
        v.push("foo");
        v.push("bar");
        let result = diff_ba::dbg!(&v, {
            v.pop();
            v.push("baz");
            v.join(", ")
        });
        assert_eq!(result, "foo, baz");
    }
}