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

use crate::error::Error;
use crate::tokenizer::Tokenizer;
use crate::ieee488::Context;

/// This trait implements a command with optional event/query operations.
///
///
/// # Example
///
/// ```
/// use scpi::command::Command;
/// use scpi::Context;
/// use scpi::error::Error;
/// use scpi::tokenizer::Tokenizer;
///
/// struct MyCommand {
///    //...
/// }
///
/// // Implement Command for MyCommand
/// impl Command for MyCommand {
///     fn event(&self,context: &mut Context, args: &mut Tokenizer) -> Result<(), Error> {
///         //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...
///         }
///
///         // Do stuff without x or y...
///
///         //I'm good thank you
///         Ok(())
///     }
///
///     fn query(&self,context: &mut Context, args: &mut Tokenizer) -> Result<(), Error> {
///         Err(Error::UndefinedHeader)//Query not allowed
///     }
///
/// }
///
/// ```
///
pub trait Command {

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

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