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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! A module for misc. functions.
pub mod file {
extern crate walkdir;
use self::walkdir::{WalkDir, DirEntry};
use std::env;
use std::path::PathBuf;
/// Takes a filestem and searches for the file in the current directory.
/// returns the directory that the file is located in.
pub fn locate_file(module: &str) -> Option<PathBuf> {
let cwd = env::current_dir().unwrap();
for entry in WalkDir::new(cwd.as_path())
.into_iter()
.filter_entry(|e| !is_hidden(e)) {
let entry = match entry {
Ok(e) => e,
Err(_) => continue,
};
let is_file = entry.metadata()
.ok()
.map_or(false, |m| m.is_file());
if is_file {
let stem = entry.path().file_stem();
let stems_match = stem.map_or(false, |s| s == module);
if stems_match {
let mut buf = entry.path().to_path_buf();
buf.set_file_name("");
return Some(buf)
}
}
}
None
}
pub fn is_hidden(entry: &DirEntry) -> bool {
entry.file_name()
.to_str()
.map(|s| s.starts_with('.'))
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_locate_file() {
let result = locate_file("main");
assert!(result.is_some());
}
}
}
#[macro_export]
macro_rules! vprintln {
($($arg:tt)*) => {{
if ::utils::print::is_verbose() {
println!($($arg)*);
}
}}
}
#[allow(dead_code)]
pub mod print {
use std::fmt;
/// Function version of the vprintln macro. Does not support formatting
/// (use format! within it).
pub fn vprint(input: &str) {
if is_verbose() {
println!("{}", input);
}
}
pub fn is_verbose() -> bool {
unsafe {
::VERBOSE
}
}
macro_rules! define_colors {
($($(#[$attrs:meta])* pub struct $s:ident => $color:expr;)*) => {
$(
$(#[$attrs])*
pub struct $s<T>(pub T);
impl<T: fmt::Display> fmt::Display for $s<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\x1b[{}m{}\x1b[0m", $color, self.0)
}
}
impl<T: fmt::Debug> fmt::Debug for $s<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\x1b[{}m{:?}\x1b[0m", $color, self.0)
}
}
)*
}
}
define_colors! {
/// Write a `Display` or `Debug` escaped with Red
pub struct Red => "31";
/// Write a `Display` or `Debug` escaped with Green
pub struct Green => "32";
/// Write a `Display` or `Debug` escaped with Yellow
pub struct Yellow => "33";
}
}