mcfunction_debugger/dap/
error.rs

1// Mcfunction-Debugger is a debugger for Minecraft's *.mcfunction files that does not require any
2// Minecraft mods.
3//
4// © Copyright (C) 2021-2024 Adrodoc <adrodoc55@googlemail.com> & Skagaros <skagaros@gmail.com>
5//
6// This file is part of Mcfunction-Debugger.
7//
8// Mcfunction-Debugger is free software: you can redistribute it and/or modify it under the terms of
9// the GNU General Public License as published by the Free Software Foundation, either version 3 of
10// the License, or (at your option) any later version.
11//
12// Mcfunction-Debugger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along with Mcfunction-Debugger.
17// If not, see <http://www.gnu.org/licenses/>.
18
19use 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}