SearchError

Enum SearchError 

Source
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 by std::error::Error trait
  • Error: From thiserror, auto-implements std::error::Error

§The #[error("...")] attribute

  • Automatically implements Display trait
  • 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

Fields

§text: String
§searched_paths: String
§

YamlParseError

Failed to parse YAML file

Fields

§file: PathBuf
§reason: String
§

JsonParseError

Failed to parse JSON file

Fields

§file: PathBuf
§reason: String
§

NoCodeReferences

Translation key found but no code references detected

Fields

§file: PathBuf
§

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

Source

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:

  • &str implements Into<String> (converts by allocating)
  • String implements Into<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.

Source

pub fn no_translation_files_with_paths( text: impl Into<String>, paths: impl Into<String>, ) -> Self

Create a NoTranslationFiles error with custom searched paths.

§Example
use code_search_cli::error::SearchError;

let err = SearchError::no_translation_files_with_paths(
    "Add New",
    "src/locales, config/i18n"
);

Both parameters accept &str or String thanks to impl Into<String>.

Source

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, String
  • impl Into<String> accepts &str, String

Each parameter independently accepts its own set of convertible types.

Source

pub fn json_parse_error( file: impl Into<PathBuf>, reason: impl Into<String>, ) -> Self

Create a JsonParseError from a file path and error

Source

pub fn no_code_references( key: impl Into<String>, file: impl Into<PathBuf>, ) -> Self

Create a NoCodeReferences error

Trait Implementations§

Source§

impl Debug for SearchError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SearchError

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for SearchError

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Error> for SearchError

Source§

fn from(source: Error) -> Self

Converts to this type from the input type.
Source§

impl From<FromUtf8Error> for SearchError

Source§

fn from(source: FromUtf8Error) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.