yang_rs/
lib.rs

1//
2// YANG - Yet Another Next Generation
3//   The parser and libraries supporting RFC7950.
4//
5//   Copyright (C) 2021 Toshiaki Takada
6//
7
8pub mod arg;
9pub mod compound;
10pub mod config;
11pub mod core;
12pub mod error;
13pub mod parser;
14pub mod stmt;
15pub mod substmt;
16
17#[macro_use]
18extern crate lazy_static;
19extern crate derive_getters;
20
21#[macro_export]
22macro_rules! collect_a_stmt {
23    ($stmts:expr, $st:ident) => {
24        match $stmts.get_mut(<$st>::keyword()) {
25            Some(v) => match v.pop() {
26                Some(en) => match en {
27                    YangStmt::$st(stmt) => Ok(stmt),
28                    _ => Err(YangError::MissingStatement(<$st>::keyword())),
29                },
30                None => Err(YangError::MissingStatement(<$st>::keyword())),
31            },
32            None => Err(YangError::MissingStatement(<$st>::keyword())),
33        }
34    };
35}
36
37#[macro_export]
38macro_rules! collect_vec_stmt {
39    ($stmts:expr, $st:ident) => {
40        match $stmts.get_mut(<$st>::keyword()) {
41            Some(v) => {
42                let mut error = false;
43                let mut w = Vec::new();
44                for en in v.drain(..) {
45                    if let YangStmt::$st(stmt) = en {
46                        w.push(stmt)
47                    } else {
48                        error = true;
49                    }
50                }
51
52                if error {
53                    Err(YangError::PlaceHolder)
54                } else {
55                    Ok(w)
56                }
57            }
58            None => Ok(Vec::new()),
59        }
60    };
61}
62
63#[macro_export]
64macro_rules! collect_opt_stmt {
65    ($stmts:expr, $st:ident) => {
66        match $stmts.get_mut(<$st>::keyword()) {
67            Some(v) => match v.pop() {
68                Some(en) => match en {
69                    YangStmt::$st(stmt) => Ok(Some(stmt)),
70                    _ => Err(YangError::MissingStatement(<$st>::keyword())),
71                },
72                None => Ok(None),
73            },
74            None => Ok(None),
75        }
76    };
77}
78
79#[macro_export]
80macro_rules! parse_a_stmt {
81    ($st:ident, $parser:ident) => {
82        match <$st>::parse($parser)? {
83            YangStmt::$st(stmt) => Ok(stmt),
84            _ => Err(YangError::MissingStatement(String::from(<$st>::keyword()))),
85        }
86    };
87}