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

use crate::error::Cause;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NextArguments {
    pub thread_id: u64,
    pub single_thread: bool,
    pub granularity: Option<SteppingGranularity>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SteppingGranularity {
    Statement,
    Line,
    Instruction,
}

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

        let thread_id = utils::attribute_u64("threadId", thread_id);
        let single_thread = utils::attribute_bool_optional("singleThread", single_thread);
        let granularity = utils::attribute_string_optional("granularity", granularity);

        utils::finalize_object(thread_id.chain(single_thread).chain(granularity))
    }
}

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

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

        let granularity = utils::get_string_optional(map, "granularity")?
            .map(SteppingGranularity::try_from)
            .transpose()?;

        Ok(Self {
            thread_id,
            single_thread,
            granularity,
        })
    }
}

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

        match r {
            Statement => "statement".into(),
            Line => "line".into(),
            Instruction => "instruction".into(),
        }
    }
}

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

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

        Ok(match s.as_str() {
            "statement" => Statement,
            "line" => Line,
            "instruction" => Instruction,
            _ => return Err(Error::new("granularity", Cause::IsInvalid)),
        })
    }
}