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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use super::*;

use crate::error::Cause;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExceptionInfoArguments {
    pub thread_id: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExceptionInfoResponse {
    pub exception_id: String,
    pub description: Option<String>,
    pub break_mode: ExceptionBreakMode,
    pub details: Option<ExceptionDetails>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExceptionBreakMode {
    Never,
    Always,
    Unhandled,
    UserUnhandled,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExceptionDetails {
    pub message: Option<String>,
    pub type_name: Option<String>,
    pub full_type_name: Option<String>,
    pub evaluate_name: Option<String>,
    pub stack_trace: Option<String>,
    pub inner_exception: Vec<ExceptionDetails>,
}

impl From<ExceptionInfoArguments> for Value {
    fn from(args: ExceptionInfoArguments) -> Self {
        let ExceptionInfoArguments { thread_id } = args;

        let thread_id = utils::attribute_u64("threadId", thread_id);

        utils::finalize_object(thread_id)
    }
}

impl TryFrom<&Map<String, Value>> for ExceptionInfoArguments {
    type Error = Error;

    fn try_from(map: &Map<String, Value>) -> Result<Self, Self::Error> {
        let thread_id = utils::get_u64(map, "threadId")?;

        Ok(Self { thread_id })
    }
}

impl TryFrom<&Map<String, Value>> for ExceptionInfoResponse {
    type Error = Error;

    fn try_from(map: &Map<String, Value>) -> Result<Self, Self::Error> {
        let exception_id = utils::get_string(map, "exceptionId")?;
        let description = utils::get_string_optional(map, "description")?;
        let break_mode = ExceptionBreakMode::try_from(utils::get_string(map, "breakMode")?)?;
        let details = utils::get_object_optional(map, "details")?;

        Ok(Self {
            exception_id,
            description,
            break_mode,
            details,
        })
    }
}

impl From<ExceptionInfoResponse> for Value {
    fn from(args: ExceptionInfoResponse) -> Self {
        let ExceptionInfoResponse {
            exception_id,
            description,
            break_mode,
            details,
        } = args;

        let exception_id = utils::attribute_string("exceptionId", exception_id);
        let description = utils::attribute_string_optional("description", description);
        let break_mode = utils::attribute_string("breakMode", break_mode);
        let details = utils::attribute_optional("details", details);

        utils::finalize_object(
            exception_id
                .chain(description)
                .chain(break_mode)
                .chain(details),
        )
    }
}

impl TryFrom<&Map<String, Value>> for ExceptionDetails {
    type Error = Error;

    fn try_from(map: &Map<String, Value>) -> Result<Self, Self::Error> {
        let message = utils::get_string_optional(map, "message")?;
        let type_name = utils::get_string_optional(map, "typeName")?;
        let full_type_name = utils::get_string_optional(map, "fullTypeName")?;
        let evaluate_name = utils::get_string_optional(map, "evaluateName")?;
        let stack_trace = utils::get_string_optional(map, "stackTrace")?;
        let inner_exception = utils::get_array_optional(map, "innerException")?;

        Ok(Self {
            message,
            type_name,
            full_type_name,
            evaluate_name,
            stack_trace,
            inner_exception,
        })
    }
}

impl From<ExceptionDetails> for Value {
    fn from(args: ExceptionDetails) -> Self {
        let ExceptionDetails {
            message,
            type_name,
            full_type_name,
            evaluate_name,
            stack_trace,
            inner_exception,
        } = args;

        let message = utils::attribute_string_optional("message", message);
        let type_name = utils::attribute_string_optional("typeName", type_name);
        let full_type_name = utils::attribute_string_optional("fullTypeName", full_type_name);
        let evaluate_name = utils::attribute_string_optional("evaluateName", evaluate_name);
        let stack_trace = utils::attribute_string_optional("stackTrace", stack_trace);
        let inner_exception = utils::attribute_optional("innerException", Some(inner_exception));

        utils::finalize_object(
            message
                .chain(type_name)
                .chain(full_type_name)
                .chain(evaluate_name)
                .chain(stack_trace)
                .chain(inner_exception),
        )
    }
}

impl From<ExceptionBreakMode> for String {
    fn from(r: ExceptionBreakMode) -> Self {
        use self::ExceptionBreakMode::*;

        match r {
            Never => "never".into(),
            Always => "always".into(),
            Unhandled => "unhandled".into(),
            UserUnhandled => "userUnhandled".into(),
        }
    }
}

impl TryFrom<String> for ExceptionBreakMode {
    type Error = Error;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        use self::ExceptionBreakMode::*;

        Ok(match s.as_str() {
            "never" => Never,
            "always" => Always,
            "unhandled" => Unhandled,
            "userUnhandled" => UserUnhandled,
            _ => return Err(Error::new("ExceptionBreakMode", Cause::IsInvalid)),
        })
    }
}