codebank/error.rs
1use std::io;
2use std::path::PathBuf;
3use thiserror::Error;
4
5/// Error types for the CodeBank library.
6///
7/// This enum represents all possible errors that can occur in the CodeBank library.
8///
9/// # Examples
10///
11/// ```
12/// use codebank::Error;
13/// use std::path::PathBuf;
14///
15/// // Create an IO error
16/// let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
17/// let error = Error::Io(io_err);
18/// assert!(matches!(error, Error::Io(_)));
19///
20/// // Create a parse error
21/// let error = Error::Parse("invalid syntax".to_string());
22/// assert!(matches!(error, Error::Parse(_)));
23///
24/// // Create a file not found error
25/// let error = Error::FileNotFound(PathBuf::from("missing.rs"));
26/// assert!(matches!(error, Error::FileNotFound(_)));
27/// ```
28#[derive(Error, Debug)]
29pub enum Error {
30 /// IO error wrapper
31 #[error("IO error: {0}")]
32 Io(#[from] io::Error),
33
34 /// Parse error for syntax parsing failures
35 #[error("Parse error: {0}")]
36 Parse(String),
37
38 /// Tree-sitter error for tree-sitter specific failures
39 #[error("Tree-sitter error: {0}")]
40 TreeSitter(String),
41
42 /// File not found error
43 #[error("File not found: {0}")]
44 FileNotFound(PathBuf),
45
46 /// Directory not found error
47 #[error("Directory not found: {0}")]
48 DirectoryNotFound(PathBuf),
49
50 /// Invalid configuration error
51 #[error("Invalid configuration: {0}")]
52 InvalidConfig(String),
53
54 /// Unsupported language error
55 #[error("Unsupported language: {0}")]
56 UnsupportedLanguage(String),
57}
58
59/// Result type alias for CodeBank operations.
60///
61/// This type is used throughout the CodeBank library to handle operations
62/// that can fail with a [`Error`].
63///
64/// # Examples
65///
66/// ```
67/// use codebank::{Result, Error};
68/// use std::path::PathBuf;
69///
70/// fn example_operation() -> Result<String> {
71/// // Simulate a failing operation
72/// Err(Error::FileNotFound(PathBuf::from("missing.rs")))
73/// }
74///
75/// // Handle the result
76/// match example_operation() {
77/// Ok(content) => println!("Success: {}", content),
78/// Err(e) => println!("Operation failed: {}", e),
79/// }
80/// ```
81pub type Result<T> = std::result::Result<T, Error>;