Skip to main content

surql_parser/
error.rs

1//! Custom error types for the surql-parser public API.
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error {
5	#[error("Parse error: {0}")]
6	Parse(String),
7
8	#[error("IO error: {0}")]
9	Io(#[from] std::io::Error),
10
11	#[error("Schema error: {0}")]
12	Schema(String),
13
14	#[error("Format error: {0}")]
15	Format(String),
16}
17
18pub type Result<T> = std::result::Result<T, Error>;
19
20#[cfg(test)]
21mod tests {
22	use super::*;
23
24	#[test]
25	fn should_display_parse_error() {
26		let err = Error::Parse("unexpected token".into());
27		assert_eq!(err.to_string(), "Parse error: unexpected token");
28	}
29
30	#[test]
31	fn should_display_schema_error() {
32		let err = Error::Schema("table not found".into());
33		assert_eq!(err.to_string(), "Schema error: table not found");
34	}
35
36	#[test]
37	fn should_display_format_error() {
38		let err = Error::Format("invalid indent".into());
39		assert_eq!(err.to_string(), "Format error: invalid indent");
40	}
41
42	#[test]
43	fn should_convert_from_io_error() {
44		let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
45		let err: Error = io_err.into();
46		assert!(matches!(err, Error::Io(_)));
47		assert!(err.to_string().contains("file not found"));
48	}
49
50	#[test]
51	fn should_return_parse_error_from_invalid_surql() {
52		let result = crate::parse("SELEC * FORM user");
53		assert!(result.is_err());
54		let err = result.unwrap_err();
55		assert!(matches!(err, Error::Parse(_)));
56	}
57
58	#[test]
59	fn should_return_ok_from_valid_surql() {
60		let result = crate::parse("SELECT * FROM user");
61		assert!(result.is_ok());
62	}
63}