util.rs

  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 modify self with the given closure.
 31    fn when_else(
 32        self,
 33        condition: bool,
 34        then: impl FnOnce(Self) -> Self,
 35        else_fn: impl FnOnce(Self) -> Self,
 36    ) -> Self
 37    where
 38        Self: Sized,
 39    {
 40        self.map(|this| if condition { then(this) } else { else_fn(this) })
 41    }
 42
 43    /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
 44    fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
 45    where
 46        Self: Sized,
 47    {
 48        self.map(|this| {
 49            if let Some(value) = option {
 50                then(this, value)
 51            } else {
 52                this
 53            }
 54        })
 55    }
 56    /// Conditionally unwrap and modify self with the given closure, if the given option is None.
 57    fn when_none<T>(self, option: &Option<T>, then: impl FnOnce(Self) -> Self) -> Self
 58    where
 59        Self: Sized,
 60    {
 61        self.map(|this| {
 62            if let Some(_) = option {
 63                this
 64            } else {
 65                then(this)
 66            }
 67        })
 68    }
 69}
 70
 71#[cfg(any(test, feature = "test-support"))]
 72pub async fn timeout<F, T>(timeout: Duration, f: F) -> Result<T, ()>
 73where
 74    F: Future<Output = T>,
 75{
 76    let timer = async {
 77        smol::Timer::after(timeout).await;
 78        Err(())
 79    };
 80    let future = async move { Ok(f.await) };
 81    timer.race(future).await
 82}
 83
 84#[cfg(any(test, feature = "test-support"))]
 85pub struct CwdBacktrace<'a>(pub &'a backtrace::Backtrace);
 86
 87#[cfg(any(test, feature = "test-support"))]
 88impl std::fmt::Debug for CwdBacktrace<'_> {
 89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 90        use backtrace::{BacktraceFmt, BytesOrWideString};
 91
 92        let cwd = std::env::current_dir().unwrap();
 93        let cwd = cwd.parent().unwrap();
 94        let mut print_path = |fmt: &mut std::fmt::Formatter<'_>, path: BytesOrWideString<'_>| {
 95            std::fmt::Display::fmt(&path, fmt)
 96        };
 97        let mut fmt = BacktraceFmt::new(f, backtrace::PrintFmt::Full, &mut print_path);
 98        for frame in self.0.frames() {
 99            let mut formatted_frame = fmt.frame();
100            if frame
101                .symbols()
102                .iter()
103                .any(|s| s.filename().map_or(false, |f| f.starts_with(cwd)))
104            {
105                formatted_frame.backtrace_frame(frame)?;
106            }
107        }
108        fmt.finish()
109    }
110}