use crate::harness::ExecutionError;
use thiserror::Error;
use super::command::CommandError;
use super::git::GitError;
use super::instructions::InstructionError;
use super::search::SearchError;
use super::workspace::WorkspaceError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CodingError {
#[error("unexpected effect kind: {message}")]
UnexpectedKind {
message: String,
},
#[error("invalid coding payload: {message}")]
InvalidPayload {
message: String,
},
#[error("workspace error: {0}")]
Workspace(Box<WorkspaceError>),
#[error("command error: {0}")]
Command(#[from] CommandError),
#[error("git error: {0}")]
Git(#[from] GitError),
#[error("search error: {0}")]
Search(#[from] SearchError),
#[error("instruction error: {0}")]
Instruction(#[from] InstructionError),
}
impl From<WorkspaceError> for CodingError {
fn from(error: WorkspaceError) -> Self {
Self::Workspace(Box::new(error))
}
}
impl CodingError {
pub fn invalid_payload(message: impl Into<String>) -> Self {
Self::InvalidPayload {
message: message.into(),
}
}
pub fn unexpected_kind(message: impl Into<String>) -> Self {
Self::UnexpectedKind {
message: message.into(),
}
}
}
impl From<CodingError> for ExecutionError {
fn from(error: CodingError) -> Self {
match error {
CodingError::Command(CommandError::TimedOut { message }) => {
ExecutionError::TimedOut(message)
}
CodingError::Command(CommandError::Cancelled { message }) => {
ExecutionError::Cancelled(message)
}
CodingError::Command(CommandError::UnsupportedPolicy { message }) => {
ExecutionError::Denied(message)
}
CodingError::UnexpectedKind { message } => ExecutionError::Unsupported(message),
other => ExecutionError::Failed(other.to_string()),
}
}
}