Skip to main content

classify

Function classify 

Source
pub fn classify(meta: Meta, path: impl AsRef<[u8]>) -> Classification
Expand description

The I/O-free part of identify.tags_from_path: everything that follows from the entry’s Meta and name.

path may include directories (only the base name is used); see tags_from_filename for the name rules. Directories, symlinks and sockets are Done with a single type tag. A regular file gets file, executable/non-executable and its name tags; it needs content only if that leaves no text/binary tag.

FileKind::Other (FIFOs, devices) is never read: it is classified like a regular file whose content is unavailable and tagged binary when the name does not decide the encoding. (identify would open it, which blocks forever on a FIFO.)

use file_identify::{Classification, Meta, classify};

// Known extension: no I/O needed at all.
let Classification::Done(tags) = classify(Meta::regular(false), "src/lib.rs") else {
    unreachable!()
};
assert_eq!(tags.to_string(), "file, non-executable, rust, text");

// Unknown name: the first bytes decide text vs binary.
let Classification::NeedsContent(pending) = classify(Meta::regular(false), "data.bin2") else {
    unreachable!()
};
assert!(pending.needs_encoding() && !pending.needs_shebang());
assert_eq!(pending.finish_bytes(b"\0\x01").to_string(), "binary, file, non-executable");