1use std::fmt::{self, Display, Formatter};
2use thiserror::Error;
3
4pub(crate) fn with_io_context(
5 additional: impl Display,
6 context: impl Display,
7 error: impl Into<std::io::Error>,
8) -> Error {
9 Error {
10 kind: ErrorKind::Io {
11 error: error.into(),
12 additional: additional.to_string(),
13 },
14 context: context.to_string(),
15 }
16}
17
18pub type Result<T, E = Error> = std::result::Result<T, E>;
19
20#[derive(Debug)]
23pub struct Error {
24 pub kind: ErrorKind,
26
27 pub context: String,
29}
30
31impl std::error::Error for Error {
32 fn cause(&self) -> Option<&dyn std::error::Error> {
33 self.kind.source()
34 }
35}
36
37impl Display for Error {
38 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39 write!(f, "encountered {} during {}", self.kind, self.context)
40 }
41}
42
43#[derive(Debug, Error)]
46#[non_exhaustive]
47pub enum ErrorKind {
48 #[error("{error} ({additional})")]
50 Io {
51 error: std::io::Error,
52 additional: String,
53 },
54
55 #[error("non-relative path {0}")]
57 NonRelativePath(std::path::PathBuf),
58
59 #[cfg(feature = "opencode")]
61 #[error("{0}")]
62 InvalidOpencodeConfigSyntax(jsonc_parser::errors::ParseError),
63
64 #[cfg(feature = "opencode")]
66 #[error("{0}")]
67 InvalidOpencodeConfigSchema(String),
68
69 #[cfg(feature = "zed")]
71 #[error("{0}")]
72 InvalidZedConfigSyntax(jsonc_parser::errors::ParseError),
73
74 #[cfg(feature = "zed")]
76 #[error("{0}")]
77 InvalidZedConfigSchema(String),
78}