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