Skip to main content

git_semantic/search/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum SearchError {
5    #[error("invalid date format '{value}' — expected YYYY-MM-DD")]
6    InvalidDateFormat {
7        value: String,
8        #[source]
9        source: chrono::ParseError,
10    },
11
12    #[error("no index found for this repository")]
13    IndexNotLoaded,
14
15    #[error(transparent)]
16    Embedding(#[from] crate::embedding::EmbeddingError),
17}
18
19impl SearchError {
20    /// Returns a user-facing hint for how to resolve this error.
21    pub fn hint(&self) -> Option<&'static str> {
22        match self {
23            Self::InvalidDateFormat { .. } => {
24                Some("Use the format YYYY-MM-DD, e.g. --after 2024-01-15")
25            }
26            Self::IndexNotLoaded => Some("Run: git-semantic index"),
27            Self::Embedding(_) => None, // Delegate to EmbeddingError's own hint
28        }
29    }
30
31    /// Returns an error code for programmatic identification.
32    pub fn code(&self) -> &'static str {
33        match self {
34            Self::InvalidDateFormat { .. } => "E4001",
35            Self::IndexNotLoaded => "E4002",
36            Self::Embedding(_) => "E4003",
37        }
38    }
39}