avm1_parser/
lib.rs

1pub use crate::avm1::parse_action;
2pub use crate::cfg::parse_cfg;
3
4mod avm1;
5mod basic_data_types;
6mod cfg;
7
8#[cfg(test)]
9mod parser_tests {
10  use super::*;
11  use ::avm1_types as avm1;
12  use ::test_generator::test_resources;
13  use avm1_types::cfg::Cfg;
14  use std::io::{Read, Write};
15  use std::path::Path;
16
17  #[test_resources("../tests/actions/*.avm1")]
18  fn test_parse_action(path: &str) {
19    let json_path: String = path.replace(".avm1", ".json");
20    let mut input_file = ::std::fs::File::open(path).unwrap();
21    let mut input: Vec<u8> = Vec::new();
22    input_file.read_to_end(&mut input).expect("Failed to read AVM1 file");
23
24    let (remaining_input, actual_action) = parse_action(&input).unwrap();
25
26    assert_eq!(remaining_input, &[] as &[u8]);
27
28    let json_file = ::std::fs::File::open(json_path).unwrap();
29    let reader = ::std::io::BufReader::new(json_file);
30    let expected_action: avm1::raw::Action = serde_json_v8::from_reader(reader).unwrap();
31
32    assert_eq!(actual_action, expected_action);
33  }
34
35  #[test_resources("../tests/avm1/[!.]*/*/")]
36  fn test_parse_cfg(path: &str) {
37    use serde::Serialize;
38
39    let path: &Path = Path::new(path);
40    let _name = path
41      .components()
42      .last()
43      .unwrap()
44      .as_os_str()
45      .to_str()
46      .expect("Failed to retrieve sample name");
47
48    //    if name == "hello-world" || name == "homestuck-beta2" {
49    //      return;
50    //    }
51
52    let avm1_path = path.join("main.avm1");
53    let avm1_bytes: Vec<u8> = ::std::fs::read(avm1_path).expect("Failed to read input");
54
55    let actual_cfg = parse_cfg(&avm1_bytes);
56
57    let actual_cfg_path = path.join("local-cfg.rs.json");
58    let actual_cfg_file = ::std::fs::File::create(actual_cfg_path).expect("Failed to create actual CFG file");
59    let actual_cfg_writer = ::std::io::BufWriter::new(actual_cfg_file);
60
61    let mut ser = serde_json_v8::Serializer::pretty(actual_cfg_writer);
62    actual_cfg.serialize(&mut ser).expect("Failed to write actual CFG");
63    ser.into_inner().write_all(b"\n").unwrap();
64
65    // assert_eq!(remaining_input, &[] as &[u8]);
66
67    let expected_cfg_path = path.join("cfg.json");
68    let expected_cfg_file = ::std::fs::File::open(expected_cfg_path).expect("Failed to open CFG");
69    let expected_cfg_reader = ::std::io::BufReader::new(expected_cfg_file);
70    let expected_cfg = serde_json_v8::from_reader::<_, Cfg>(expected_cfg_reader).expect("Failed to read CFG");
71
72    assert_eq!(actual_cfg, expected_cfg);
73  }
74}
75
76//struct Node<'a> {
77//  pub parent: Option<&'a Node<'a>>,
78//  pub value: u32,
79//}
80//
81////fn root_node(value: u32) -> Node<'static> {
82////  Node { parent: None, value }
83////}
84////
85//fn child_node<'inner, 'outer: 'inner>(parent: &'inner mut Node<'outer>, value: u32) -> Node<'inner> {
86//  Node { parent: Some(parent), value }
87//}
88//
89//impl Node<'_> {
90//  fn root(value: u32) -> Node<'static> {
91//    Node { parent: None, value }
92//  }
93//}
94//
95//impl<'inner, 'outer: 'inner> Node<'outer> {
96//  fn child(&'inner mut self, value: u32) -> Node<'inner> {
97//    Node { parent: Some(self), value }
98//  }
99//}
100//
101//pub fn main() {
102//  let mut root = Node::root(0);
103//  let mut c1 = root.child(1);
104//  let mut c2 = child_node(&mut c1, 2);
105//  {
106//    let mut c3 = child_node(&mut c2, 3);
107//    let c4 = child_node(&mut c3, 4);
108//    let mut cur = Some(&c4);
109//    while let Some(n) = cur {
110//      println!("{}", n.value);
111//      cur = n.parent;
112//    }
113//  }
114//  println!("{}", c2.value);
115//}