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

use crate::prelude::Breakpoint;
use crate::prelude::Source;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetBreakpointsArguments {
    pub source: Source,
    pub breakpoints: Vec<SourceBreakpoint>,
    pub lines: Vec<u64>,
    pub source_modified: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceBreakpoint {
    pub line: u64,
    pub column: Option<u64>,
    pub condition: Option<String>,
    pub hit_condition: Option<String>,
    pub log_message: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetBreakpointsResponse {
    pub breakpoints: Vec<Breakpoint>,
}

impl From<SetBreakpointsArguments> for Value {
    fn from(args: SetBreakpointsArguments) -> Self {
        let SetBreakpointsArguments {
            source,
            breakpoints,
            lines,
            source_modified,
        } = args;

        let source = utils::attribute("source", source);
        let breakpoints = utils::attribute_optional("breakpoints", Some(breakpoints));
        let lines = utils::attribute_optional("lines", Some(lines));
        let source_modified = utils::attribute_bool_optional("sourceModified", source_modified);

        utils::finalize_object(
            source
                .chain(breakpoints)
                .chain(lines)
                .chain(source_modified),
        )
    }
}

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

    fn try_from(map: &Map<String, Value>) -> Result<Self, Self::Error> {
        let source = utils::get_object(map, "source")?;
        let breakpoints = utils::get_array_optional(map, "breakpoints")?;
        let lines = utils::get_array_of_u64_optional(map, "lines")?;
        let source_modified = utils::get_bool_optional(map, "sourceModified")?;

        Ok(Self {
            source,
            breakpoints,
            lines,
            source_modified,
        })
    }
}

impl From<SourceBreakpoint> for Value {
    fn from(args: SourceBreakpoint) -> Self {
        let SourceBreakpoint {
            line,
            column,
            condition,
            hit_condition,
            log_message,
        } = args;

        let line = utils::attribute_u64("line", line);
        let column = utils::attribute_u64_optional("column", column);
        let condition = utils::attribute_string_optional("condition", condition);
        let hit_condition = utils::attribute_string_optional("hitCondition", hit_condition);
        let log_message = utils::attribute_string_optional("logMessage", log_message);

        utils::finalize_object(
            line.chain(column)
                .chain(condition)
                .chain(hit_condition)
                .chain(log_message),
        )
    }
}

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

    fn try_from(map: &Map<String, Value>) -> Result<Self, Self::Error> {
        let line = utils::get_u64(map, "line")?;
        let column = utils::get_u64_optional(map, "column")?;
        let condition = utils::get_string_optional(map, "condition")?;
        let hit_condition = utils::get_string_optional(map, "hitCondition")?;
        let log_message = utils::get_string_optional(map, "logMessage")?;

        Ok(Self {
            line,
            column,
            condition,
            hit_condition,
            log_message,
        })
    }
}

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

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

        utils::finalize_object(breakpoints)
    }
}

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

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

        Ok(Self { breakpoints })
    }
}