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