#[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.
Binary
The file does not look like text.
Fields
§
kind: BinaryKindThe kind of binary; BinaryKind::Unknown if no magic number
identified it.
SourceText(SourceText)
The file looks like text.
Implementations§
Source§impl FileClass
impl FileClass
Sourcepub fn is_source_text(&self) -> bool
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§
Auto Trait Implementations§
impl Forget for FileClass
impl Freeze for FileClass
impl Move for FileClass
impl RefUnwindSafe for FileClass
impl Send for FileClass
impl Sync for FileClass
impl Unpin for FileClass
impl UnsafeUnpin for FileClass
impl UnwindSafe for FileClass
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more