Skip to main content

reovim_module_codec_utf8/
classifier.rs

1//! UTF-8 content classifier.
2//!
3//! Lowest priority (10) — acts as the fallback classifier when no
4//! higher-priority classifier recognizes the content.
5
6use reovim_driver_codec::{ContentClassifier, ContentType};
7
8/// UTF-8 content classifier.
9///
10/// Returns `Some(ContentType("text/utf-8"))` if the raw bytes are valid
11/// UTF-8, `None` otherwise. Priority 10 (lowest) — this is the fallback
12/// when no other classifier matches.
13pub struct Utf8Classifier;
14
15impl Utf8Classifier {
16    /// Create a new UTF-8 classifier.
17    #[must_use]
18    pub const fn new() -> Self {
19        Self
20    }
21}
22
23#[cfg_attr(coverage_nightly, coverage(off))]
24impl Default for Utf8Classifier {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl ContentClassifier for Utf8Classifier {
31    fn classify(&self, raw: &[u8], _path: &str) -> Option<ContentType> {
32        if std::str::from_utf8(raw).is_ok() {
33            Some(ContentType::new(ContentType::UTF8))
34        } else {
35            None
36        }
37    }
38
39    fn priority(&self) -> u8 {
40        10
41    }
42
43    fn name(&self) -> &'static str {
44        "utf-8"
45    }
46}
47
48#[cfg(test)]
49#[path = "classifier_tests.rs"]
50mod tests;