comparison src/th06/anm0.rs @ 755:fc937d93a57c

Bump nom to version 6, and image to 0.23.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 04 Jan 2021 21:14:02 +0100
parents 258f4aebf3fc
children
comparison
equal deleted inserted replaced
754:a6875f90c141 755:fc937d93a57c
86 fn parse_name(i: &[u8]) -> IResult<&[u8], String> { 86 fn parse_name(i: &[u8]) -> IResult<&[u8], String> {
87 let (_, slice) = take_while_m_n(0, 32, |c| c != 0)(i)?; 87 let (_, slice) = take_while_m_n(0, 32, |c| c != 0)(i)?;
88 let string = match String::from_utf8(slice.to_vec()) { 88 let string = match String::from_utf8(slice.to_vec()) {
89 Ok(string) => string, 89 Ok(string) => string,
90 // XXX: use a more specific error instead. 90 // XXX: use a more specific error instead.
91 Err(_) => return Err(nom::Err::Failure((i, nom::error::ErrorKind::Eof))) 91 Err(_) => return Err(nom::Err::Failure(nom::error::Error::new(i, nom::error::ErrorKind::Eof)))
92 }; 92 };
93 Ok((i, string)) 93 Ok((i, string))
94 } 94 }
95 95
96 fn parse_sprite(i: &[u8]) -> IResult<&[u8], Sprite> { 96 fn parse_sprite(i: &[u8]) -> IResult<&[u8], Sprite> {
125 )* 125 )*
126 Instruction::$name($($arg),*) 126 Instruction::$name($($arg),*)
127 } 127 }
128 )* 128 )*
129 // XXX: use a more specific error instead. 129 // XXX: use a more specific error instead.
130 _ => return Err(nom::Err::Failure((i, nom::error::ErrorKind::Eof))) 130 _ => return Err(nom::Err::Failure(nom::error::Error::new(i, nom::error::ErrorKind::Eof)))
131 }; 131 };
132 Ok((i, instr)) 132 Ok((i, instr))
133 } 133 }
134 }; 134 };
135 } 135 }
183 let (i, sprite_offsets) = many_m_n(num_sprites, num_sprites, le_u32)(i)?; 183 let (i, sprite_offsets) = many_m_n(num_sprites, num_sprites, le_u32)(i)?;
184 let (_, script_offsets) = many_m_n(num_scripts, num_scripts, tuple((le_u32, le_u32)))(i)?; 184 let (_, script_offsets) = many_m_n(num_scripts, num_scripts, tuple((le_u32, le_u32)))(i)?;
185 185
186 let png_filename = if first_name_offset > 0 { 186 let png_filename = if first_name_offset > 0 {
187 if input.len() < first_name_offset as usize { 187 if input.len() < first_name_offset as usize {
188 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); 188 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
189 } 189 }
190 let i = &input[first_name_offset as usize..]; 190 let i = &input[first_name_offset as usize..];
191 let (_, name) = parse_name(i)?; 191 let (_, name) = parse_name(i)?;
192 name 192 name
193 } else { 193 } else {
194 String::new() 194 String::new()
195 }; 195 };
196 196
197 let alpha_filename = if second_name_offset > 0 { 197 let alpha_filename = if second_name_offset > 0 {
198 if input.len() < second_name_offset as usize { 198 if input.len() < second_name_offset as usize {
199 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); 199 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
200 } 200 }
201 let i = &input[second_name_offset as usize..]; 201 let i = &input[second_name_offset as usize..];
202 let (_, name) = parse_name(i)?; 202 let (_, name) = parse_name(i)?;
203 Some(name) 203 Some(name)
204 } else { 204 } else {
207 207
208 let mut sprites = vec![]; 208 let mut sprites = vec![];
209 let mut i = &input[..]; 209 let mut i = &input[..];
210 for offset in sprite_offsets.into_iter().map(|x| x as usize) { 210 for offset in sprite_offsets.into_iter().map(|x| x as usize) {
211 if input.len() < offset { 211 if input.len() < offset {
212 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); 212 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
213 } 213 }
214 i = &input[offset..]; 214 i = &input[offset..];
215 let (_, sprite) = parse_sprite(i)?; 215 let (_, sprite) = parse_sprite(i)?;
216 sprites.push(sprite); 216 sprites.push(sprite);
217 } 217 }
218 218
219 let mut scripts = HashMap::new(); 219 let mut scripts = HashMap::new();
220 for (index, offset) in script_offsets.into_iter().map(|(index, offset)| (index as u8, offset as usize)) { 220 for (index, offset) in script_offsets.into_iter().map(|(index, offset)| (index as u8, offset as usize)) {
221 if input.len() < offset { 221 if input.len() < offset {
222 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); 222 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
223 } 223 }
224 i = &input[offset..]; 224 i = &input[offset..];
225 let mut instruction_offsets = vec![]; 225 let mut instruction_offsets = vec![];
226 226
227 let mut instructions = vec![]; 227 let mut instructions = vec![];
245 let result = instruction_offsets.binary_search(&(*offset as usize)); 245 let result = instruction_offsets.binary_search(&(*offset as usize));
246 match result { 246 match result {
247 Ok(ptr) => *offset = ptr as u32, 247 Ok(ptr) => *offset = ptr as u32,
248 Err(ptr) => { 248 Err(ptr) => {
249 // XXX: use a more specific error instead. 249 // XXX: use a more specific error instead.
250 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); 250 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
251 //println!("Instruction offset not found for pointer: {}", ptr); 251 //println!("Instruction offset not found for pointer: {}", ptr);
252 } 252 }
253 } 253 }
254 } 254 }
255 Instruction::InterruptLabel(interrupt) => { 255 Instruction::InterruptLabel(interrupt) => {