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
//! Command trait and helper functions.
//!
//!

use crate::error::Result;
use crate::response::ResponseUnit;
use crate::tokenizer::Tokenizer;
use crate::Context;

/// This trait implements a command with optional event/query operations.
///
///
/// # Example
///
/// ```rust
/// use scpi::prelude::*;
/// use scpi::error::Result;
///
/// struct MyCommand {
///    //...
/// }
///
/// // Implement Command for MyCommand
/// impl Command for MyCommand {
///     fn event(&self,context: &mut Context, args: &mut Tokenizer) -> Result<()> {
///         //Read a optional argument x
///         if let Some(x) = args.next_data(true)? {
///             // Non-optional argument y if x is present
///             let y = args.next_data(false)?.unwrap();
///
///             // Do stuff with x and y...
///         }else{
///             // Do stuff with neither x or y...
///         }
///
///         //I'm good thank you
///         Ok(())
///     }
///
///     fn query(&self,context: &mut Context, args: &mut Tokenizer, response: &mut ResponseUnit) -> Result<()> {
///         Err(ErrorCode::UndefinedHeader.into())//Query not allowed
///     }
///
/// }
///
/// ```
///
pub trait Command {
    fn help(&self, _response: &mut ResponseUnit) {}

    fn meta(&self) -> CommandTypeMeta {
        CommandTypeMeta::Unknown
    }

    /// Called when the event form is used
    fn event(&self, context: &mut Context, args: &mut Tokenizer) -> Result<()>;

    ///Called when the query form is used
    fn query(
        &self,
        context: &mut Context,
        args: &mut Tokenizer,
        response: &mut ResponseUnit,
    ) -> Result<()>;
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum CommandTypeMeta {
    Unknown,
    NoQuery,
    QueryOnly,
    None,
}

/// Creates a stub for event()
///
#[macro_export]
macro_rules! qonly {
    () => {
        fn meta(&self) -> CommandTypeMeta {
            CommandTypeMeta::QueryOnly
        }

        fn event(&self, _context: &mut Context, _args: &mut Tokenizer) -> Result<()> {
            Err(ErrorCode::UndefinedHeader.into())
        }
    };
}

/// Creates a stub for query()
///
#[macro_export]
macro_rules! nquery {
    () => {
        fn meta(&self) -> CommandTypeMeta {
            CommandTypeMeta::NoQuery
        }

        fn query(
            &self,
            _context: &mut Context,
            _args: &mut Tokenizer,
            _response: &mut ResponseUnit,
        ) -> Result<()> {
            Err(ErrorCode::UndefinedHeader.into())
        }
    };
}

#[cfg(test)]
mod test_command {
    use crate::error::Result;
    use crate::prelude::*;

    struct Query {}
    impl Command for Query {
        qonly!();

        fn query(
            &self,
            _context: &mut Context,
            _args: &mut Tokenizer,
            _response: &mut ResponseUnit,
        ) -> Result<()> {
            Ok(())
        }
    }

    #[test]
    fn test_query() {
        assert_eq!(Query {}.meta(), CommandTypeMeta::QueryOnly);
    }

    struct Event {}
    impl Command for Event {
        nquery!();

        fn event(&self, _context: &mut Context, _args: &mut Tokenizer) -> Result<()> {
            Ok(())
        }
    }

    #[test]
    fn test_event() {
        assert_eq!(Event {}.meta(), CommandTypeMeta::NoQuery);
    }

    struct Default {}
    impl Command for Default {
        fn event(&self, _context: &mut Context, _args: &mut Tokenizer) -> Result<()> {
            Ok(())
        }

        fn query(
            &self,
            _context: &mut Context,
            _args: &mut Tokenizer,
            _response: &mut ResponseUnit,
        ) -> Result<()> {
            Ok(())
        }
    }

    #[test]
    fn test_default() {
        assert_eq!(Default {}.meta(), CommandTypeMeta::Unknown);
    }
}