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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use core::fmt::Debug;
use core::panic::Location;
use std::io::Write;

use crate::{AnsiColorScheme, AnsiColored, DebugAnsiColored, PanicDetector};

/// A structure representing a scoped guard with unwind context with
/// [`core::fmt::Write`] writer.
///
/// If dropped during unwind it will write a message to a given writer
/// containing given function or scope context.
///
/// When this structure is dropped (falls out of scope) and the current thread
/// is not unwinding, the unwind context will be forgotten.
///
/// # Examples
///
/// ```rust
/// use unwind_context::{unwind_context, UnwindContextWithIo};
///
/// fn func(foo: u32, bar: &str, secret: &str) {
///     let _ctx: UnwindContextWithIo<_, _, _> = unwind_context!(fn(foo, bar, ...));
///     // ...
/// }
/// ```
///
/// [`unwind_context`]: crate::unwind_context
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct UnwindContextWithIo<W: Write, T: Debug + DebugAnsiColored, P: PanicDetector> {
    data: T,
    writer: W,
    panic_detector: P,
    color_scheme: Option<&'static AnsiColorScheme>,
    location: &'static Location<'static>,
}

impl<W: Write, T: Debug + DebugAnsiColored, P: PanicDetector> Drop
    for UnwindContextWithIo<W, T, P>
{
    #[inline]
    fn drop(&mut self) {
        if self.panic_detector.is_panicking() {
            self.print();
        }
    }
}

impl<W: Write, T: Debug + DebugAnsiColored, P: PanicDetector> UnwindContextWithIo<W, T, P> {
    /// Create a new `UnwindContextWithFmt` with the provided
    /// [`core::fmt::Write`] writer, context scope data, and color scheme.
    ///
    /// This function is not intended to be used directly. Consider using macros
    /// like [`unwind_context`] or [`unwind_context_with_io`] instead.
    ///
    /// [`unwind_context`]: crate::unwind_context
    /// [`unwind_context_with_io`]: crate::unwind_context_with_io
    #[inline]
    #[must_use = "\
        if unused, the `UnwindContextWithIo` will immediately drop,
        consider binding the `UnwindContextWithIo` like `let _ctx = ...`.
    "]
    #[track_caller]
    pub fn new(
        data: T,
        writer: W,
        panic_detector: P,
        color_scheme: Option<&'static AnsiColorScheme>,
    ) -> Self {
        Self {
            data,
            writer,
            panic_detector,
            color_scheme,
            location: Location::caller(),
        }
    }

    /// Print context to a writer specified in the `UnwindContextWithIo`
    /// constructor.
    ///
    /// This method is called when a panic detected.
    #[cold]
    #[inline(never)]
    pub fn print(&mut self) {
        if let Some(color_scheme) = self.color_scheme {
            let _ = writeln!(
                self.writer,
                "{:?}\n    at {}{}:{}:{}{}",
                AnsiColored::new(&self.data, color_scheme),
                color_scheme.location,
                self.location.file(),
                self.location.line(),
                self.location.column(),
                color_scheme.default,
            );
        } else {
            let _ = writeln!(
                self.writer,
                "{:?}\n    at {}:{}:{}",
                self.data,
                self.location.file(),
                self.location.line(),
                self.location.column(),
            );
        }
        let _ = self.writer.flush();
    }
}

/// Creates [`UnwindContextWithIo`] with a given [`std::io::Write`] writer,
/// panic detector, color scheme, and a given function or scope context.
///
/// If not specified it uses [`std::io::stderr`] as a default writer,
/// [`StdPanicDetector`] as a default panic detector and
/// [`get_default_color_scheme_if_enabled`] as a default color scheme. When
/// using default values for all optional parameters, consider the
/// use of [`unwind_context`] macro instead. See
/// [equivalent macros](#equivalent-macros) section below.
///
/// The returned unwind context scope guard value should be kept alive as long
/// as unwind context is needed. If unused, the [`UnwindContextWithIo`] will
/// immediately drop.
///
/// Passed context arguments are lazily formatted. The created wrapper takes
/// ownership of the given arguments, so it may be necessary to use value
/// references, clones, or pass the pre-prepared string representation. It also
/// supports the `...` placeholder to show that some values have been omitted.
///
/// For more information about context argument, see
/// [`build_unwind_context_data`].
///
/// # Examples
///
/// ```rust
/// use unwind_context::unwind_context_with_io;
///
/// fn example1(foo: u32, bar: &str, secret: &str) {
///     let _ctx = unwind_context_with_io!((fn(foo, bar, ...)), color_scheme = None);
///     // ...
/// }
/// ```
///
/// ```rust
/// use unwind_context::unwind_context_with_io;
///
/// fn example2(foo: u32, bar: &str, secret: &str) {
///     let _ctx = unwind_context_with_io!((fn(foo, bar, ...)), writer = ::std::io::stdout());
///     // ...
/// }
/// ```
///
/// ```rust
/// use unwind_context::{unwind_context_with_io, AnsiColorScheme};
///
/// fn example3<W: std::io::Write, P: unwind_context::PanicDetector>(
///     foo: u32,
///     bar: &str,
///     custom_writer: &mut W,
///     custom_panic_detector: P,
///     custom_color_scheme: &'static AnsiColorScheme,
/// ) {
///     let _ctx = unwind_context_with_io!(
///         (fn(foo, bar)),
///         writer = custom_writer,
///         panic_detector = custom_panic_detector,
///         color_scheme = Some(custom_color_scheme),
///     );
///     // ...
/// }
/// ```
///
/// # Equivalent macros
/// ```rust
/// use unwind_context::{unwind_context, unwind_context_with_io};
///
/// fn func(foo: u32, bar: &str) {
///     let _ctx = unwind_context!(fn(foo, bar));
///     let _ctx = unwind_context_with_io!((fn(foo, bar)));
///     let _ctx = unwind_context_with_io!(
///         (fn(foo, bar)),
///         writer = ::std::io::stderr(),
///         panic_detector = unwind_context::StdPanicDetector,
///         color_scheme = unwind_context::get_default_color_scheme_if_enabled(),
///     );
/// }
/// ```
///
/// [`unwind_context`]: crate::unwind_context
/// [`StdPanicDetector`]: crate::StdPanicDetector
/// [`get_default_color_scheme_if_enabled`]: crate::get_default_color_scheme_if_enabled
/// [`build_unwind_context_data`]: crate::build_unwind_context_data
#[macro_export]
macro_rules! unwind_context_with_io {
    (
        ( $( $context:tt )* )
        $(, writer = $writer:expr )?
        $(, panic_detector = $panic_detector:expr )?
        $(, color_scheme = $color_scheme:expr )?
        $(,)?
    ) => {
        $crate::UnwindContextWithIo::new(
            $crate::build_unwind_context_data!( $($context)* ),
            $crate::expr_or_default_expr!(
                $( $writer )?,
                ::std::io::stderr()
            ),
            $crate::expr_or_default_expr!(
                $( $panic_detector )?,
                $crate::StdPanicDetector
            ),
            $crate::expr_or_default_expr!(
                $( $color_scheme )?,
                $crate::get_default_color_scheme_if_enabled()
            ),
        )
    };
}

/// Creates [`UnwindContextWithIo`] with a given [`std::io::Write`] writer,
/// panic detector, color scheme, and a given function or scope context in debug
/// builds only.
///
/// If not specified it uses [`std::io::stderr`] as a default writer,
/// [`StdPanicDetector`] as a default panic detector and
/// [`get_default_color_scheme_if_enabled`] as a default color scheme. When
/// using default values for all optional parameters, consider the
/// use of [`debug_unwind_context`] macro instead. See
/// [equivalent macros](#equivalent-macros) section below.
///
/// The returned unwind context scope guard value should be kept alive as long
/// as unwind context is needed. If unused, the [`UnwindContextWithIo`] will
/// immediately drop.
///
/// Passed context arguments are lazily formatted. The created wrapper takes
/// ownership of the given arguments, so it may be necessary to use value
/// references, clones, or pass the pre-prepared string representation. It also
/// supports the `...` placeholder to show that some values have been omitted.
///
/// An optimized build will generate `()` unless `-C debug-assertions` is passed
/// to the compiler. This makes this macro no-op with the default release
/// profile.
///
/// For more information about macro arguments, see [`unwind_context_with_io`].
/// For more information about context argument, see
/// [`build_unwind_context_data`].
///
/// # Examples
///
/// ```rust
/// use unwind_context::debug_unwind_context_with_io;
///
/// fn example1(foo: u32, bar: &str, secret: &str) {
///     let _ctx = debug_unwind_context_with_io!((fn(foo, bar, ...)), color_scheme = None);
///     // ...
/// }
/// ```
///
/// ```rust
/// use unwind_context::debug_unwind_context_with_io;
///
/// fn example2(foo: u32, bar: &str, secret: &str) {
///     let _ctx = debug_unwind_context_with_io!((fn(foo, bar, ...)), writer = ::std::io::stdout());
///     // ...
/// }
/// ```
///
/// ```rust
/// use unwind_context::{debug_unwind_context_with_io, AnsiColorScheme};
///
/// fn example3<W: std::io::Write, P: unwind_context::PanicDetector>(
///     foo: u32,
///     bar: &str,
///     custom_writer: &mut W,
///     custom_panic_detector: P,
///     custom_color_scheme: &'static AnsiColorScheme,
/// ) {
///     let _ctx = debug_unwind_context_with_io!(
///         (fn(foo, bar)),
///         writer = custom_writer,
///         panic_detector = custom_panic_detector,
///         color_scheme = Some(custom_color_scheme),
///     );
///     // ...
/// }
/// ```
///
/// # Equivalent macros
/// ```rust
/// use unwind_context::{debug_unwind_context, debug_unwind_context_with_io};
///
/// fn func(foo: u32, bar: &str) {
///     debug_unwind_context!(fn(foo, bar));
///     debug_unwind_context_with_io!((fn(foo, bar)));
///     debug_unwind_context_with_io!(
///         (fn(foo, bar)),
///         writer = ::std::io::stderr(),
///         panic_detector = unwind_context::StdPanicDetector,
///         color_scheme = unwind_context::get_default_color_scheme_if_enabled(),
///     );
/// }
/// ```
///
/// [`unwind_context_with_io`]: crate::unwind_context_with_io
/// [`debug_unwind_context`]: crate::debug_unwind_context
/// [`StdPanicDetector`]: crate::StdPanicDetector
/// [`get_default_color_scheme_if_enabled`]: crate::get_default_color_scheme_if_enabled
/// [`build_unwind_context_data`]: crate::build_unwind_context_data
#[macro_export]
macro_rules! debug_unwind_context_with_io {
    ( $( $tokens:tt )* ) => { $crate::debug_unwind_context_with_io_impl!( $($tokens)* ) };
}

#[doc(hidden)]
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug_unwind_context_with_io_impl {
    ( $( $tokens:tt )* ) => { $crate::unwind_context_with_io!( $($tokens)* ) };
}

#[doc(hidden)]
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug_unwind_context_with_io_impl {
    ($($tokens:tt)*) => {
        ()
    };
}

#[cfg(test)]
mod tests {
    use std::borrow::ToOwned;
    use std::io::{Result as IoResult, Write as IoWrite};
    use std::string::String;
    use std::sync::mpsc;

    use crate::test_common::{check_location_part, TEST_COLOR_SCHEME};
    use crate::test_util::{collect_string_from_recv, PatternMatcher};
    use crate::AnsiColorScheme;

    #[derive(Clone)]
    pub struct Writer(mpsc::Sender<String>);

    impl IoWrite for Writer {
        #[allow(clippy::unwrap_used)]
        fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
            self.0
                .send(String::from_utf8(buf.to_owned()).unwrap())
                .unwrap();
            Ok(buf.len())
        }

        fn flush(&mut self) -> IoResult<()> {
            Ok(())
        }
    }

    fn get_min_line() -> u32 {
        line!()
    }

    #[allow(clippy::unwrap_used)]
    fn func1<W: Clone + IoWrite>(
        foo: usize,
        bar: &str,
        writer: &mut W,
        color_scheme: Option<&'static AnsiColorScheme>,
    ) -> usize {
        let _ctx = unwind_context_with_io!(
            (fn(foo, bar)),
            writer = writer.clone(),
            color_scheme = color_scheme
        );
        func2(foo.checked_mul(2).unwrap(), &bar[1..], writer, color_scheme)
    }

    #[allow(clippy::unwrap_used)]
    fn func2<W: Clone + IoWrite>(
        foo: usize,
        bar: &str,
        writer: &mut W,
        color_scheme: Option<&'static AnsiColorScheme>,
    ) -> usize {
        let _ctx = unwind_context_with_io!(
            (fn(foo, bar)),
            writer = writer.clone(),
            color_scheme = color_scheme
        );
        func3(foo.checked_mul(3).unwrap(), &bar[1..], writer, color_scheme)
    }

    #[allow(clippy::unwrap_used)]
    fn func3<W: IoWrite>(
        foo: usize,
        bar: &str,
        writer: &mut W,
        color_scheme: Option<&'static AnsiColorScheme>,
    ) -> usize {
        let _ctx =
            unwind_context_with_io!((fn(foo, bar)), writer = writer, color_scheme = color_scheme);
        foo.checked_sub(bar.len()).unwrap()
    }

    #[allow(clippy::unwrap_used)]
    fn func_with_debug_unwind_context<W: IoWrite>(
        foo: usize,
        bar: &str,
        #[allow(unused_variables)] writer: &mut W,
        #[allow(unused_variables)] color_scheme: Option<&'static AnsiColorScheme>,
    ) -> usize {
        let _ctx = debug_unwind_context_with_io!(
            (fn(foo, bar)),
            writer = writer,
            color_scheme = color_scheme
        );
        foo.checked_sub(bar.len()).unwrap()
    }

    fn get_max_line() -> u32 {
        line!()
    }

    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_unwind_context_with_io_without_unwind() {
        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = func1(1000, "abcdef", &mut writer, None);
        assert_eq!(result, 5996);
        assert_eq!(collect_string_from_recv(&recv), "");

        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = func1(1000, "ab", &mut writer, None);
        assert_eq!(result, 6000);
        assert_eq!(collect_string_from_recv(&recv), "");
    }

    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_unwind_context_with_io_with_unwind() {
        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = std::panic::catch_unwind(move || func1(1000, "a", &mut writer, None));
        assert!(result.is_err());
        let output = collect_string_from_recv(&recv);
        let output = &mut output.as_str();
        output
            .expect_str("fn func2(foo: 2000, bar: \"\")\n")
            .unwrap();
        check_location_part(output, "", "", file!(), get_min_line(), get_max_line());
        output
            .expect_str("fn func1(foo: 1000, bar: \"a\")\n")
            .unwrap();
        check_location_part(output, "", "", file!(), get_min_line(), get_max_line());
        assert_eq!(*output, "");

        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = std::panic::catch_unwind(move || func1(1000, "", &mut writer, None));
        assert!(result.is_err());
        let output = collect_string_from_recv(&recv);
        let output = &mut output.as_str();
        output
            .expect_str("fn func1(foo: 1000, bar: \"\")\n")
            .unwrap();
        check_location_part(output, "", "", file!(), get_min_line(), get_max_line());
        assert_eq!(*output, "");

        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = std::panic::catch_unwind(move || func1(0, "abcdef", &mut writer, None));
        assert!(result.is_err());
        let output = collect_string_from_recv(&recv);
        let output = &mut output.as_str();
        output
            .expect_str("fn func3(foo: 0, bar: \"cdef\")\n")
            .unwrap();
        check_location_part(output, "", "", file!(), get_min_line(), get_max_line());
        output
            .expect_str("fn func2(foo: 0, bar: \"bcdef\")\n")
            .unwrap();
        check_location_part(output, "", "", file!(), get_min_line(), get_max_line());
        output
            .expect_str("fn func1(foo: 0, bar: \"abcdef\")\n")
            .unwrap();
        check_location_part(output, "", "", file!(), get_min_line(), get_max_line());
        assert_eq!(*output, "");
    }

    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_unwind_context_with_io_with_unwind_with_colored_fmt() {
        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = std::panic::catch_unwind(move || {
            func1(1000, "a", &mut writer, Some(&TEST_COLOR_SCHEME))
        });
        assert!(result.is_err());
        let output = collect_string_from_recv(&recv);
        let output = &mut output.as_str();
        output
            .expect_str(
                "{FN}fn {FN_NAME}func2{FN_BRACE}({DEF}foo: {NUM}2000{DEF}, bar: \
                 {QUOT}\"\"{DEF}{FN_BRACE}){DEF}\n",
            )
            .unwrap();
        check_location_part(
            output,
            "{LOC}",
            "{DEF}",
            file!(),
            get_min_line(),
            get_max_line(),
        );
        output
            .expect_str(
                "{FN}fn {FN_NAME}func1{FN_BRACE}({DEF}foo: {NUM}1000{DEF}, bar: \
                 {QUOT}\"a\"{DEF}{FN_BRACE}){DEF}\n",
            )
            .unwrap();
        check_location_part(
            output,
            "{LOC}",
            "{DEF}",
            file!(),
            get_min_line(),
            get_max_line(),
        );
        assert_eq!(*output, "");
    }

    #[allow(clippy::unwrap_used)]
    #[test]
    fn test_debug_unwind_context_with_io_without_unwind() {
        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = std::panic::catch_unwind(move || {
            func_with_debug_unwind_context(4, "abc", &mut writer, None)
        });

        assert_eq!(result.unwrap(), 1);
        let output = collect_string_from_recv(&recv);
        assert_eq!(output, "");
    }

    #[test]
    fn test_debug_unwind_context_with_io_with_unwind() {
        let (sender, recv) = mpsc::channel();
        let mut writer = Writer(sender);
        let result = std::panic::catch_unwind(move || {
            func_with_debug_unwind_context(2, "abc", &mut writer, None)
        });
        assert!(result.is_err());
        let output = collect_string_from_recv(&recv);
        let output = &mut output.as_str();

        #[cfg(debug_assertions)]
        {
            output
                .expect_str("fn func_with_debug_unwind_context(foo: 2, bar: \"abc\")\n")
                .unwrap();
            check_location_part(output, "", "", file!(), get_min_line(), get_max_line());
            assert_eq!(*output, "");
        }
        assert_eq!(*output, "");
    }
}