cj-path-util 0.3.0

Utilities to make working with Path/PathBuf more productive
Documentation
use std::{
    ffi::{OsStr, OsString},
    os::unix::{ffi::OsStringExt, prelude::OsStrExt},
    path::{Path, PathBuf},
};

/// Add an extension to a path with a filename. Returns none if the
/// path does not in fact have a filename. `extension` must not include
/// the dot. If `extension` is empty, nothing is appended (not even the
/// dot).
pub fn add_extension<P: AsRef<Path>, S: AsRef<OsStr>>(this: P, extension: S) -> Option<PathBuf> {
    let mut path = this.as_ref().to_owned();
    if !_add_extension_mut(&mut path, extension.as_ref()) {
        None
    } else {
        Some(path)
    }
}

/// Add an extension to a path with a filename. Returns false if it
/// does not in fact have a filename. `extension` must not include the
/// dot. If `extension` is empty, nothing is appended (not even the
/// dot). This function exists because the `add_extension` method in
/// std is currently an unstable library feature.
pub fn add_extension_mut<S: AsRef<OsStr>>(this: &mut PathBuf, extension: S) -> bool {
    _add_extension_mut(this, extension.as_ref())
}

fn _add_extension_mut(this: &mut PathBuf, extension: &OsStr) -> bool {
    let file_name = match this.file_name() {
        None => return false,
        Some(f) => f.as_bytes(),
    };

    let mut new = extension.as_bytes().to_vec();
    if !new.is_empty() {
        // "truncate until right after the file name
        // this is necessary for trimming the trailing slash"

        // Hmm, dunno. This is not going to behave the same, but I'm
        // happy with just appending a dot and the extension, please.

        let mut file_name: Vec<u8> = Vec::from(file_name);
        file_name.push(b'.');
        file_name.append(&mut new);

        // XX this depends on Unix, sigh.
        let file_name = OsString::from_vec(file_name);
        this.set_file_name(file_name);
    }

    true
}

#[test]
fn t_add_extension() {
    let t = |path: &str, ext: &str| {
        let mut path = PathBuf::from(path);
        if add_extension_mut(&mut path, ext) {
            path.to_string_lossy().to_string()
        } else {
            format!("{path:?} -- unchanged")
        }
    };

    assert_eq!(t("hello", ""), "hello");
    assert_eq!(t("hello", "foo"), "hello.foo");
    assert_eq!(t("hello.foo", "bar"), "hello.foo.bar");
    assert_eq!(t("hello", ".foo"), "hello..foo");
    assert_eq!(t("/", ".foo"), "\"/\" -- unchanged");
    assert_eq!(t("hello/", ".foo"), "hello..foo"); // XX oh, buggy. todo fix
}