pub enum SearchError {
NoTranslationFiles {
text: String,
searched_paths: String,
},
YamlParseError {
file: PathBuf,
reason: String,
},
JsonParseError {
file: PathBuf,
reason: String,
},
NoCodeReferences {
key: String,
file: PathBuf,
},
Io(Error),
RipgrepExecutionFailed(String),
InvalidUtf8(FromUtf8Error),
InvalidPath(String),
Generic(String),
}Expand description
Custom error type for code search operations.
§Rust Book Reference
Chapter 9.2: Recoverable Errors with Result https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html
This demonstrates defining custom error types using an enum, which allows representing multiple kinds of errors with different associated data.
§Educational Notes
§Why use an enum for errors?
- Each variant can carry different data (strings, paths, numbers)
- Pattern matching ensures all error cases are handled
- Type system prevents mixing up different error kinds
§The #[derive(Debug, Error)] attributes
Debug: Required bystd::error::ErrortraitError: Fromthiserror, auto-implementsstd::error::Error
§The #[error("...")] attribute
- Automatically implements
Displaytrait - Use
{field}to interpolate struct fields - Creates user-friendly error messages
Compare this to the book’s manual Display implementation in Chapter 9.2
to see how thiserror reduces boilerplate.
Variants§
NoTranslationFiles
No translation files found containing the search text
YamlParseError
Failed to parse YAML file
JsonParseError
Failed to parse JSON file
NoCodeReferences
Translation key found but no code references detected
Io(Error)
IO error occurred during file operations.
§Rust Book Reference
Chapter 9.2: Propagating Errors https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#propagating-errors
§Educational Notes - The #[from] Attribute
The #[from] attribute automatically implements:
impl From<std::io::Error> for SearchError {
fn from(err: std::io::Error) -> Self {
SearchError::Io(err)
}
}This enables the ? operator to automatically convert IO errors:
fn read_file(path: &Path) -> Result<String> {
let contents = std::fs::read_to_string(path)?; // IO error auto-converts
Ok(contents)
}Without #[from], you would need manual conversion or .map_err().
Key Point: The ? operator calls From::from automatically,
making error propagation seamless across different error types.
RipgrepExecutionFailed(String)
Failed to execute ripgrep command
InvalidUtf8(FromUtf8Error)
Invalid UTF-8 in ripgrep output
InvalidPath(String)
Failed to parse file path
Generic(String)
Generic search error with context
Implementations§
Source§impl SearchError
impl SearchError
Sourcepub fn no_translation_files(text: impl Into<String>) -> Self
pub fn no_translation_files(text: impl Into<String>) -> Self
Create a NoTranslationFiles error with default searched paths.
§Rust Book Reference
Chapter 10.2: Traits as Parameters https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
§Educational Notes - impl Into<String>
Using impl Into<String> instead of String makes the API more flexible:
// All of these work:
SearchError::no_translation_files("add new"); // &str
SearchError::no_translation_files(String::from("add new")); // String
SearchError::no_translation_files(owned_string); // String (moved)How it works:
&strimplementsInto<String>(converts by allocating)StringimplementsInto<String>(converts by identity/move)- The
.into()call inside performs the conversion
Trade-off:
- Pro: Caller convenience - accepts multiple types
- Pro: Follows Rust API guidelines
- Con: Slightly less clear what conversions happen
Best Practice: Use impl Into<T> for owned types in constructors/builders,
use &str for borrowed parameters in regular methods.
Sourcepub fn no_translation_files_with_paths(
text: impl Into<String>,
paths: impl Into<String>,
) -> Self
pub fn no_translation_files_with_paths( text: impl Into<String>, paths: impl Into<String>, ) -> Self
Sourcepub fn yaml_parse_error(
file: impl Into<PathBuf>,
reason: impl Into<String>,
) -> Self
pub fn yaml_parse_error( file: impl Into<PathBuf>, reason: impl Into<String>, ) -> Self
Create a YamlParseError from a file path and error.
§Educational Notes - Multiple Generic Parameters
This method shows using impl Into<T> with different types:
impl Into<PathBuf>accepts&Path,PathBuf,&str,Stringimpl Into<String>accepts&str,String
Each parameter independently accepts its own set of convertible types.