Skip to main content

linguist/
error.rs

1use std::fmt;
2
3/// Errors that can occur during language detection
4#[derive(Debug, Clone, PartialEq)]
5pub enum LinguistError {
6    /// The provided path is invalid for whatever reason
7    InvalidPath(String),
8
9    /// A regex pattern in the heuristics is malformed
10    InvalidRegex { pattern: String, error: String },
11
12    /// A named pattern referenced in heuristics doesn't exist
13    MissingNamedPattern(String),
14}
15
16impl fmt::Display for LinguistError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            LinguistError::InvalidPath(path) => {
20                write!(f, "Invalid path: {path}")
21            }
22            LinguistError::InvalidRegex { pattern, error } => {
23                write!(f, "Invalid regex pattern '{pattern}': {error}")
24            }
25            LinguistError::MissingNamedPattern(name) => {
26                write!(f, "Named pattern '{name}' not found in heuristics")
27            }
28        }
29    }
30}
31
32impl std::error::Error for LinguistError {}