Crate ntfsanitise

Crate ntfsanitise 

Source
Expand description

§ntfsanitise

Unsafe forbidden Latest Version docs.rs MSRV License Coverage Pipeline Status

A tiny crate for sanitising filenames for use on NTFS filesystems.

The set of banned characters are exported as the constant BANNED.

§Usage

// path-related features require the standard library
#[cfg(feature = "std")]
fn main() {
	use ntfsanitise::{sanitise, is_dirty, is_clean, PathExt, PathBufExt};
	use std::path::PathBuf;

	// sanitise strings
	let input = "unsuitable:filename?.txt";
	assert!(is_dirty(input));
	let sanitised = sanitise(input, "_");
	assert_eq!(sanitised, "unsuitable_filename_.txt");
	assert!(is_clean(sanitised));

	// sanitise paths
	let mut path = PathBuf::from("/home/user/<file>.txt");
	assert!(path.is_dirty());
	path.sanitise_filename("_");

	assert!(path.is_clean());
	assert!(path.file_name().is_some_and(|file_name| file_name.to_str() == Some("_file_.txt")));
}

#[cfg(not(feature = "std"))]
fn main() {
	use ntfsanitise::{BANNED, is_clean, is_dirty};

	// no_std support
	assert!(is_dirty("unsuitable:filename.txt"));
	assert!(is_clean("suitable_filename.txt"));
	assert!(BANNED.contains(&'<'));
}

Constants§

BANNED
Banned characters for NTFS filenames.

Traits§

PathBufExt
Extension trait for std::path::PathBuf.
PathExt
Extension trait for std::path::Path.

Functions§

is_clean
Checks whether the given string is “clean” (i.e. does not contain any banned characters).
is_dirty
Checks whether the given string is “dirty” (i.e. contains banned characters).
sanitise
Replaces banned characters with the given replacement.