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
use super::*;

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScopesResponse {
    pub scopes: Vec<Scope>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopePresentationHint {
    Arguments,
    Locals,
    Registers,
    Custom(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Scope {
    pub name: String,
    pub presentation_hint: Option<ScopePresentationHint>,
    pub variables_reference: u64,
    pub named_variables: Option<u64>,
    pub indexed_variables: Option<u64>,
    pub expensive: bool,
    pub source: Option<Source>,
    pub line: Option<u64>,
    pub column: Option<u64>,
    pub end_line: Option<u64>,
    pub end_column: Option<u64>,
}

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

        let frame_id = utils::attribute_u64("frameId", frame_id);

        utils::finalize_object(frame_id)
    }
}

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

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

        Ok(Self { frame_id })
    }
}

impl From<ScopesResponse> for Value {
    fn from(response: ScopesResponse) -> Self {
        let ScopesResponse { scopes } = response;

        let scopes = utils::attribute_array("scopes", scopes);

        utils::finalize_object(scopes)
    }
}

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

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

        Ok(Self { scopes })
    }
}

impl From<ScopePresentationHint> for String {
    fn from(hint: ScopePresentationHint) -> Self {
        match hint {
            ScopePresentationHint::Arguments => "arguments".into(),
            ScopePresentationHint::Locals => "locals".into(),
            ScopePresentationHint::Registers => "registers".into(),
            ScopePresentationHint::Custom(s) => s,
        }
    }
}

impl From<&str> for ScopePresentationHint {
    fn from(s: &str) -> Self {
        match s {
            "arguments" => ScopePresentationHint::Arguments,
            "locals" => ScopePresentationHint::Locals,
            "registers" => ScopePresentationHint::Registers,
            _ => ScopePresentationHint::Custom(s.into()),
        }
    }
}

impl From<Scope> for Value {
    fn from(scope: Scope) -> Self {
        let Scope {
            name,
            presentation_hint,
            variables_reference,
            named_variables,
            indexed_variables,
            expensive,
            source,
            line,
            column,
            end_line,
            end_column,
        } = scope;

        let name = utils::attribute_string("name", name);
        let presentation_hint =
            utils::attribute_string_optional("presentationHint", presentation_hint);
        let variables_reference = utils::attribute_u64("variablesReference", variables_reference);
        let named_variables = utils::attribute_u64_optional("namedVariables", named_variables);
        let indexed_variables =
            utils::attribute_u64_optional("indexedVariables", indexed_variables);
        let expensive = utils::attribute_bool("expensive", expensive);
        let source = utils::attribute_optional("source", source);
        let line = utils::attribute_u64_optional("line", line);
        let column = utils::attribute_u64_optional("column", column);
        let end_line = utils::attribute_u64_optional("endLine", end_line);
        let end_column = utils::attribute_u64_optional("endColumn", end_column);

        utils::finalize_object(
            name.chain(presentation_hint)
                .chain(variables_reference)
                .chain(named_variables)
                .chain(indexed_variables)
                .chain(expensive)
                .chain(source)
                .chain(line)
                .chain(column)
                .chain(end_line)
                .chain(end_column),
        )
    }
}

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

    fn try_from(map: &Map<String, Value>) -> Result<Self, Self::Error> {
        let name = utils::get_string(map, "name")?;
        let presentation_hint =
            utils::get_str_optional(map, "presentationHint")?.map(ScopePresentationHint::from);
        let variables_reference = utils::get_u64(map, "variablesReference")?;
        let named_variables = utils::get_u64_optional(map, "namedVariables")?;
        let indexed_variables = utils::get_u64_optional(map, "indexedVariables")?;
        let expensive = utils::get_bool(map, "expensive")?;
        let source = utils::get_object_optional(map, "source")?;
        let line = utils::get_u64_optional(map, "line")?;
        let column = utils::get_u64_optional(map, "column")?;
        let end_line = utils::get_u64_optional(map, "endLine")?;
        let end_column = utils::get_u64_optional(map, "endColumn")?;

        Ok(Self {
            name,
            presentation_hint,
            variables_reference,
            named_variables,
            indexed_variables,
            expensive,
            source,
            line,
            column,
            end_line,
            end_column,
        })
    }
}