Skip to main content

derive_ext

Function derive_ext 

Source
pub fn derive_ext(name: &OsStr) -> Option<String>
Expand description

Extract the compound-tail extension from a file name, lowercased and including the leading dot.

“Compound tail” means archive.tar.gz yields .tar.gz rather than .gz, because the pair is what a human means by the file’s type. Only .tar is folded this way; generalizing to an arbitrary set of compound stems belongs in the rule dialect, not in a hand-maintained list here.

Returns None for names with no usable extension, including dotfiles like .gitignore — a leading dot marks a hidden file, it does not introduce an extension.

This is the raw extension, and it is deliberately not the File Rollup one: any final valid-Unicode component counts without a character or length restriction, so file.c++ is .c++ here while logical_ext and TypeRegistry::canonical_ext give it none. It names the extension view’s piles and the label of an unrecognized type, and it keeps the answer it had before registries existed for the detached and command-line callers that depend on it. An invalid UTF-8 or UTF-16 extension returns None. On Unix and Windows an invalid native stem does not prevent a valid extension from being extracted.

use std::ffi::OsStr;
use fdu_core::classify::derive_ext;

assert_eq!(derive_ext(OsStr::new("archive.tar.gz")).as_deref(), Some(".tar.gz"));
assert_eq!(derive_ext(OsStr::new("notes.MD")).as_deref(), Some(".md"));
assert_eq!(derive_ext(OsStr::new("file.c++")).as_deref(), Some(".c++"));
assert_eq!(derive_ext(OsStr::new(".gitignore")), None);
assert_eq!(derive_ext(OsStr::new("README")), None);