mcfunction_debug_adapter/
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-2023 Adrodoc <adrodoc55@googlemail.com> & skess42 <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::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}
40
41pub enum RequestError<C> {
42    Terminate(C),
43    Respond(PartialErrorResponse),
44}
45impl<C> From<PartialErrorResponse> for RequestError<C> {
46    fn from(error: PartialErrorResponse) -> Self {
47        Self::Respond(error)
48    }
49}
50
51pub struct PartialErrorResponse {
52    pub message: String,
53    pub details: Option<ErrorMessage>,
54}
55
56impl PartialErrorResponse {
57    pub fn new(message: String) -> PartialErrorResponse {
58        PartialErrorResponse {
59            message,
60            details: None,
61        }
62    }
63
64    pub fn with_command(self, command: String) -> ErrorResponse {
65        ErrorResponse::builder()
66            .command(command)
67            .message(self.message)
68            .body(ErrorResponseBody::new(self.details))
69            .build()
70    }
71}
72
73impl From<io::Error> for PartialErrorResponse {
74    fn from(error: io::Error) -> Self {
75        Self {
76            message: error.to_string(),
77            details: None,
78        }
79    }
80}