Skip to main content

FileClass

Enum FileClass 

Source
#[non_exhaustive]
pub enum FileClass { Empty, Oversize { size: u64, limit: u64, }, Binary { kind: BinaryKind, mime: Option<&'static str>, }, SourceText(SourceText), }
Expand description

What classify thinks a file is. See the module docs for why this is only a hint.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Empty

The file is zero bytes long.

§

Oversize

The file looks like text but exceeds the caller’s size limit. Binary files are reported as FileClass::Binary regardless of size.

Fields

§size: u64

The file’s size in bytes.

§limit: u64

The caller-supplied limit it exceeded.

§

Binary

The file does not look like text.

Fields

§kind: BinaryKind

The kind of binary; BinaryKind::Unknown if no magic number identified it.

§mime: Option<&'static str>

MIME type from the magic-number match (e.g. "application/zip"), for reporting; None when only the byte distribution decided.

§

SourceText(SourceText)

The file looks like text.

Implementations§

Source§

impl FileClass

Source

pub fn is_source_text(&self) -> bool

true for FileClass::SourceText — the only class worth parsing under the most conservative policy.

Examples found in repository?
examples/classify_calibrate.rs (line 179)
132fn scan(root: &Path, expect_binary: bool, show_failures: bool) -> Tally {
133    let limits = ClassifyLimits::default();
134    let mut t = Tally::default();
135    for path in list_files(root) {
136        let Some((prefix, size)) = read_prefix(&path) else {
137            t.unreadable += 1;
138            continue;
139        };
140        t.files += 1;
141        let is_c = path
142            .extension()
143            .and_then(|e| e.to_str())
144            .is_some_and(|e| C_FAMILY.contains(&e.to_ascii_lowercase().as_str()));
145        let class = classify(&path, &prefix, size, &limits);
146        let entry = t.classes.entry(class_name(&class)).or_default();
147        if is_c {
148            entry.0 += 1;
149        } else {
150            entry.1 += 1;
151        }
152
153        let failed = if expect_binary {
154            !matches!(class, FileClass::Binary { .. })
155        } else {
156            is_c && matches!(class, FileClass::Binary { .. })
157        };
158        if failed {
159            t.gate_failures += 1;
160            if show_failures {
161                eprintln!("GATE FAILURE: {} -> {class:?}", path.display());
162            }
163        }
164
165        if !expect_binary && matches!(class, FileClass::Binary { .. }) {
166            let ext = path.extension().map_or_else(
167                || "(none)".into(),
168                |e| e.to_string_lossy().to_ascii_lowercase(),
169            );
170            *t.binary_exts.entry(ext).or_default() += 1;
171            if !is_c && language_info_for_file(&path).is_some() {
172                t.other_source_binary += 1;
173                if show_failures {
174                    eprintln!("NOTE non-C source -> Binary: {}", path.display());
175                }
176            }
177        }
178
179        if is_c && class.is_source_text() {
180            let (len, nul, control, invalid) = __byte_counts(&prefix, size);
181            t.c_text += 1;
182            t.c_text_with_nul += usize::from(nul > 0);
183            t.c_text_with_control += usize::from(control > 0);
184            t.c_text_with_invalid += usize::from(invalid > 0);
185            t.max_nul.see(nul, len);
186            t.max_control.see(control, len);
187            t.max_invalid.see(invalid, len);
188        }
189    }
190    t
191}

Trait Implementations§

Source§

impl Clone for FileClass

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for FileClass

Source§

impl Debug for FileClass

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.