mcfunction_debugger/dap/
error.rs1use debug_adapter_protocol::{
20 responses::{ErrorResponse, ErrorResponseBody},
21 types::Message as ErrorMessage,
22};
23use std::{fmt::Display, io};
24
25#[derive(Debug)]
26pub enum DebugAdapterError<I, O, C> {
27 Input(I),
28 Output(O),
29 Custom(C),
30}
31impl<E> DebugAdapterError<E, E, E> {
32 pub fn into_inner(self) -> E {
33 match self {
34 DebugAdapterError::Input(e) => e,
35 DebugAdapterError::Output(e) => e,
36 DebugAdapterError::Custom(e) => e,
37 }
38 }
39}
40impl<I, O, C> Display for DebugAdapterError<I, O, C>
41where
42 I: Display,
43 O: Display,
44 C: Display,
45{
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 match self {
48 DebugAdapterError::Input(inner) => write!(f, "Input error: {}", inner),
49 DebugAdapterError::Output(inner) => write!(f, "Output error: {}", inner),
50 DebugAdapterError::Custom(inner) => write!(f, "Custom error: {}", inner),
51 }
52 }
53}
54
55pub enum RequestError<C> {
56 Terminate(C),
57 Respond(PartialErrorResponse),
58}
59impl<C> From<PartialErrorResponse> for RequestError<C> {
60 fn from(error: PartialErrorResponse) -> Self {
61 Self::Respond(error)
62 }
63}
64
65pub struct PartialErrorResponse {
66 pub message: String,
67 pub details: Option<ErrorMessage>,
68}
69
70impl PartialErrorResponse {
71 pub fn new(message: String) -> PartialErrorResponse {
72 PartialErrorResponse {
73 message,
74 details: None,
75 }
76 }
77
78 pub fn with_command(self, command: String) -> ErrorResponse {
79 ErrorResponse::builder()
80 .command(command)
81 .message(self.message)
82 .body(ErrorResponseBody::new(self.details))
83 .build()
84 }
85}
86
87impl From<io::Error> for PartialErrorResponse {
88 fn from(error: io::Error) -> Self {
89 Self {
90 message: error.to_string(),
91 details: None,
92 }
93 }
94}