rust_meth_lib/error.rs
1use thiserror::Error;
2
3/// The primary error tracking type for operations within the library.
4///
5/// Encapsulates platform errors (I/O, parsing) along with specific failure modes
6/// encountered during synchronization or communication with the underlying Language Server Protocol (LSP).
7#[derive(Debug, Error)]
8pub enum RustMethError {
9 /// Errors originating from direct operating system interaction or file system operations.
10 #[error("I/O error: {0}")]
11 Io(#[from] std::io::Error),
12
13 /// Errors resulting from failed JSON serialization or deserialization routines.
14 #[error("JSON error: {0}")]
15 Json(#[from] serde_json::Error),
16
17 /// Errors resulting from failed string-to-integer conversions.
18 #[error("Integer parse error: {0}")]
19 ParseInt(#[from] std::num::ParseIntError),
20
21 /// Indication that an unexpected byte payload was read from the language server stream
22 /// due to a missing or improperly formed `Content-Length` header field.
23 #[error("No Content-Length header in LSP response")]
24 NoContentLength,
25
26 /// Raised when looking for a specific LSP message payload, but the message loop bounds
27 /// were reached before a match was resolved.
28 #[error("recv_until: exhausted {limit} messages without a match")]
29 RecvExhausted {
30 /// The maximum number of distinct messages evaluated before halting the loop.
31 limit: usize,
32 },
33
34 /// Raised when `rust-analyzer` returns a payload structurally valid as JSON, but lacking
35 /// the fields or nested objects required by the expected API model contract.
36 #[error("Unexpected completion response shape: {0}")]
37 UnexpectedResponseShape(String),
38
39 /// Indicates that the `rust-analyzer` executable engine could not be discovered
40 /// via the host path environment or native component directory.
41 #[error(
42 "rust-analyzer not found.\nInstall it with: rustup component add rust-analyzer\nor ensure it is on your PATH."
43 )]
44 RustAnalyzerNotFound,
45
46 /// Indicates that the child process's `stdin` handle was not captured, typically because
47 /// it was not spawned with `Stdio::piped()`.
48 #[error("child process stdin was not captured (spawn with Stdio::piped())")]
49 MissingStdin,
50
51 /// Indicates that the child process's `stdout` handle was not captured, typically because
52 /// it was not spawned with `Stdio::piped()`.
53 #[error("child process stdout was not captured (spawn with Stdio::piped())")]
54 MissingStdout,
55
56 /// Raised when rust-analyzer reports a diagnostic error for the probed type,
57 /// indicating the type does not exist in scope (e.g. a typo or non-existent type).
58 #[error("type not found: `{type_name}` rust-analyzer reported: {message}")]
59 TypeNotFound {
60 /// The type name that was queried.
61 type_name: String,
62 /// The diagnostic message from rust-analyzer.
63 message: String,
64 },
65
66 #[error(
67 "rust-analyzer took too long to respond (waited {waited_secs}s querying `{type_name}`)"
68 )]
69 /// Raised when rust-analyzer fails to produce a usable response within the
70 /// configured retry budget e.g. a dependency-heavy crate is still being
71 /// fetched/indexed, or `rust-analyzer` itself has hung.
72 Timeout {
73 /// The name of the type
74 type_name: String,
75 /// How long we waited
76 waited_secs: u64,
77 },
78
79 #[error(
80 "`{type_name}` is feature-gated and not available with the current probe dependencies\n{message}"
81 )]
82 /// Raised when rust-analyzer resolves the path but reports the item was
83 /// "configured out" i.e. it exists in the crate but requires a Cargo
84 /// feature flag that wasn't enabled for the probe. The user should retry
85 /// with an explicit `--deps` override enabling the required feature.
86 FeatureGated {
87 /// The name of the type
88 type_name: String,
89 /// Message stating requirements
90 message: String,
91 },
92}
93
94/// A specialized type alias for results returning [`RustMethError`].
95pub type Result<T> = std::result::Result<T, RustMethError>;