amber/
util.rs

1use crate::console::Console;
2use std::fs::File;
3use std::io::{BufReader, Error, ErrorKind, Read};
4use std::path::PathBuf;
5use std::process;
6use std::time::{Duration, Instant};
7
8// ---------------------------------------------------------------------------------------------------------------------
9// Utility
10// ---------------------------------------------------------------------------------------------------------------------
11
12#[cfg(feature = "statistics")]
13macro_rules! watch_time (
14    ( $total:expr, $func:block ) => (
15        {
16            let beg = Instant::now();
17            $func;
18            $total += beg.elapsed();
19        }
20    );
21);
22
23#[cfg(not(feature = "statistics"))]
24macro_rules! watch_time (
25    ( $total:expr, $func:block ) => (
26        {
27            $func;
28        }
29    );
30);
31
32pub fn watch_time<F>(closure: F) -> Duration
33where
34    F: FnOnce(),
35{
36    let start = Instant::now();
37    closure();
38    start.elapsed()
39}
40
41pub fn catch<F, T, E>(closure: F) -> Result<T, E>
42where
43    F: FnOnce() -> Result<T, E>,
44{
45    closure()
46}
47
48pub fn as_secsf64(dur: Duration) -> f64 {
49    (dur.as_secs() as f64) + (dur.subsec_nanos() as f64 / 1_000_000_000.0)
50}
51
52pub fn read_from_file(path: &str) -> Result<Vec<u8>, Error> {
53    let file = match File::open(path) {
54        Ok(x) => x,
55        Err(e) => return Err(e),
56    };
57    let mut reader = BufReader::new(file);
58    let mut ret: String = String::new();
59    let _ = reader.read_to_string(&mut ret);
60    Ok(ret.into_bytes())
61}
62
63pub fn decode_error(e: ErrorKind) -> &'static str {
64    match e {
65        ErrorKind::NotFound => "file not found",
66        ErrorKind::PermissionDenied => "permission denied",
67        ErrorKind::ConnectionRefused => "connection refused",
68        ErrorKind::ConnectionReset => "connection reset",
69        ErrorKind::ConnectionAborted => "connection aborted",
70        ErrorKind::NotConnected => "not connected",
71        ErrorKind::AddrInUse => "address is in use",
72        ErrorKind::AddrNotAvailable => "address is not available",
73        ErrorKind::BrokenPipe => "broken pipe",
74        ErrorKind::AlreadyExists => "file is already exists",
75        ErrorKind::WouldBlock => "world be blocked",
76        ErrorKind::InvalidInput => "invalid parameter",
77        ErrorKind::InvalidData => "invalid data",
78        ErrorKind::TimedOut => "operation timeout",
79        ErrorKind::WriteZero => "write size is zero",
80        ErrorKind::Interrupted => "interrupted",
81        ErrorKind::Other => "unknown",
82        _ => "unknown",
83    }
84}
85
86pub enum PipelineInfo<T> {
87    Beg(usize),
88    Ok(T),
89    Info(String),
90    Err(String),
91    Time(Duration, Duration),
92    End(usize),
93}
94
95pub fn exit(code: i32, console: &mut Console) -> ! {
96    console.reset();
97    console.flush();
98    process::exit(code);
99}
100
101pub fn handle_escape(text: &str) -> String {
102    let text = text.replace("\\n", "\n");
103    let text = text.replace("\\r", "\r");
104    let text = text.replace("\\t", "\t");
105    let text = text.replace("\\\\", "\\");
106    text
107}
108
109pub fn get_config(name: &str) -> Option<PathBuf> {
110    let dot_cfg_path = directories::BaseDirs::new()
111        .map(|base| base.home_dir().join(&format!(".{}", name)))
112        .filter(|path| path.exists());
113    let app_cfg_path = directories::ProjectDirs::from("com.github", "dalance", "amber")
114        .map(|proj| proj.preference_dir().join(name))
115        .filter(|path| path.exists());
116    let xdg_cfg_path = directories::BaseDirs::new()
117        .map(|base| base.home_dir().join(".config").join("amber").join(name))
118        .filter(|path| path.exists());
119    let etc_path = PathBuf::from(format!("/etc/amber/{}", name));
120    let etc_cfg_path = etc_path.exists().then_some(etc_path);
121    dot_cfg_path.or(app_cfg_path).or(xdg_cfg_path).or(etc_cfg_path)
122}
123
124#[cfg(not(windows))]
125pub fn set_c_lflag(c_lflag: Option<termios::tcflag_t>) {
126    if let Ok(mut termios) = termios::Termios::from_fd(0) {
127        if let Some(c_lflag) = c_lflag {
128            termios.c_lflag = c_lflag;
129            let _ = termios::tcsetattr(0, termios::TCSADRAIN, &termios);
130        }
131    }
132}
133
134#[cfg(not(windows))]
135pub fn get_c_lflag() -> Option<termios::tcflag_t> {
136    if let Ok(termios) = termios::Termios::from_fd(0) {
137        Some(termios.c_lflag)
138    } else {
139        None
140    }
141}