tracing-result 0.1.0

An ergonomic wrapper making it easy to emit tracing messages on errors.
Documentation
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
#![cfg_attr(unstable_never_type, feature(never_type))]
#![cfg_attr(unstable_try_trait_v2, feature(try_trait_v2))]

//! A library for ergonomic error handling with tracing support.
//!
//! This crate provides a wrapper around [`Result`], [`Option`] & any other types
//! which implement [`Try`] that automatically emits tracing messages at configurable log levels
//! when unpacked via the `?` operator.
//!
//! # Overview
//!
//! The [`Trace`] trait extends all `Try`-types `T` with methods that wrap it in a [`Traced<T>`]
//! with a custom tracing message. The message is emitted automatically when the [`Traced<T>`] is
//! used with the `?` operator.
//!
//! Simply `use tracing_result::Trace` to bring the methods into scope. The blanket implementation
//! ensures availability on all `Try`-types, not only `Result`& `Option`.
//!
//! There are two families of methods:
//! - **`or_*` methods** ([`or_error`][Trace::or_error], [`or_warn`][Trace::or_warn],
//!   [`or_info`][Trace::or_info], [`or_debug`][Trace::or_debug], [`or_trace`][Trace::or_trace]):
//!   Log the message when an **`Err`**/**`None`** etc. is unpacked
//! - **`and_*` methods** ([`and_error`][Trace::and_error], [`and_warn`][Trace::and_warn],
//!   [`and_info`][Trace::and_info], [`and_debug`][Trace::and_debug], [`and_trace`][Trace::and_trace]):
//!   Log the message when an **`Ok`**/**`Some`** etc. is unpacked
//!
//! # Examples
//!
//! ## `Traced<Result>`
//!
//! ```
//! use std::io;
//! use tracing_result::Trace;
//!
//! fn might_fail() -> io::Result<u32> {
//!     Err(io::Error::other("something went wrong"))
//! }
//!
//! fn compute() -> io::Result<u32> {
//!     // If might_fail() returns Err, "Failed to compute value" will be logged as a warning
//!     might_fail().or_warn("Failed to compute value")?;
//!     Ok(42)
//! }
//!
//! fn unexpected_success() -> io::Result<u32> {
//!     // If Ok is returned, "Unexpected: computation succeeded" will be logged as a warning
//!     let res = io::Result::Ok(42).and_warn("Unexpected: computation succeeded")?;
//!     Ok(res)
//! }
//! ```
//!
//! ## `Traced<Option>`
//!
//! ```
//! use tracing_result::Trace;
//! use try_v2::{Extract, Transform};
//!
//! #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
//! struct Data {
//!     value: Option<i32>,
//! }
//!
//! let data: Vec<Data> = Vec::new();
//!
//! let first_value = data
//!     .first()
//!     .or_warn("No entries found") // Emitted if `None`
//!     .and_then(|d| d.value) // `Transform` methods call `Try::branch` and therefore emit
//!     .or_warn("No value in first entry") // Emitted if `None`
//!     .unwrap_or_default(); // `Extract` methods call `Try::branch` and therefore emit
//!
//! assert_eq!(first_value, 0);
//! ```

use std::ops::{ControlFlow, FromResidual, Try};
use tracing::Level;
use try_v2::{Extract, Transform};

use crate::TracingConfig::{OnOutput, OnResidual};

/// The [`Trace`] trait extends all `Try`-types `T` with methods that wrap it in a [`Traced<T>`]
/// with a custom tracing message. The message is emitted automatically when the [`Traced<T>`] is
/// used with the `?` operator.
///
/// Simply `use tracing_result::Trace` to bring the methods into scope. The blanket implementation
/// ensures availability on all `Try`-types, not only `Result`& `Option`.
///
/// There are two families of methods:
/// - **`or_*` methods** ([`or_error`][Trace::or_error], [`or_warn`][Trace::or_warn],
///   [`or_info`][Trace::or_info], [`or_debug`][Trace::or_debug], [`or_trace`][Trace::or_trace]):
///   Log the message when an **`Err`**/**`None`** etc. is unpacked
/// - **`and_*` methods** ([`and_error`][Trace::and_error], [`and_warn`][Trace::and_warn],
///   [`and_info`][Trace::and_info], [`and_debug`][Trace::and_debug], [`and_trace`][Trace::and_trace]):
///   Log the message when an **`Ok`**/**`Some`** etc. is unpacked
///
/// # Examples
///
/// ## `Traced<Result>`
///
/// ```
/// use std::io;
/// use tracing_result::Trace;
///
/// fn might_fail() -> io::Result<u32> {
///     Err(io::Error::other("something went wrong"))
/// }
///
/// fn compute() -> io::Result<u32> {
///     // If might_fail() returns Err, "Failed to compute value" will be logged as a warning
///     might_fail().or_warn("Failed to compute value")?;
///     Ok(42)
/// }
///
/// fn unexpected_success() -> io::Result<u32> {
///     // If Ok is returned, "Unexpected: computation succeeded" will be logged as a warning
///     let res = io::Result::Ok(42).and_warn("Unexpected: computation succeeded")?;
///     Ok(res)
/// }
/// ```
///
/// ## `Traced<Option>`
///
/// ```
/// use tracing_result::Trace;
/// use try_v2::{Extract, Transform};
///
/// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// struct Data {
///     value: Option<i32>,
/// }
///
/// let data: Vec<Data> = Vec::new();
///
/// let first_value = data
///     .first()
///     .or_warn("No entries found") // Emitted if `None`
///     .and_then(|d| d.value) // `Transform` methods call `Try::branch` and therefore emit
///     .or_warn("No value in first entry") // Emitted if `None`
///     .unwrap_or_default(); // `Extract` methods call `Try::branch` and therefore emit
///
/// assert_eq!(first_value, 0);
/// ```
pub trait Trace<T: Try> {
    /// Attaches an error-level message to the `Residual` case.
    fn or_error(self, name: &'static str) -> Traced<T>;

    /// Attaches an error-level message to the `Output` case.
    fn and_error(self, name: &'static str) -> Traced<T>;

    /// Attaches a warning message to the `Residual` case.
    fn or_warn(self, name: &'static str) -> Traced<T>;

    /// Attaches a warning message to the `Output` case.
    fn and_warn(self, name: &'static str) -> Traced<T>;

    /// Attaches an info message to the `Residual` case.
    fn or_info(self, name: &'static str) -> Traced<T>;

    /// Attaches an info message to the `Output` case.
    fn and_info(self, name: &'static str) -> Traced<T>;

    /// Attaches a debug message to the `Residual` case.
    fn or_debug(self, name: &'static str) -> Traced<T>;

    /// Attaches a debug message to the `Output` case.
    fn and_debug(self, name: &'static str) -> Traced<T>;

    /// Attaches a trace-level message to the `Residual` case.
    fn or_trace(self, name: &'static str) -> Traced<T>;

    /// Attaches a trace-level message to the `Output` case.
    fn and_trace(self, name: &'static str) -> Traced<T>;
}

impl<T: Try> Trace<T> for T {
    fn or_error(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnResidual(Event {
                level: Level::ERROR,
                message: name,
            }),
        }
    }

    fn and_error(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnOutput(Event {
                level: Level::ERROR,
                message: name,
            }),
        }
    }

    fn or_warn(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnResidual(Event {
                level: Level::WARN,
                message: name,
            }),
        }
    }

    fn and_warn(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnOutput(Event {
                level: Level::WARN,
                message: name,
            }),
        }
    }

    fn or_info(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnResidual(Event {
                level: Level::INFO,
                message: name,
            }),
        }
    }

    fn and_info(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnOutput(Event {
                level: Level::INFO,
                message: name,
            }),
        }
    }

    fn or_debug(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnResidual(Event {
                level: Level::DEBUG,
                message: name,
            }),
        }
    }

    fn and_debug(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnOutput(Event {
                level: Level::DEBUG,
                message: name,
            }),
        }
    }

    fn or_trace(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnResidual(Event {
                level: Level::TRACE,
                message: name,
            }),
        }
    }

    fn and_trace(self, name: &'static str) -> Traced<T> {
        Traced {
            inner: self,
            event: TracingConfig::OnOutput(Event {
                level: Level::TRACE,
                message: name,
            }),
        }
    }
}

/// The wrapper around a `Try`-type with associated tracing event. You will usually not
/// interact directly with this type but rather with the trait [`Trace`].
#[must_use = "The wrapped type may be an `Err`, `None`, or other `Residual` which requires handling."]
pub struct Traced<T>
where
    T: Try,
{
    inner: T,
    event: TracingConfig,
}

/// Calling any of the extraction functions *will emit* the enclosed tracing entries related
/// to the relevant variant.
impl<T, O> Extract<O> for Traced<T> where T: Try<Output = O> {}

/// Calling any of the transformation functions *will emit* the enclosed tracing entries related
/// to the relevant variant.
impl<T, O> Transform<O> for Traced<T> where T: Try<Output = O> {}

/// # Note
///
/// - Any attached events are emitted when `branch` is called and all wrappings are
///   removed, leaving either the `Output` or `Resdiual` of the wrapped `Try`-type.
///   For example: using `?` on a `Traced<Traced<Option<T>>>` will result in either a
///   `T` or `None`.
/// - `Traced<T>` does not implement its own `Output` or `Residual` types but immediately
///   unwraps to the `Output` / `Residual` of the wrapped `Try`-type.
impl<T: Try> Try for Traced<T> {
    type Output = T::Output;

    type Residual = T::Residual;

    fn from_output(output: Self::Output) -> Self {
        Self {
            inner: Try::from_output(output),
            event: Default::default(),
        }
    }

    /// Used in `?` to decide whether the operator should produce a value
    /// (because this returned [`ControlFlow::Continue`])
    /// or propagate a value back to the caller
    /// (because this returned [`ControlFlow::Break`]).
    ///
    /// # Note
    ///
    /// Any attached events are emitted when `branch` is called and all wrappings are
    /// removed, leaving either the `Output` or `Resdiual` of the wrapped `Try`-type.
    /// For example: using `?` on a `Traced<Traced<Option<T>>>` will result in either a
    /// `T` or `None`.
    fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
        match self.inner.branch() {
            ControlFlow::Continue(output) => {
                self.event.emit_output();
                ControlFlow::Continue(output)
            }
            ControlFlow::Break(residual) => {
                self.event.emit_residual();
                ControlFlow::Break(residual)
            }
        }
    }
}

/// # Note
///
/// - The `Traced<T>` returned from ` try { Traced<T>? } ` will have no attached tracing
///   events, as all will be emitted by the `?` inside the `try` block
/// - `FromResidual` is primarily implemented as a formality, normal usage will likely be
///   in the form `try bikeshed T { Traced<T>? }`
impl<T: Try> FromResidual for Traced<T> {
    fn from_residual(residual: <Self as Try>::Residual) -> Self {
        Self {
            inner: FromResidual::from_residual(residual),
            event: Default::default(),
        }
    }
}

pub type TracedResult<T, E> = Traced<Result<T, E>>;
pub type TracedOption<T> = Traced<Option<T>>;

impl<T, E> TracedResult<T, E> {
    /// Converts from `TracedResult<T, E>` to `Option<E>`, emitting any tracing entry in the process.
    pub fn err(self) -> Option<E> {
        match self.branch() {
            ControlFlow::Continue(_) => None,
            ControlFlow::Break(Result::Err(err)) => Some(err),
        }
    }

    /// Converts from `TracedResult<T, E>` to `Option<T>`, emitting any tracing entry in the process.
    pub fn ok(self) -> Option<T> {
        self.output()
    }
}

/// Configuration for tracing log level and message.
#[derive(Debug, Clone, Copy)]
struct Event {
    /// The tracing level to use when logging.
    level: Level,
    /// The message to log.
    message: &'static str,
}

impl Event {
    fn emit(&self) {
        match self.level {
            Level::ERROR => tracing::error!("{}", self.message),
            Level::WARN => tracing::warn!("{}", self.message),
            Level::INFO => tracing::info!("{}", self.message),
            Level::DEBUG => tracing::debug!("{}", self.message),
            Level::TRACE => tracing::trace!("{}", self.message),
        }
    }
}

#[derive(Debug, Clone, Copy, Default)]
enum TracingConfig {
    OnOutput(Event),
    OnResidual(Event),
    #[default]
    None,
}

impl TracingConfig {
    fn emit_output(&self) {
        if let OnOutput(event) = self {
            event.emit();
        }
    }

    fn emit_residual(&self) {
        if let OnResidual(event) = self {
            event.emit();
        }
    }
}

#[cfg(test)]
mod tests {
    use std::io;

    use tracing_test::traced_test;

    use super::*;

    /// Tests that `or_error` on an Ok result does not log.
    #[traced_test]
    #[test]
    fn or_error_ok() {
        fn no_error() -> io::Result<()> {
            io::Result::Ok(()).or_error("should not log")?;
            Ok(())
        }

        assert!(no_error().is_ok());
        assert!(!logs_contain("should not log"));
    }

    /// Tests that `or_error` on an Err result logs at ERROR level.
    #[traced_test]
    #[test]
    fn or_error_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).or_error("error message")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(logs_contain("error message"));
    }

    /// Tests that `and_error` on an Ok result logs at ERROR level.
    #[traced_test]
    #[test]
    fn and_error_ok() {
        fn ok() -> io::Result<()> {
            io::Result::Ok(()).and_error("error on ok")?;
            Ok(())
        }

        assert!(ok().is_ok());
        assert!(logs_contain("error on ok"));
    }

    /// Tests that `and_error` on an Err result does not log.
    #[traced_test]
    #[test]
    fn and_error_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).and_error("should not log")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(!logs_contain("should not log"));
    }

    /// Tests that `or_warn` on an Ok result does not log.
    #[traced_test]
    #[test]
    fn or_warn_ok() {
        fn no_error() -> io::Result<()> {
            io::Result::Ok(()).or_warn("stuff")?;
            Ok(())
        }

        assert!(no_error().is_ok());
        assert!(!logs_contain("stuff"));
    }

    /// Tests that `or_warn` on an Err result logs the warning message.
    #[traced_test]
    #[test]
    fn or_warn_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).or_warn("stuff")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(!logs_contain("name"));
        assert!(logs_contain("stuff"));
    }

    /// Tests that `and_warn` on an Ok result logs the warning message.
    #[traced_test]
    #[test]
    fn and_warn_ok() {
        fn ok() -> io::Result<()> {
            io::Result::Ok(()).and_warn("ok warn")?;
            Ok(())
        }

        assert!(ok().is_ok());
        assert!(logs_contain("ok warn"));
    }

    /// Tests that `and_warn` on an Err result does not log.
    #[traced_test]
    #[test]
    fn and_warn_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).and_warn("should not log")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(!logs_contain("should not log"));
    }

    /// Tests that `or_debug` on an Ok result does not log.
    #[traced_test]
    #[test]
    fn or_debug_ok() {
        fn no_error() -> io::Result<()> {
            io::Result::Ok(()).or_debug("should not log")?;
            Ok(())
        }

        assert!(no_error().is_ok());
        assert!(!logs_contain("should not log"));
    }

    /// Tests that `or_debug` on an Err result logs at DEBUG level.
    #[traced_test]
    #[test]
    fn or_debug_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).or_debug("debug message")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(logs_contain("debug message"));
    }

    /// Tests that `and_debug` on an Ok result logs at DEBUG level.
    #[traced_test]
    #[test]
    fn and_debug_ok() {
        fn ok() -> io::Result<()> {
            io::Result::Ok(()).and_debug("debug on ok")?;
            Ok(())
        }

        assert!(ok().is_ok());
        assert!(logs_contain("debug on ok"));
    }

    /// Tests that `and_debug` on an Err result does not log.
    #[traced_test]
    #[test]
    fn and_debug_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).and_debug("should not log")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(!logs_contain("should not log"));
    }

    /// Tests that `or_trace` on an Ok result does not log.
    #[traced_test]
    #[test]
    fn or_trace_ok() {
        fn no_error() -> io::Result<()> {
            io::Result::Ok(()).or_trace("should not log")?;
            Ok(())
        }

        assert!(no_error().is_ok());
        assert!(!logs_contain("should not log"));
    }

    /// Tests that `or_trace` on an Err result logs at TRACE level.
    #[traced_test]
    #[test]
    fn or_trace_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).or_trace("trace message")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(logs_contain("trace message"));
    }

    /// Tests that `and_trace` on an Ok result logs at TRACE level.
    #[traced_test]
    #[test]
    fn and_trace_ok() {
        fn ok() -> io::Result<()> {
            io::Result::Ok(()).and_trace("trace on ok")?;
            Ok(())
        }

        assert!(ok().is_ok());
        assert!(logs_contain("trace on ok"));
    }

    /// Tests that `and_trace` on an Err result does not log.
    #[traced_test]
    #[test]
    fn and_trace_err() {
        fn err() -> io::Result<()> {
            Err(io::Error::other("oops")).and_trace("should not log")?;
            Ok(())
        }

        assert!(err().is_err());
        assert!(!logs_contain("should not log"));
    }

    /// Tests that `ok` on an Ok(v) with no log entry returns Some(v) and does not emit.
    #[traced_test]
    #[test]
    fn ok_or_ok() {
        let good = io::Result::Ok(5).or_warn("should not log");
        let v = good.ok();
        assert_eq!(v, Some(5));
        assert!(!logs_contain("should not log"));
    }

    #[traced_test]
    #[test]
    fn ok_and_ok() {
        let good = io::Result::Ok(5).and_warn("should log");
        let v = good.ok();
        assert_eq!(v, Some(5));
        assert!(logs_contain("should log"));
    }

    #[traced_test]
    #[test]
    fn err_or_err() {
        let err: TracedResult<i32, _> = Err(io::Error::other("oops")).or_warn("should log");
        let v = err.err();
        assert_eq!(v.map(|e| e.to_string()), Some("oops".to_string()));
        assert!(logs_contain("should log"));
    }

    #[traced_test]
    #[test]
    fn err_and_err() {
        let err: TracedResult<i32, _> = Err(io::Error::other("oops")).and_warn("should not log");
        let v = err.err();
        assert_eq!(v.map(|e| e.to_string()), Some("oops".to_string()));
        assert!(!logs_contain("should not log"));
    }

    #[traced_test]
    #[test]
    fn err_conversion() {
        fn convert() -> Result<(), u32> {
            let err: Result<(), u16> = Err(5);
            err.or_error("conversion")?;
            Ok(())
        }
        assert!(convert().is_err());
        assert!(logs_contain("conversion"));
    }

    #[traced_test]
    #[test]
    fn option() {
        fn opt() -> Option<usize> {
            let n = Some(5).or_warn("wibble")?;
            Some(n)
        }

        assert_eq!(opt(), Some(5));
        assert!(!logs_contain("wibble"));
    }

    #[traced_test]
    #[test]
    fn none_and_then() {
        let _: Option<usize> = Option::<usize>::None
            .or_warn("wibble")
            .and_then(|x| Some(x + 1));
        assert!(logs_contain("wibble"));
    }

    #[traced_test]
    #[test]
    fn some_and_then() {
        let n = Some(4).or_warn("wibble").and_then(|x| Some(x + 1));
        assert_eq!(n, Some(5));
        assert!(!logs_contain("wibble"));
    }

    #[traced_test]
    #[test]
    fn chain() {
        fn chained() -> Option<usize> {
            let n = Some(4).or_warn("empty").and_debug("full")?;
            Some(n + 1)
        }
        assert_eq!(chained(), Some(5));
        assert!(logs_contain("full"));
        assert!(!logs_contain("empty"));
    }
}