1#[cfg(any(test, feature = "test-support"))]
2use std::time::Duration;
3
4#[cfg(any(test, feature = "test-support"))]
5use futures::Future;
6
7#[cfg(any(test, feature = "test-support"))]
8use smol::future::FutureExt;
9
10pub use util::*;
11
12/// A helper trait for building complex objects with imperative conditionals in a fluent style.
13pub trait FluentBuilder {
14 /// Imperatively modify self with the given closure.
15 fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
16 where
17 Self: Sized,
18 {
19 f(self)
20 }
21
22 /// Conditionally modify self with the given closure.
23 fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
24 where
25 Self: Sized,
26 {
27 self.map(|this| if condition { then(this) } else { this })
28 }
29
30 /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
31 fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
32 where
33 Self: Sized,
34 {
35 self.map(|this| {
36 if let Some(value) = option {
37 then(this, value)
38 } else {
39 this
40 }
41 })
42 }
43 /// Conditionally unwrap and modify self with the given closure, if the given option is None.
44 fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self
45 where
46 Self: Sized,
47 {
48 self.map(|this| {
49 if let Some(_) = option {
50 this
51 } else {
52 then(this)
53 }
54 })
55 }
56}
57
58#[cfg(any(test, feature = "test-support"))]
59pub async fn timeout<F, T>(timeout: Duration, f: F) -> Result<T, ()>
60where
61 F: Future<Output = T>,
62{
63 let timer = async {
64 smol::Timer::after(timeout).await;
65 Err(())
66 };
67 let future = async move { Ok(f.await) };
68 timer.race(future).await
69}
70
71#[cfg(any(test, feature = "test-support"))]
72pub struct CwdBacktrace<'a>(pub &'a backtrace::Backtrace);
73
74#[cfg(any(test, feature = "test-support"))]
75impl std::fmt::Debug for CwdBacktrace<'_> {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 use backtrace::{BacktraceFmt, BytesOrWideString};
78
79 let cwd = std::env::current_dir().unwrap();
80 let cwd = cwd.parent().unwrap();
81 let mut print_path = |fmt: &mut std::fmt::Formatter<'_>, path: BytesOrWideString<'_>| {
82 std::fmt::Display::fmt(&path, fmt)
83 };
84 let mut fmt = BacktraceFmt::new(f, backtrace::PrintFmt::Full, &mut print_path);
85 for frame in self.0.frames() {
86 let mut formatted_frame = fmt.frame();
87 if frame
88 .symbols()
89 .iter()
90 .any(|s| s.filename().map_or(false, |f| f.starts_with(cwd)))
91 {
92 formatted_frame.backtrace_frame(frame)?;
93 }
94 }
95 fmt.finish()
96 }
97}