Parsing a few header fields from nom

main
_ 2020-01-09 00:27:46 +00:00
parent 8b25b2fadd
commit 52e1b71038
1 changed files with 24 additions and 20 deletions

View File

@ -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 <u8>,
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 <P> (name: P) -> Vec <u8>
@ -43,16 +55,6 @@ where P: AsRef <Path>
data
}
impl IqmBuffer {
pub fn from_buffer (data: Vec <u8>) -> 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);
}
}