Skip to main content

file_identify/
tags.rs

1use std::collections::HashSet;
2use std::sync::LazyLock;
3
4pub const DIRECTORY: &str = "directory";
5pub const SYMLINK: &str = "symlink";
6pub const SOCKET: &str = "socket";
7pub const FILE: &str = "file";
8pub const EXECUTABLE: &str = "executable";
9pub const NON_EXECUTABLE: &str = "non-executable";
10pub const TEXT: &str = "text";
11pub const BINARY: &str = "binary";
12
13pub type TagSet = HashSet<&'static str>;
14
15/// Helper function to convert a static array of tags to a TagSet.
16#[inline]
17pub fn tags_from_array(tags: &[&'static str]) -> TagSet {
18    tags.iter().copied().collect()
19}
20
21pub static TYPE_TAGS: LazyLock<TagSet> =
22    LazyLock::new(|| HashSet::from([DIRECTORY, FILE, SYMLINK, SOCKET]));
23pub static MODE_TAGS: LazyLock<TagSet> =
24    LazyLock::new(|| HashSet::from([EXECUTABLE, NON_EXECUTABLE]));
25pub static ENCODING_TAGS: LazyLock<TagSet> = LazyLock::new(|| HashSet::from([BINARY, TEXT]));
26
27/// Check if a tag is a file type tag (optimized with pattern matching)
28pub fn is_type_tag(tag: &str) -> bool {
29    matches!(tag, DIRECTORY | FILE | SYMLINK | SOCKET)
30}
31
32/// Check if a tag is a file mode tag (optimized with pattern matching)  
33pub fn is_mode_tag(tag: &str) -> bool {
34    matches!(tag, EXECUTABLE | NON_EXECUTABLE)
35}
36
37/// Check if a tag is an encoding tag (optimized with pattern matching)
38pub fn is_encoding_tag(tag: &str) -> bool {
39    matches!(tag, BINARY | TEXT)
40}
41
42/// The kind of filesystem entry.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub enum FileKind {
45    Regular,
46    Directory,
47    Symlink,
48    Socket,
49}