dust/src/error.rs

304 lines
8.4 KiB
Rust
Raw Normal View History

2023-08-22 15:40:50 +00:00
//! Error and Result types.
//!
//! To deal with errors from dependencies, either create a new error variant
2023-10-13 17:53:00 +00:00
//! or use the ToolFailure variant if the error can only occur inside a tool.
2023-09-29 11:17:11 +00:00
2023-10-13 16:26:44 +00:00
use crate::{value::Value, Identifier};
2023-08-22 15:40:50 +00:00
2023-10-14 00:16:14 +00:00
use std::{fmt, io, time};
2023-08-22 15:40:50 +00:00
pub type Result<T> = std::result::Result<T, Error>;
2023-10-06 11:55:14 +00:00
#[derive(Clone, PartialEq)]
2023-08-22 15:40:50 +00:00
pub enum Error {
2023-10-10 17:29:11 +00:00
UnexpectedSyntaxNode {
2023-09-28 19:58:01 +00:00
expected: &'static str,
actual: &'static str,
2023-10-01 06:43:27 +00:00
location: tree_sitter::Point,
2023-10-06 11:55:14 +00:00
relevant_source: String,
2023-09-28 19:58:01 +00:00
},
2023-08-22 16:31:45 +00:00
/// The 'assert' macro did not resolve successfully.
AssertEqualFailed {
expected: Value,
actual: Value,
},
/// The 'assert' macro did not resolve successfully.
AssertFailed,
2023-08-22 15:40:50 +00:00
/// A row was inserted to a table with the wrong amount of values.
WrongColumnAmount {
expected: usize,
actual: usize,
},
/// An operator was called with the wrong amount of arguments.
ExpectedOperatorArgumentAmount {
expected: usize,
actual: usize,
},
/// A function was called with the wrong amount of arguments.
2023-10-11 16:07:30 +00:00
ExpectedToolArgumentAmount {
tool_name: &'static str,
2023-08-22 15:40:50 +00:00
expected: usize,
actual: usize,
},
/// A function was called with the wrong amount of arguments.
ExpectedAtLeastFunctionArgumentAmount {
identifier: String,
minimum: usize,
actual: usize,
},
ExpectedString {
actual: Value,
},
ExpectedInt {
actual: Value,
},
ExpectedFloat {
actual: Value,
},
/// An integer, floating point or value was expected.
ExpectedNumber {
actual: Value,
},
/// An integer, floating point or string value was expected.
ExpectedNumberOrString {
actual: Value,
},
ExpectedBoolean {
actual: Value,
},
ExpectedList {
actual: Value,
},
ExpectedFixedLenList {
expected_len: usize,
actual: Value,
},
ExpectedEmpty {
actual: Value,
},
ExpectedMap {
actual: Value,
},
ExpectedTable {
actual: Value,
},
ExpectedFunction {
actual: Value,
},
/// A string, list, map or table value was expected.
ExpectedCollection {
actual: Value,
},
/// A `VariableIdentifier` operation did not find its value in the context.
VariableIdentifierNotFound(String),
/// A `FunctionIdentifier` operation did not find its value in the context.
2023-10-06 17:32:58 +00:00
FunctionIdentifierNotFound(Identifier),
2023-08-22 15:40:50 +00:00
/// The function failed due to an external error.
2023-10-13 16:26:44 +00:00
ToolFailure(String),
2023-08-22 15:40:50 +00:00
/// A custom error explained by its message.
CustomMessage(String),
}
2023-10-14 17:52:16 +00:00
impl Error {
pub fn expect_tool_argument_amount(
tool_name: &'static str,
expected: usize,
actual: usize,
) -> Result<()> {
if expected == actual {
Ok(())
} else {
Err(Error::ExpectedToolArgumentAmount {
tool_name,
expected,
actual,
})
}
}
}
2023-08-22 15:40:50 +00:00
impl From<csv::Error> for Error {
fn from(value: csv::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl From<json::Error> for Error {
fn from(value: json::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl From<io::Error> for Error {
fn from(value: std::io::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl From<git2::Error> for Error {
fn from(value: git2::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl From<reqwest::Error> for Error {
fn from(value: reqwest::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
2023-10-14 00:16:14 +00:00
impl From<time::SystemTimeError> for Error {
fn from(value: time::SystemTimeError) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl From<trash::Error> for Error {
fn from(value: trash::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl From<toml::de::Error> for Error {
fn from(value: toml::de::Error) -> Self {
2023-10-13 16:26:44 +00:00
Error::ToolFailure(value.to_string())
2023-08-22 15:40:50 +00:00
}
}
impl std::error::Error for Error {}
2023-10-06 11:55:14 +00:00
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
2023-08-22 15:40:50 +00:00
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
match self {
2023-10-02 21:15:05 +00:00
AssertEqualFailed { expected, actual } => write!(
f,
"Equality assertion failed. {expected} does not equal {actual}."
),
AssertFailed => write!(
f,
"Assertion failed. A false value was passed to \"assert\"."
),
2023-08-22 15:40:50 +00:00
ExpectedOperatorArgumentAmount { expected, actual } => write!(
f,
"An operator expected {} arguments, but got {}.",
expected, actual
),
2023-10-11 16:07:30 +00:00
ExpectedToolArgumentAmount {
tool_name,
2023-08-22 15:40:50 +00:00
expected,
actual,
} => write!(
f,
2023-10-11 16:07:30 +00:00
"{tool_name} expected {expected} arguments, but got {actual}.",
2023-08-22 15:40:50 +00:00
),
ExpectedAtLeastFunctionArgumentAmount {
minimum,
actual,
identifier,
} => write!(
f,
"{identifier} expected a minimum of {minimum} arguments, but got {actual}.",
),
ExpectedString { actual } => {
write!(f, "Expected a Value::String, but got {:?}.", actual)
}
ExpectedInt { actual } => write!(f, "Expected a Value::Int, but got {:?}.", actual),
ExpectedFloat { actual } => write!(f, "Expected a Value::Float, but got {:?}.", actual),
ExpectedNumber { actual } => write!(
f,
"Expected a Value::Float or Value::Int, but got {:?}.",
actual
),
ExpectedNumberOrString { actual } => write!(
f,
"Expected a Value::Number or a Value::String, but got {:?}.",
actual
),
ExpectedBoolean { actual } => {
write!(f, "Expected a Value::Boolean, but got {:?}.", actual)
}
ExpectedList { actual } => write!(f, "Expected a Value::Tuple, but got {:?}.", actual),
ExpectedFixedLenList {
expected_len,
actual,
} => write!(
f,
"Expected a Value::Tuple of len {}, but got {:?}.",
expected_len, actual
),
ExpectedEmpty { actual } => write!(f, "Expected a Value::Empty, but got {:?}.", actual),
ExpectedMap { actual } => write!(f, "Expected a Value::Map, but got {:?}.", actual),
ExpectedTable { actual } => write!(f, "Expected a Value::Table, but got {:?}.", actual),
ExpectedFunction { actual } => {
write!(f, "Expected Value::Function, but got {:?}.", actual)
}
ExpectedCollection { actual } => {
write!(
f,
"Expected a string, list, map or table, but got {:?}.",
actual
)
}
VariableIdentifierNotFound(identifier) => write!(
f,
2023-10-10 21:12:38 +00:00
"Variable identifier is not bound to anything by context: {}.",
2023-08-22 15:40:50 +00:00
identifier
),
FunctionIdentifierNotFound(identifier) => write!(
f,
2023-10-10 21:12:38 +00:00
"Function identifier is not bound to anything by context: {}.",
identifier.inner()
2023-08-22 15:40:50 +00:00
),
2023-10-10 17:29:11 +00:00
UnexpectedSyntaxNode {
2023-10-02 21:15:05 +00:00
expected,
2023-08-22 15:40:50 +00:00
actual,
2023-10-02 21:15:05 +00:00
location,
2023-10-13 16:26:44 +00:00
relevant_source,
2023-10-13 23:56:57 +00:00
} => write!(
f,
"Expected syntax node {expected}, but got {actual} at {location}. Code: {relevant_source} ",
),
2023-10-14 00:16:14 +00:00
WrongColumnAmount { expected, actual } => write!(f, "Wrong column amount. Expected {expected} but got {actual}."),
ToolFailure(message) => write!(f, "{message}"),
CustomMessage(message) => write!(f, "{message}"),
2023-08-22 15:40:50 +00:00
}
}
}