1use crate::{ApplyError, ErrorCode, Failure, SelectionError, TemplateError, VfsError};
2
3use vvv_core::RelPath;
4use vvv_core::{
5 EditConflict, LanguageId, Position, QueryError, ResolveError, SearchError, SymbolKind,
6};
7
8use crate::history::HistoryError;
9
10#[derive(Debug, thiserror::Error)]
11pub enum EngineError {
12 #[error("{}: {source}", path.display())]
13 Open {
14 path: RelPath,
15 #[source]
16 source: std::io::Error,
17 },
18 #[error(transparent)]
19 Vfs(#[from] VfsError),
20 #[error(transparent)]
21 Query(#[from] QueryError),
22 #[error("search failed in {}", path.display())]
23 Search {
24 path: RelPath,
25 #[source]
26 source: SearchError,
27 },
28 #[error(transparent)]
29 Selection(#[from] SelectionError),
30 #[error("{}:{}", path.display(), line + 1)]
31 Template {
32 path: RelPath,
33 line: u32,
34 #[source]
35 source: TemplateError,
36 },
37 #[error(transparent)]
38 Conflict(#[from] EditConflict),
39 #[error("`{name}` is declared in several places ({}); pick one with --in <file>", declarations.iter().map(ToString::to_string).collect::<Vec<_>>().join(", "))]
40 AmbiguousSymbol {
41 name: String,
42 declarations: Vec<crate::graph::DeclarationSite>,
43 },
44 #[error("no {} named `{name}`", kind.map_or("symbol", SymbolKind::as_str))]
45 NoSuchSymbol {
46 name: String,
47 kind: Option<SymbolKind>,
48 },
49 #[error("{} already exists", .0.display())]
50 Exists(RelPath),
51 #[error("no registered language claims {}", .0.display())]
52 NoLanguage(RelPath),
53 #[error("{0} has no layout yet, so its paths cannot be followed or its files moved")]
54 NoLayout(LanguageId),
55 #[error("{}:{} is past the end of the file", path.display(), position.display())]
56 NoSuchPosition { path: RelPath, position: Position },
57 #[error(transparent)]
58 Resolve(#[from] ResolveError),
59 #[error(transparent)]
60 Apply(#[from] ApplyError),
61 #[error(transparent)]
62 History(#[from] HistoryError),
63}
64
65impl EngineError {
66 pub fn code(&self) -> ErrorCode {
68 match self {
69 Self::Open { .. } => ErrorCode::Io,
70 Self::Vfs(e)
71 | Self::History(HistoryError::Vfs(e))
72 | Self::Apply(ApplyError::Vfs(e)) => match e {
73 VfsError::NotFound(_) => ErrorCode::NotFound,
74 _ => ErrorCode::Io,
75 },
76 Self::Query(_) => ErrorCode::BadQuery,
77 Self::Search { .. } => ErrorCode::BadPattern,
78 Self::Selection(_) => ErrorCode::BadSelection,
79 Self::Template { .. } => ErrorCode::BadTemplate,
80 Self::Conflict(_) => ErrorCode::Conflict,
81 Self::AmbiguousSymbol { .. } => ErrorCode::AmbiguousSymbol,
82 Self::NoSuchSymbol { .. } => ErrorCode::NoSuchSymbol,
83 Self::Exists(_) => ErrorCode::Exists,
84 Self::NoLanguage(_) => ErrorCode::NoLanguage,
85 Self::NoLayout(_) => ErrorCode::NoLayout,
86 Self::NoSuchPosition { .. } => ErrorCode::NoSuchPosition,
87 Self::Resolve(e) => match e {
88 ResolveError::Missing(_) | ResolveError::NoDeclaringFile { .. } => {
89 ErrorCode::NotFound
90 }
91 _ => ErrorCode::Unmovable,
92 },
93 Self::Apply(e) => match e {
94 ApplyError::Stale { .. } | ApplyError::Modified { .. } => ErrorCode::Stale,
95 _ => ErrorCode::Io,
96 },
97 Self::History(_) => ErrorCode::NoHistory,
98 }
99 }
100
101 pub fn hint(&self) -> Option<String> {
103 match self {
104 Self::NoSuchSymbol { name, .. } => Some(format!(
105 "declarations are matched by exact name; `vvv search --name {name}` shows what exists"
106 )),
107 Self::AmbiguousSymbol { declarations, .. } => Some(format!(
108 "for example: --in {}",
109 declarations
110 .first()
111 .map(|d| d.path.display().to_string())
112 .unwrap_or_default()
113 )),
114 Self::Query(_) => Some(
115 "search by pattern: vvv search 'fn $NAME($$$) { $$$ }'\nby declaration: vvv search --symbol trait | vvv search --name Foo"
116 .to_owned(),
117 ),
118 _ => None,
119 }
120 }
121}
122
123impl From<&EngineError> for Failure {
124 fn from(error: &EngineError) -> Self {
125 let failure = Failure::new(error.code(), format!("{error:#}"));
126 match error.hint() {
127 Some(hint) => failure.with_hint(hint),
128 None => failure,
129 }
130 }
131}