1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// McFunction-Debugger is a debugger for Minecraft's *.mcfunction files that does not require any
// Minecraft mods.
//
// © Copyright (C) 2021-2023 Adrodoc <adrodoc55@googlemail.com> & skess42 <skagaros@gmail.com>
//
// This file is part of McFunction-Debugger.
//
// McFunction-Debugger is free software: you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// McFunction-Debugger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with McFunction-Debugger.
// If not, see <http://www.gnu.org/licenses/>.

use debug_adapter_protocol::{
    responses::{ErrorResponse, ErrorResponseBody},
    types::Message as ErrorMessage,
};
use std::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,
        }
    }
}

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,
        }
    }
}