Skip to main content

xuko_misc/
lib.rs

1//! xuko-misc provide miscellaneous utilities
2
3/// Colors
4pub mod color;
5
6/// Profiling
7pub mod profiling;
8
9/// Path module
10///
11/// Provides utilities for [`Path`]s
12pub mod path {
13
14    use std::path::{Path, PathBuf};
15
16    /// Search for file or directory `stub` in `search_locations`
17    pub fn search_for_path(search_locations: &[&Path], stub: &str) -> Option<PathBuf> {
18        for location in search_locations {
19            let p = Path::new(location).join(stub);
20
21            match p.try_exists() {
22                Ok(true) => return Some(p),
23                _ => continue,
24            }
25        }
26
27        None
28    }
29}
30
31/// C for-loop
32///
33/// # Examples
34///
35/// ```
36/// use xuko_misc::cfor;
37/// cfor!(for (let mut x = 0; x != 5; x += 1) {
38///     println!("Hello! #{}", x + 1);
39/// });
40/// ```
41#[macro_export]
42macro_rules! cfor {
43    (for ($init:stmt;$cond:expr;$inc:stmt) $body:tt) => {
44        $init
45        while $cond {
46            $body
47            $inc
48        }
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn cforloop() {
58        cfor!(for (let mut x = 0; x != 5; x += 1) {
59            println!("X is: {}", x + 1);
60        });
61    }
62}