From 52e1b71038ab7373d51b7e6b3aa8013001a1a992 Mon Sep 17 00:00:00 2001 From: _ <> Date: Thu, 9 Jan 2020 00:27:46 +0000 Subject: [PATCH] Parsing a few header fields from nom --- src/main.rs | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8959838..77a7240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ use nom::{ IResult, - bytes::complete::{tag, take_while_m_n}, - combinator::map_res, - sequence::tuple + bytes::complete::{tag}, + number::complete::{le_u32}, }; use sdl2::event::Event; @@ -16,14 +15,27 @@ use std::io::Read; use std::path::Path; use std::time::Duration; -pub fn parse_iqm (input: &[u8]) -> IResult<&[u8], ()> { - let (input, _) = tag (b"INTERQUAKEMODEL\0")(input)?; - Ok ((input, ())) +pub struct Iqm { + version: u32, + filesize: u32, + flags: u32, } -pub struct IqmBuffer { - data: Vec , +pub fn parse_iqm (input: &[u8]) -> IResult<&[u8], Iqm> { + let (input, _) = tag (b"INTERQUAKEMODEL\0")(input)?; + let (input, version) = le_u32 (input)?; + // I only know how to parse version 2 + assert_eq! (version, 2); + + let (input, filesize) = le_u32 (input)?; + let (input, flags) = le_u32 (input)?; + + Ok ((input, Iqm { + version, + filesize, + flags, + })) } pub fn load_small_file

(name: P) -> Vec @@ -43,16 +55,6 @@ where P: AsRef data } -impl IqmBuffer { - pub fn from_buffer (data: Vec ) -> IqmBuffer { - assert_eq! (parse_iqm (&data [..]), Ok ((&data [16..], ()))); - - IqmBuffer { - data, - } - } -} - const VERT_SHADER_SRC: &str = " #define lowp @@ -361,8 +363,10 @@ mod tests { #[test] pub fn iqm () { - assert_eq! (parse_iqm (b"INTERQUAKEMODEL\0"), Ok ((&b"" [..], ()))); + let data = load_small_file ("pumpking.iqm"); + let model = parse_iqm (&data [..]).unwrap ().1; - IqmBuffer::from_buffer (load_small_file ("pumpking.iqm")); + assert_eq! (model.filesize, 90368); + assert_eq! (model.flags, 0); } }