working_dir

Crates.ioworking_dir
lib.rsworking_dir
version0.1.1
sourcesrc
created_at2022-11-26 08:19:39.593256
updated_at2022-11-26 08:29:35.812643
descriptionProvides a working directory abstraction as local state
homepagehttps://github.com/codeinred/working_dir
repositoryhttps://github.com/codeinred/working_dir.git
max_upload_size
id723169
size20,068
Alecto Irene Perez (codeinred)

documentation

https://docs.rs/working_dir/

README

working_dir: Working Directories as local state

Working directories and relative paths are great, but a working directory is global state. This is no good. Changing the working directory of the whole program is such a hack, and there are plenty of times you want to do something in the context of a working directory other than the one you're currently running in.

This is a small library intended to provide objects that act as a working directory, supporting a full range of file and directory manipulations. It was created as part of a larger project to create a tool to automatically handle large-scale refactors of C and C++ projects.

When refactoring such a project, it's necessary to update both the locations of files, and any #include declarations that pertain to moved files. In such a context, it's useful to think of an inlcude set as a list of as working directories, and the project's root directory can also be thought of as a working directory that files are being moved within.

Here's a minimal example of one way this library might be used in such a context:

use std::path::{Path, PathBuf};

use working_dir::Dir;

fn find_include<P: AsRef<Path>, Q: AsRef<Path>>(include_set: &[Dir<P>], file: Q) -> Option<PathBuf> {
    let file = file.as_ref();

    for include_dir in include_set {
        if include_dir.contains(file) {
            return Some(include_dir / file)
        }
    }
    return None
}


fn main() {
    let file = "stdio.h";
    let include_set = [
        "/usr/local/include",
        "/usr/target/include",
        "/usr/include"
    ].map(Dir);

    if let Some(path) = find_include(&include_set, file) {
        println!("{file} found at {path:?}")
    } else {
        println!("Unable to find {file} in {include_set:?}")
    }
}
Commit count: 18

cargo fmt