Crate crossplatform_path

Crate crossplatform_path 

Source
Expand description

§crossplatform_path

Crossplatform Path Rust library
version: 2.0.1 date: 2025-09-28 author: bestia.dev repository: GitHub

maintained ready-for-use rustlang

License crates.io Documentation Rust crossplatform_path

Lines in Rust code Lines in Doc comments Lines in Comments Lines in examples Lines in tests

Hashtags: #maintained #work-in-progress #rustlang
My projects on GitHub are more like a tutorial than a finished product: bestia-dev tutorials.

§Motivation

I have a few Rust projects that are compiled for Linux and Windows. I need to save some paths inside a config file. Windows have a strange way to work with file/folder paths. I need a library to work in a neutral crossplatform way. Only at the last line of code I transform the neutral path into something that the current OS recognizes.

There exist already some libraries for that: relative-path, typed-path, x-path, camino,…

But it is fun to make something new and simple and very very opinionated.

§Opinionated to the max

My opinions are probably not useful for all developers, but they work for me and probably for most.

  1. The path will be strictly utf8. I know that there exists a rare possibility of the path being in some other strange format, but I will never encounter that with this library. Or at least, I will always avoid that.

  2. Unix and Linux have paths that look nice. Windows is the problem here. The crossplatform format will be very much like the Linux paths.

  3. The only path separator will be the universal ‘/’. If some ‘\’ exists, it will be replaced by ‘/’. Linux allows the use of ‘\’ inside a filename, but my opinion is that this is bad and should be avoided.

  4. Linux is very permissive. Only NULL and ‘/’ are forbidden in path components. But it is a good idea to not allow special characters forbidden on Windows:

    < > : " / \\ | ? *
    0 (NULL byte)
    0-31 (ASCII control characters)  
  5. Filenames cannot end in a space or dot.

  6. Not allow reserved filenames even with extensions and foldernames:
    CON, PRN, AUX, NUL
    COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9
    LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9
    These names are not really needed and will not be allowed:
    . (special name referring to current directory)
    This have to be avoided because of traversal attacks:
    .. (special name referring to parent directory)

  7. Instead of the problematic Windows ‘c:’ or ‘d:’ drives,
    the neutral crossplatform format will be ‘/mnt/c’ or ‘/mnt/d’
    From Windows:
    c:\ will be transformed into /mnt/c/
    d:\ will be transformed into /mnt/d/

  8. This special symbols and root folders are allowed and will be transformed for Windows:
    ‘~’ will be transformed into %UserProfile%
    /tmp will be transformed into %TEMP%

  9. Definitely some paths in one OS have absolutely no meaning in other OS, but these have to be avoided manually.

§Usage

// cargo add crossplatform_path

let cross_path = crossplatform_path::CrossPathBuf::new(r#"tmp\folder_1"#)?.join_relative(r#"file_1.txt"#)?;
println!("{cross_path}");

let linux_path_buf = cross_path.to_path_buf_nix();
println!("linux: {:?}", linux_path_buf);

let win_path_buf = cross_path.to_path_buf_win();
println!("windows: {:?}", win_path_buf);

let current_os_path_buf = cross_path.to_path_buf_current_os();
println!("current_os: {:?}", current_os_path_buf);

println!("exists: {}", cross_path.exists());
println!("is_dir: {}", cross_path.is_dir());
println!("is_file: {}", cross_path.is_file());

println!("parent: {}", cross_path.parent()?);
println!("file_name: {}", cross_path.file_name()?);
println!("file_stem: {}", cross_path.file_stem()?);
println!("extension: {}", cross_path.extension()?);

println!("write_str_to_file");
cross_path.write_str_to_file("content for testing")?;

let content = cross_path.read_to_string()?;
println!("read_to_string: {content}");


let cross_path = cross_path.add_start_slash()?.add_end_slash()?;
println!("add slashes {}", cross_path);

let cross_path = cross_path.trim_start_slash()?.trim_end_slash()?;
println!("trim slashes {}", cross_path);
    

§Development details

Read the development details in a separate md file: DEVELOPMENT.md

§Releases changelog

Read the releases changelog in a separate md file: RELEASES.md

§TODO

And code happily ever after…

§Open-source and free as a beer

My open-source projects are free as a beer (MIT license).
I just love programming.
But I need also to drink. If you find my projects and tutorials helpful, please buy me a beer by donating to my PayPal.
You know the price of a beer in your local bar ;-)
So I can drink a free beer for your health :-)
Na zdravje! Alla salute! Prost! Nazdravlje! 🍻

//bestia.dev
//github.com/bestia-dev
//bestiadev.substack.com
//youtube.com/@bestia-dev-tutorials

Structs§

CrossPathBuf
CrossPathBuf stores Path in a Neutral Crossplatform format. \

Enums§

Error
All possible library errors for thiserror.

Type Aliases§

Result
crossplatform_path::Result