use thiserror::Error;
pub type Result<T> = core::result::Result<T, QvmError>;
#[derive(Error, Debug, Clone, PartialEq)]
pub enum QvmError {
#[error("Parse error: {message} at position {position}")]
ParseError { message: String, position: usize },
#[error("Invalid circuit: {0}")]
InvalidCircuit(String),
#[error("Topology error: {0}")]
TopologyError(String),
#[error("Scheduling error: {0}")]
SchedulingError(String),
#[error("Resource allocation failed: {0}")]
AllocationError(String),
#[error("Circuit composition failed: {0}")]
CompositionError(String),
#[cfg(feature = "std")]
#[error("I/O error: {0}")]
IoError(String),
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("Internal error: {0}")]
InternalError(String),
}
impl QvmError {
pub fn parse_error(message: impl Into<String>, position: usize) -> Self {
Self::ParseError {
message: message.into(),
position,
}
}
pub fn invalid_circuit(message: impl Into<String>) -> Self {
Self::InvalidCircuit(message.into())
}
pub fn topology_error(message: impl Into<String>) -> Self {
Self::TopologyError(message.into())
}
pub fn scheduling_error(message: impl Into<String>) -> Self {
Self::SchedulingError(message.into())
}
pub fn allocation_error(message: impl Into<String>) -> Self {
Self::AllocationError(message.into())
}
pub fn composition_error(message: impl Into<String>) -> Self {
Self::CompositionError(message.into())
}
pub fn config_error(message: impl Into<String>) -> Self {
Self::ConfigError(message.into())
}
pub fn internal_error(message: impl Into<String>) -> Self {
Self::InternalError(message.into())
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for QvmError {
fn from(err: std::io::Error) -> Self {
Self::IoError(err.to_string())
}
}
impl From<serde_json::Error> for QvmError {
fn from(err: serde_json::Error) -> Self {
Self::ParseError {
message: err.to_string(),
position: 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = QvmError::parse_error("Expected token", 10);
assert_eq!(
err.to_string(),
"Parse error: Expected token at position 10"
);
}
#[test]
fn test_error_chain() {
let err = QvmError::scheduling_error("No resources available");
assert!(err.to_string().contains("scheduling"));
}
}