use debug_adapter_protocol::{
responses::{ErrorResponse, ErrorResponseBody},
types::Message as ErrorMessage,
};
use std::{fmt::Display, io};
#[derive(Debug)]
pub enum DebugAdapterError<I, O, C> {
Input(I),
Output(O),
Custom(C),
}
impl<E> DebugAdapterError<E, E, E> {
pub fn into_inner(self) -> E {
match self {
DebugAdapterError::Input(e) => e,
DebugAdapterError::Output(e) => e,
DebugAdapterError::Custom(e) => e,
}
}
}
impl<I, O, C> Display for DebugAdapterError<I, O, C>
where
I: Display,
O: Display,
C: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DebugAdapterError::Input(inner) => write!(f, "Input error: {}", inner),
DebugAdapterError::Output(inner) => write!(f, "Output error: {}", inner),
DebugAdapterError::Custom(inner) => write!(f, "Custom error: {}", inner),
}
}
}
pub enum RequestError<C> {
Terminate(C),
Respond(PartialErrorResponse),
}
impl<C> From<PartialErrorResponse> for RequestError<C> {
fn from(error: PartialErrorResponse) -> Self {
Self::Respond(error)
}
}
pub struct PartialErrorResponse {
pub message: String,
pub details: Option<ErrorMessage>,
}
impl PartialErrorResponse {
pub fn new(message: String) -> PartialErrorResponse {
PartialErrorResponse {
message,
details: None,
}
}
pub fn with_command(self, command: String) -> ErrorResponse {
ErrorResponse::builder()
.command(command)
.message(self.message)
.body(ErrorResponseBody::new(self.details))
.build()
}
}
impl From<io::Error> for PartialErrorResponse {
fn from(error: io::Error) -> Self {
Self {
message: error.to_string(),
details: None,
}
}
}