changeset 701:b6c351ca0a35

anm0: return the nom IResult and the list of Anm0s.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 15:34:03 +0200
parents ccb739c5b66c
children 718348c7608e
files examples/anmrenderer.rs examples/eclrenderer.rs examples/stdrenderer.rs src/th06/anm0.rs src/th06/anm0_vm.rs src/th06/ecl_vm.rs src/th06/enemy.rs
diffstat 7 files changed, 17 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/examples/anmrenderer.rs	Fri Aug 23 13:05:48 2019 +0200
+++ b/examples/anmrenderer.rs	Fri Aug 23 15:34:03 2019 +0200
@@ -108,7 +108,8 @@
 
     // Open the ANM file.
     let buf = load_file_into_vec(anm_filename);
-    let anm0 = Anm0::from_slice(&buf).unwrap();
+    let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+    let anm0 = anms.pop().unwrap();
 
     if !anm0.scripts.contains_key(&script) {
         eprintln!("This anm0 doesn’t contain a script named {}.", script);
--- a/examples/eclrenderer.rs	Fri Aug 23 13:05:48 2019 +0200
+++ b/examples/eclrenderer.rs	Fri Aug 23 15:34:03 2019 +0200
@@ -115,7 +115,8 @@
 
     // Open the ANM file.
     let buf = load_file_into_vec(anm_filename);
-    let anm0 = Anm0::from_slice(&buf).unwrap();
+    let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+    let anm0 = anms.pop().unwrap();
     let anm0 = Rc::new(RefCell::new(anm0));
 
     if ecl.subs.len() < sub as usize {
--- a/examples/stdrenderer.rs	Fri Aug 23 13:05:48 2019 +0200
+++ b/examples/stdrenderer.rs	Fri Aug 23 15:34:03 2019 +0200
@@ -132,7 +132,8 @@
 
     // Open the ANM file.
     let buf = load_file_into_vec(anm_filename);
-    let anm0 = Anm0::from_slice(&buf).unwrap();
+    let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+    let anm0 = anms.pop().unwrap();
 
     // TODO: seed this PRNG with a valid seed.
     let prng = Rc::new(RefCell::new(Prng::new(0)));
--- a/src/th06/anm0.rs	Fri Aug 23 13:05:48 2019 +0200
+++ b/src/th06/anm0.rs	Fri Aug 23 15:34:03 2019 +0200
@@ -70,11 +70,8 @@
 
 impl Anm0 {
     /// Parse a slice of bytes into an `Anm0` struct.
-    pub fn from_slice(data: &[u8]) -> Result<Anm0, ()> {
-        // XXX: report the exact nom error instead!
-        let (_, anm0) = parse_anm0(data).or_else(|_| Err(()))?;
-        assert_eq!(anm0.len(), 1);
-        Ok(anm0[0].clone())
+    pub fn from_slice(data: &[u8]) -> IResult<&[u8], Vec<Anm0>> {
+        parse_anm0(data)
     }
 
     /// TODO
@@ -318,7 +315,9 @@
         let mut file = io::BufReader::new(file);
         let mut buf = vec![];
         file.read_to_end(&mut buf).unwrap();
-        let anm0 = Anm0::from_slice(&buf).unwrap();
+        let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+        assert_eq!(anms.len(), 1);
+        let anm0 = anms.pop().unwrap();
         assert_eq!(anm0.size, (256, 256));
         assert_eq!(anm0.format, 5);
     }
--- a/src/th06/anm0_vm.rs	Fri Aug 23 13:05:48 2019 +0200
+++ b/src/th06/anm0_vm.rs	Fri Aug 23 15:34:03 2019 +0200
@@ -444,7 +444,8 @@
         let mut file = io::BufReader::new(file);
         let mut buf = vec![];
         file.read_to_end(&mut buf).unwrap();
-        let anm0 = Anm0::from_slice(&buf).unwrap();
+        let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+        let anm0 = anms.pop().unwrap();
         assert_eq!(anm0.size, (256, 256));
         assert_eq!(anm0.format, 5);
         let sprite = Rc::new(RefCell::new(Sprite::new()));
--- a/src/th06/ecl_vm.rs	Fri Aug 23 13:05:48 2019 +0200
+++ b/src/th06/ecl_vm.rs	Fri Aug 23 15:34:03 2019 +0200
@@ -977,7 +977,8 @@
         let mut file = io::BufReader::new(file);
         let mut buf = vec![];
         file.read_to_end(&mut buf).unwrap();
-        let anm0 = Anm0::from_slice(&buf).unwrap();
+        let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+        let anm0 = anms.pop().unwrap();
         let anm0 = Rc::new(RefCell::new(anm0));
         let prng = Rc::new(RefCell::new(Prng::new(0)));
         let game = Game::new(prng, Rank::Easy);
--- a/src/th06/enemy.rs	Fri Aug 23 13:05:48 2019 +0200
+++ b/src/th06/enemy.rs	Fri Aug 23 15:34:03 2019 +0200
@@ -501,7 +501,8 @@
         let mut file = io::BufReader::new(file);
         let mut buf = vec![];
         file.read_to_end(&mut buf).unwrap();
-        let anm0 = Anm0::from_slice(&buf).unwrap();
+        let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+        let anm0 = anms.pop().unwrap();
         let anm0 = Rc::new(RefCell::new(anm0));
         let prng = Rc::new(RefCell::new(Prng::new(0)));
         let game = Game::new(prng, Rank::Easy);