Skip to main content

reifydb_evaluate/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_value::{
5	error::{Diagnostic, Error, IntoDiagnostic},
6	fragment::Fragment,
7};
8
9#[derive(Debug, thiserror::Error)]
10pub enum EvaluateError {
11	#[error("Unknown function: {name}")]
12	UnknownFunction {
13		name: String,
14		fragment: Fragment,
15	},
16
17	#[error("Variable '{name}' is not defined")]
18	VariableNotFound {
19		name: String,
20	},
21
22	#[error("Cannot reassign immutable variable '{name}'")]
23	VariableIsImmutable {
24		name: String,
25	},
26}
27
28impl IntoDiagnostic for EvaluateError {
29	fn into_diagnostic(self) -> Diagnostic {
30		match self {
31			EvaluateError::UnknownFunction {
32				name,
33				fragment,
34			} => Diagnostic {
35				code: "FUNCTION_001".to_string(),
36				rql: None,
37				message: format!("Unknown function: {}", name),
38				column: None,
39				fragment,
40				label: Some("unknown function".to_string()),
41				help: Some("Check the function name and available functions".to_string()),
42				notes: vec![],
43				cause: None,
44				operator_chain: None,
45			},
46			EvaluateError::VariableNotFound {
47				name,
48			} => Diagnostic {
49				code: "RUNTIME_001".to_string(),
50				rql: None,
51				message: format!("Variable '{}' is not defined", name),
52				column: None,
53				fragment: Fragment::None,
54				label: None,
55				help: Some(format!(
56					"Define the variable using 'let {} = <value>' before using it",
57					name
58				)),
59				notes: vec![],
60				cause: None,
61				operator_chain: None,
62			},
63			EvaluateError::VariableIsImmutable {
64				name,
65			} => Diagnostic {
66				code: "RUNTIME_003".to_string(),
67				rql: None,
68				message: format!("Cannot reassign immutable variable '{}'", name),
69				column: None,
70				fragment: Fragment::None,
71				label: None,
72				help: Some("Use 'let mut $name := value' to declare a mutable variable".to_string()),
73				notes: vec!["Only mutable variables can be reassigned".to_string()],
74				cause: None,
75				operator_chain: None,
76			},
77		}
78	}
79}
80
81impl From<EvaluateError> for Error {
82	fn from(err: EvaluateError) -> Self {
83		Error(Box::new(err.into_diagnostic()))
84	}
85}