lib_ruby_parser_nodes/
lib.rs

1#![doc = include_str!("../README.md")]
2
3extern crate liquid;
4extern crate serde;
5
6pub mod filters;
7pub mod helpers;
8mod messages;
9#[allow(non_upper_case_globals)]
10mod messages_data;
11mod nodes;
12#[allow(non_upper_case_globals)]
13mod nodes_data;
14
15pub use messages::*;
16pub use nodes::*;
17
18pub fn nodes() -> &'static [&'static Node] {
19    nodes_data::ALL_NODES
20}
21
22pub fn messages() -> &'static [&'static Message] {
23    messages_data::ALL_MESSAGES
24}
25
26mod liquid_template;
27pub use liquid_template::LiquidTemplate;
28
29pub mod reexports {
30    pub mod liquid {
31        pub use liquid_core::value;
32    }
33    pub mod serde {
34        pub use serde::{ser::SerializeStruct, Serialize, Serializer};
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use crate::{nodes, NodeFieldType};
41
42    #[test]
43    fn test_nodes_order() {
44        let nodes = nodes();
45
46        let contents = std::fs::read_to_string("src/nodes_data.rs").unwrap();
47        let mut node_decls = vec![];
48        for line in contents.lines() {
49            if line.starts_with("static") && line.ends_with(": Node = Node {") {
50                let decl = line
51                    .strip_prefix("static ")
52                    .unwrap()
53                    .strip_suffix(": Node = Node {")
54                    .unwrap();
55                node_decls.push(decl);
56            }
57        }
58        for (left, right) in node_decls.iter().zip(node_decls.iter().skip(1)) {
59            assert!(
60                left.to_lowercase() < right.to_lowercase(),
61                "node declarations are not sorted: {} goes before {}",
62                left,
63                right
64            )
65        }
66
67        for (left, right) in nodes.iter().zip(nodes.iter().skip(1)) {
68            let lhs = left.camelcase_name;
69            let rhs = right.camelcase_name;
70            assert!(
71                lhs.to_lowercase() < rhs.to_lowercase(),
72                "nodes are not sorted: {} goes before {}",
73                lhs,
74                rhs
75            )
76        }
77
78        assert_eq!(node_decls.len(), nodes.len());
79
80        for (node_decl, node) in node_decls.iter().zip(nodes.iter()) {
81            assert_eq!(
82                node_decl, &node.camelcase_name,
83                "node declaration order doesn't match nodes order: {} / {}",
84                node_decl, node.camelcase_name
85            )
86        }
87    }
88
89    #[test]
90    fn test_node_fields() {
91        let nodes = nodes();
92
93        for node in nodes {
94            assert!(
95                node.fields
96                    .iter()
97                    .any(|f| f.snakecase_name == "expression_l"),
98                "node {} doesn't have 'expression_l' field",
99                node.camelcase_name
100            );
101
102            let mut found_loc = false;
103            for node_field in node.fields {
104                if node_field.field_type == NodeFieldType::Loc
105                    || node_field.field_type == NodeFieldType::MaybeLoc
106                {
107                    found_loc = true;
108                } else if found_loc {
109                    panic!(
110                        "Fields of node {} are not sorted: {} goes after *_l fields",
111                        node.camelcase_name, node_field.snakecase_name
112                    )
113                }
114            }
115        }
116    }
117}