summaryrefslogtreecommitdiffstats
path: root/src/prelude.rs
blob: bc48955197392aacec271d71faafae1e609f60e7 (plain) (blame)
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
pub use crate::env::Env;
pub use anyhow::{anyhow, Result};

pub use std::io::{Read as _, Write as _};

pub use futures_util::future::FutureExt as _;
pub use futures_util::stream::StreamExt as _;
pub use futures_util::stream::TryStreamExt as _;
pub use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};

pub use std::os::unix::ffi::{OsStrExt as _, OsStringExt as _};
pub use std::os::unix::io::{AsRawFd as _, FromRawFd as _, IntoRawFd as _};
pub use std::os::unix::process::ExitStatusExt as _;
pub use users::os::unix::UserExt as _;

pub use ext::Result as _;

mod ext {
    pub trait Result {
        type T;
        type E;

        fn allow(self, allow_e: Self::E) -> Self;
        fn allow_with(self, allow_e: Self::E, default_t: Self::T) -> Self;
    }

    impl<T, E> Result for std::result::Result<T, E>
    where
        T: std::default::Default,
        E: std::cmp::PartialEq,
    {
        type T = T;
        type E = E;

        fn allow(self, allow_e: Self::E) -> Self {
            self.or_else(|e| {
                if e == allow_e {
                    Ok(std::default::Default::default())
                } else {
                    Err(e)
                }
            })
        }

        fn allow_with(self, allow_e: Self::E, default_t: Self::T) -> Self {
            self.or_else(
                |e| if e == allow_e { Ok(default_t) } else { Err(e) },
            )
        }
    }
}