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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! A SCPI command tree consisting of command nodes and handlers.
//!
//! Se chapter 6 of SCPI standard for guidelines for header and command tree guidelines.
//!
//! # Example
//! Take the following command tree.
//! ```text
//! *COM
//! :TODO
//! :TODO?
//! :BRANch
//!     :CHILd <args>
//!     [:DEFault?] <args>
//! ```
//! This would be written like below.
//! Note that a node handles both the query and event form of commands and it's up to the handler to reject a query on an event only node or vice-versa.
//! ```
//! # struct MyDevice;
//! # impl scpi::Device for MyDevice {
//! #     fn handle_error(&mut self, err: Error) {}
//! # }
//! use scpi::tree::{prelude::*, command::Todo};
//! const ROOT: Node<MyDevice> = Branch {
//!     name: b"",
//!     default: false,
//!     sub: &[
//!         Leaf {
//!             name: b"*COM",
//!             default: false,
//!             handler: &Todo,
//!         },
//!         Leaf {
//!             name: b"TODO",
//!             default: false,
//!             handler: &Todo,//Handles both TODO and TODO?
//!         },
//!         Branch {
//!             name: b"BRANch",
//!             default: false,
//!             sub: &[
//!                 // Default leaves must be first!
//!                 Leaf {
//!                     name: b"DEFault",
//!                     default: true,
//!                     handler: &Todo,
//!                 },
//!                 Leaf {
//!                     name: b"CHILd",
//!                     default: false,
//!                     handler: &Todo,
//!                 },
//!             ],
//!         },
//!     ],
//! };
//! ```

use core::iter::Peekable;
//extern crate std;

pub mod command;

use command::Command;

use crate::error::{Error, ErrorCode, Result};
use crate::parser::parameters::Parameters;
use crate::parser::response::Formatter;
use crate::parser::tokenizer::{Token, Tokenizer};
use crate::{Context, Device};

/// Everything needed when creating command trees or command handlers
pub mod prelude {
    pub use super::{
        command::{Command, CommandTypeMeta},
        Node::{self, Branch, Leaf},
    };
    pub use crate::{
        error::{Error, ErrorCode},
        parser::{
            format::*,
            parameters::Parameters,
            response::{Formatter, ResponseData, ResponseUnit},
            tokenizer::{Token, Tokenizer},
        },
        Context, Device,
    };
}

/// A SCPI command node
/// These nodes are structured as a command tree where each node represent a SCPI header mnemonic.
///
pub enum Node<'a, D> {
    /// A leaf node which can be called or queried.
    Leaf {
        /// Mnemonic of this leaf
        name: &'static [u8],
        /// Default node, will be executed if the branch immediately below is executed.
        /// Only one default node is allowed in each branch.
        default: bool,
        /// Command handler
        handler: &'a dyn Command<D>,
    },
    /// A branch which contains one or more leaves.
    Branch {
        /// Mnemonic of this branch
        name: &'static [u8],
        /// Default node.
        default: bool,
        /// Child nodes
        /// **Note:** Default node must be first!
        sub: &'a [Node<'a, D>],
    },
}

impl<'a, D> Node<'a, D> {
    /// Create a leaf node
    ///
    /// Alternatively use [crate::Leaf!]
    pub const fn leaf(name: &'static [u8], handler: &'a dyn Command<D>) -> Self {
        Self::Leaf {
            name,
            default: false,
            handler,
        }
    }

    /// Create a default leaf node
    ///
    /// Alternatively use [crate::Leaf!]
    pub const fn default_leaf(name: &'static [u8], handler: &'a dyn Command<D>) -> Self {
        Self::Leaf {
            name,
            default: true,
            handler,
        }
    }

    /// Create a branch node
    ///
    /// Alternatively use [crate::Branch!]
    pub const fn branch(name: &'static [u8], sub: &'a [Node<'a, D>]) -> Self {
        Self::Branch {
            name,
            default: false,
            sub,
        }
    }

    /// Create a default branch node
    ///
    /// Alternatively use [crate::Branch!]
    pub const fn default_branch(name: &'static [u8], sub: &'a [Node<'a, D>]) -> Self {
        Self::Branch {
            name,
            default: true,
            sub,
        }
    }

    /// Create a root node
    ///
    /// Alternatively use [crate::Root!]
    pub const fn root(sub: &'a [Node<'a, D>]) -> Self {
        Self::Branch {
            name: b"",
            default: false,
            sub,
        }
    }
}

/// A utility to create a [Node::Leaf].
#[macro_export]
macro_rules! Leaf {
    ($name:literal => $handler:expr) => {
        $crate::tree::Node::Leaf {
            name: $name,
            default: false,
            handler: $handler,
        }
    };
    (default $name:literal => $handler:expr) => {
        $crate::tree::Node::Leaf {
            name: $name,
            default: true,
            handler: $handler,
        }
    };
}

/// A utility to create a [Node::Branch].
#[macro_export]
macro_rules! Branch {
    ($name:literal; $($child:expr),+) => {
        $crate::tree::Node::Branch {
            name: $name,
            default: false,
            sub: &[
                $($child),+
            ],
        }
    };
    ($name:literal => $handler:expr; $($child:expr),+) => {
        $crate::tree::Node::Branch {
            name: $name,
            default: false,
            sub: &[
                Leaf!{default b"" => $handler },
                $($child),+
            ],
        }
    };
    (default $name:literal; $($child:expr),+) => {
        $crate::tree::Node::Branch {
            name: $name,
            default: true,
            sub: &[
                $($child),+
            ],
        }
    };
}

/// A utility to create the root [Node] of a command tree.
#[macro_export]
macro_rules! Root {
    ($($child:expr),+) => {
        $crate::tree::Node::Branch {
            name: b"",
            default: false,
            sub: &[
                $($child),+
            ],
        }
    };
}

impl<'a, D> Node<'a, D> {
    pub fn name(&self) -> &'static [u8] {
        match self {
            Self::Leaf { name, .. } => name,
            Self::Branch { name, .. } => name,
        }
    }
}

impl<'a, D> Node<'a, D>
where
    D: Device,
{
    /// Execute a command against a given device.
    ///
    /// # Arguments:
    /// * command - To be executed
    /// * device - To execute against
    /// * context - Context for this command
    /// * response - A formatter to write a response into.
    ///
    pub fn run<FMT>(
        &self,
        command: &[u8],
        device: &mut D,
        context: &mut Context,
        response: &mut FMT,
    ) -> Result<()>
    where
        FMT: Formatter,
    {
        let mut tokenizer = Tokenizer::new(command).peekable();
        let res = self.run_tokens(device, context, &mut tokenizer, response);
        if let Err(err) = &res {
            device.handle_error(*err);
        }
        res
    }

    pub(crate) fn run_tokens<FMT>(
        &self,
        device: &mut D,
        context: &mut Context,
        tokens: &mut Peekable<Tokenizer>,
        response: &mut FMT,
    ) -> Result<()>
    where
        FMT: Formatter,
    {
        let mut leaf = self;

        //Start response message
        response.message_start()?;
        loop {
            // Execute header
            match tokens.peek() {
                // :header..
                Some(Ok(Token::HeaderMnemonicSeparator)) => {
                    leaf = self;
                    // Consume seperator
                    tokens.next();
                    self.exec(&mut leaf, device, context, tokens, response)?;
                }
                // header.. | *header
                Some(Ok(Token::ProgramMnemonic(s))) => {
                    if s.starts_with(b"*") {
                        let mut _x = self;
                        self.exec(&mut _x, device, context, tokens, response)?;
                    } else {
                        leaf.exec(&mut leaf, device, context, tokens, response)?;
                    }
                }
                // Empty input
                None => break Ok(()),
                //
                Some(Err(err)) => break Err(Error::new(*err)),
                // idk?
                Some(_) => break Err(ErrorCode::SyntaxError.into()),
            }
            // Should've consumed up to unit seperator

            // What's next?
            match tokens.next() {
                // EOM
                None => {
                    if !response.is_empty() {
                        response.message_end()?;
                    }
                    break Ok(());
                }
                // New unit
                Some(Ok(Token::ProgramMessageUnitSeparator)) => {
                    continue;
                }
                // More tokens...
                Some(Ok(tok)) => {
                    if tok.is_data() || tok == Token::ProgramDataSeparator {
                        break Err(ErrorCode::ParameterNotAllowed.into());
                    } else {
                        break Err(ErrorCode::SyntaxError.into());
                    }
                }
                // Error
                Some(Err(err)) => break Err(Error::new(err)),
            }
        }
    }

    pub(crate) fn exec<FMT>(
        &'a self,
        leaf: &mut &'a Node<'a, D>,
        device: &mut D,
        context: &mut Context,
        tokens: &mut Peekable<Tokenizer>,
        response: &mut FMT,
    ) -> Result<()>
    where
        FMT: Formatter,
    {
        let next = match tokens.peek() {
            Some(Ok(tok)) => Some(tok),
            Some(Err(err)) => return Err(Error::new(*err)),
            None => None,
        };

        //extern crate std;

        match self {
            Node::Leaf { handler, .. } => {
                //std::println!("Leaf {}", std::str::from_utf8(name).unwrap());
                match next {
                    // "Leaf .." | "Leaf\EOM"
                    Some(Token::ProgramHeaderSeparator | Token::ProgramMessageUnitSeparator)
                    | None => {
                        // Consume the header seperator
                        tokens.next_if(|t| matches!(t, Ok(Token::ProgramHeaderSeparator)));

                        // Execute handler
                        handler.event(device, context, Parameters::with(tokens))
                    }
                    // Branch?..
                    Some(Token::HeaderQuerySuffix) => {
                        // Consume query suffix
                        tokens.next();

                        // Consume header seperator
                        tokens.next_if(|t| matches!(t, Ok(Token::ProgramHeaderSeparator)));

                        // Execute handler
                        let response_unit = response.response_unit()?;
                        handler.query(device, context, Parameters::with(tokens), response_unit)
                    }
                    // This is a leaf node, cannot traverse further
                    Some(Token::HeaderMnemonicSeparator | Token::ProgramMnemonic(..)) => {
                        Err(ErrorCode::UndefinedHeader.into())
                    }
                    // Tokenizer shouldn't emit anything else...
                    Some(_) => Err(ErrorCode::SyntaxError.into()),
                }
            }
            Node::Branch { sub, .. } => {
                //std::println!("Branch {}", std::str::from_utf8(name).unwrap());
                match next {
                    // Branch[:]<mnemonic>..
                    Some(Token::HeaderMnemonicSeparator | Token::ProgramMnemonic(..)) => {
                        // Consume seperator
                        tokens.next_if(|t| matches!(t, Ok(Token::HeaderMnemonicSeparator)));

                        // Get mnemonic
                        let mnemonic = match tokens.peek() {
                            Some(Ok(mnemonic @ Token::ProgramMnemonic(..))) => mnemonic,
                            Some(Err(err)) => return Err((*err).into()),
                            _ => return Err(ErrorCode::CommandHeaderError.into()),
                        };

                        //std::println!("Branch:{mnemonic:?}");

                        // Try to match a child with mnemonic
                        *leaf = self;
                        for child in *sub {
                            if mnemonic.match_program_header(child.name()) {
                                tokens.next(); // Consume mnemonic
                                return child.exec(leaf, device, context, tokens, response);
                            }
                        }

                        // Check if there's a default child branch
                        if let Some(child) = sub
                            .iter()
                            .find(|child| matches!(child, Node::Branch { default: true, .. }))
                        {
                            child.exec(leaf, device, context, tokens, response)
                        } else {
                            Err(ErrorCode::UndefinedHeader.into())
                        }
                    }
                    // Branch .. | Branch\EOM | Branch;
                    Some(
                        Token::ProgramHeaderSeparator
                        | Token::ProgramMessageUnitSeparator
                        | Token::HeaderQuerySuffix,
                    )
                    | None => {
                        // Try to find a default leaf or branch execute
                        if let Some(default_leaf) = sub
                            .iter()
                            .find(|child| matches!(child, Node::Leaf { default: true, .. }))
                        {
                            default_leaf.exec(leaf, device, context, tokens, response)
                        } else if let Some(default_branch) = sub
                            .iter()
                            .find(|child| matches!(child, Node::Branch { default: true, .. }))
                        {
                            default_branch.exec(leaf, device, context, tokens, response)
                        } else {
                            Err(ErrorCode::UndefinedHeader.into())
                        }
                    }
                    // Tokenizer shouldn't emit anything else...
                    Some(_) => Err(ErrorCode::SyntaxError.into()),
                }
            }
        }
    }
}