use thiserror::Error;
#[derive(Debug, Error)]
pub enum GraphSONError {
#[error("JSON parse error: {0}")]
JsonParse(#[from] serde_json::Error),
#[error("unknown type tag: {0}")]
UnknownTypeTag(String),
#[error("missing required field: {0}")]
MissingField(String),
#[error("invalid value for type {type_tag}: {message}")]
InvalidValue {
type_tag: String,
message: String,
},
#[error("duplicate vertex ID: {0}")]
DuplicateVertexId(u64),
#[error("duplicate edge ID: {0}")]
DuplicateEdgeId(u64),
#[error("vertex not found: {0}")]
VertexNotFound(u64),
#[error("map keys must be strings, found: {0}")]
NonStringMapKey(String),
#[error("schema validation error: {0}")]
SchemaValidation(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
}
pub type Result<T> = std::result::Result<T, GraphSONError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = GraphSONError::UnknownTypeTag("g:Unknown".to_string());
assert_eq!(err.to_string(), "unknown type tag: g:Unknown");
let err = GraphSONError::InvalidValue {
type_tag: "g:Int64".to_string(),
message: "expected integer".to_string(),
};
assert_eq!(
err.to_string(),
"invalid value for type g:Int64: expected integer"
);
let err = GraphSONError::VertexNotFound(42);
assert_eq!(err.to_string(), "vertex not found: 42");
}
#[test]
fn test_json_error_conversion() {
let json_err: std::result::Result<serde_json::Value, _> =
serde_json::from_str("not valid json");
let err: GraphSONError = json_err.unwrap_err().into();
assert!(matches!(err, GraphSONError::JsonParse(_)));
}
#[test]
fn test_io_error_conversion() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let err: GraphSONError = io_err.into();
assert!(matches!(err, GraphSONError::Io(_)));
}
}