1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
use half::f16;
use nom::number::streaming::{
  be_u16 as parse_be_u16, le_i16 as parse_le_i16, le_i32 as parse_le_i32, le_u16 as parse_le_u16, le_u8 as parse_u8,
};
use nom::{IResult as NomResult, Needed};
use swf_fixed::{Sfixed16P16, Sfixed8P8, Ufixed8P8};
use swf_types as swf;
use swf_types::LanguageCode;
use std::num::NonZeroUsize;

/// Parse the bit-encoded representation of a bool (1 bit)
pub fn parse_bool_bits((input_slice, bit_pos): (&[u8], usize)) -> NomResult<(&[u8], usize), bool> {
  if input_slice.is_empty() {
    Err(::nom::Err::Incomplete(Needed::Size(NonZeroUsize::new(1).unwrap())))
  } else {
    let res: bool = input_slice[0] & (1 << (7 - bit_pos)) > 0;
    if bit_pos == 7 {
      Ok(((&input_slice[1..], 0), res))
    } else {
      Ok(((input_slice, bit_pos + 1), res))
    }
  }
}

/// Parse a sequence of bytes up to the end of input or first nul-byte. If there
/// is a nul-byte, it is consumed but not included in the result.
// TODO: Move to `crate::complete::basic_data_types`
// TODO: Check encoding (assuming UTF-8)
pub fn parse_block_c_string(input: &[u8]) -> NomResult<&[u8], String> {
  let raw = match memchr::memchr(0, input) {
    Some(idx) => &input[0..idx],
    None => input,
  };

  match std::str::from_utf8(raw) {
    Ok(checked) => Ok((&[], checked.to_string())),
    Err(_) => Err(nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Verify))),
  }
}

/// Parse a null-terminated sequence of bytes. The nul-byte is consumed but not included in the
/// result.
pub fn parse_c_string(input: &[u8]) -> NomResult<&[u8], String> {
  const NUL_BYTE: &[u8] = b"\x00";

  let (input, raw) = nom::bytes::streaming::take_until(NUL_BYTE)(input)?;
  let (input, _) = nom::bytes::streaming::take(NUL_BYTE.len())(input)?;

  match std::str::from_utf8(raw) {
    Ok(checked) => Ok((input, checked.to_string())),
    Err(_) => Err(nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Verify))),
  }
}

/// Parse the variable-length encoded little-endian representation of an unsigned 32-bit integer
pub fn parse_leb128_u32(input: &[u8]) -> NomResult<&[u8], u32> {
  let mut result: u32 = 0;
  let mut current_input: &[u8] = input;
  for i in 0..5 {
    match parse_u8(current_input) {
      Ok((next_input, next_byte)) => {
        result |= ((next_byte as u32) & 0x7f) << (7 * i);
        if next_byte & (1 << 7) == 0 {
          return Ok((next_input, result));
        } else {
          current_input = next_input;
        }
      }
      Err(e) => return Err(e),
    }
  }
  Ok((current_input, result))
}

/// Parse the bit-encoded big-endian representation of a signed fixed-point 16.16-bit number
pub fn parse_fixed16_p16_bits(input: (&[u8], usize), n: usize) -> NomResult<(&[u8], usize), Sfixed16P16> {
  use nom::combinator::map;
  map(do_parse_i32_bits(n), Sfixed16P16::from_epsilons)(input)
}

/// Parse the bit-encoded big-endian representation of a signed fixed-point 8.8-bit number
pub fn parse_fixed8_p8_bits(input: (&[u8], usize), n: usize) -> NomResult<(&[u8], usize), Sfixed8P8> {
  use nom::combinator::map;
  map(do_parse_i16_bits(n), Sfixed8P8::from_epsilons)(input)
}

/// Generates a bits parser reading a `i16` over `n` bits.
pub fn do_parse_i16_bits(n: usize) -> impl Fn((&[u8], usize)) -> NomResult<(&[u8], usize), i16> {
  debug_assert!(n <= 16);
  move |input: (&[u8], usize)| {
    let (input, x) = nom::bits::streaming::take::<_, u16, _, _>(n)(input)?;
    let x = match n {
      0 => 0,
      16 => x as i16,
      _ => {
        if x >> (n - 1) > 0 {
          -1i16 << (n - 1) | (x as i16)
        } else {
          x as i16
        }
      }
    };
    Ok((input, x))
  }
}

/// Parse the bit-encoded big-endian representation of a signed 16-bit integer
pub fn parse_i16_bits(input: (&[u8], usize), n: usize) -> NomResult<(&[u8], usize), i16> {
  do_parse_i16_bits(n)(input)
}

/// Generates a bits parser reading a `i32` over `n` bits.
pub fn do_parse_i32_bits(n: usize) -> impl Fn((&[u8], usize)) -> NomResult<(&[u8], usize), i32> {
  debug_assert!(n <= 32);
  move |input: (&[u8], usize)| {
    let (input, x) = nom::bits::streaming::take::<_, u32, _, _>(n)(input)?;
    let x = match n {
      0 => 0,
      32 => x as i32,
      _ => {
        if x >> (n - 1) > 0 {
          -1i32 << (n - 1) | (x as i32)
        } else {
          x as i32
        }
      }
    };
    Ok((input, x))
  }
}

pub fn parse_i32_bits(input: (&[u8], usize), n: usize) -> NomResult<(&[u8], usize), i32> {
  do_parse_i32_bits(n)(input)
}

/// Generates a bits parser reading a `u32` over `n` bits.
pub fn do_parse_u32_bits(n: usize) -> impl Fn((&[u8], usize)) -> NomResult<(&[u8], usize), u32> {
  debug_assert!(n <= 32);
  move |input: (&[u8], usize)| nom::bits::streaming::take::<_, u32, _, _>(n)(input)
}

pub fn parse_u32_bits(input: (&[u8], usize), n: usize) -> NomResult<(&[u8], usize), u32> {
  do_parse_u32_bits(n)(input)
}

pub fn parse_be_f16(input: &[u8]) -> NomResult<&[u8], f32> {
  use nom::combinator::map;
  map(parse_be_u16, transmute_u16_to_f16)(input)
}

pub fn parse_le_f16(input: &[u8]) -> NomResult<&[u8], f32> {
  use nom::combinator::map;
  map(parse_le_u16, transmute_u16_to_f16)(input)
}

fn transmute_u16_to_f16(bits: u16) -> f32 {
  f16::from_bits(bits).to_f32()
}

/// Parse the little-endian representation of an unsigned fixed-point 8.8-bit number
pub fn parse_le_ufixed8_p8(input: &[u8]) -> NomResult<&[u8], Ufixed8P8> {
  use nom::combinator::map;
  map(parse_le_u16, Ufixed8P8::from_epsilons)(input)
}

/// Parse the little-endian representation of a signed fixed-point 8.8-bit number
pub fn parse_le_fixed8_p8(input: &[u8]) -> NomResult<&[u8], Sfixed8P8> {
  use nom::combinator::map;
  map(parse_le_i16, Sfixed8P8::from_epsilons)(input)
}

/// Parse the little-endian representation of a signed fixed-point 16.16-bit number
pub fn parse_le_fixed16_p16(input: &[u8]) -> NomResult<&[u8], Sfixed16P16> {
  use nom::combinator::map;
  map(parse_le_i32, Sfixed16P16::from_epsilons)(input)
}

pub fn parse_rect(input: &[u8]) -> NomResult<&[u8], swf::Rect> {
  use nom::bits::bits;
  bits(parse_rect_bits)(input)
}

pub fn parse_rect_bits(input: (&[u8], usize)) -> NomResult<(&[u8], usize), swf::Rect> {
  use nom::combinator::map;

  let (input, n_bits) = map(do_parse_u16_bits(5), |x| x as usize)(input)?;
  let (input, x_min) = parse_i32_bits(input, n_bits)?;
  let (input, x_max) = parse_i32_bits(input, n_bits)?;
  let (input, y_min) = parse_i32_bits(input, n_bits)?;
  let (input, y_max) = parse_i32_bits(input, n_bits)?;
  Ok((
    input,
    swf::Rect {
      x_min,
      x_max,
      y_min,
      y_max,
    },
  ))
}

pub fn parse_s_rgb8(input: &[u8]) -> NomResult<&[u8], swf::SRgb8> {
  let (input, r) = parse_u8(input)?;
  let (input, g) = parse_u8(input)?;
  let (input, b) = parse_u8(input)?;
  Ok((input, swf::SRgb8 { r, g, b }))
}

pub fn parse_straight_s_rgba8(input: &[u8]) -> NomResult<&[u8], swf::StraightSRgba8> {
  let (input, r) = parse_u8(input)?;
  let (input, g) = parse_u8(input)?;
  let (input, b) = parse_u8(input)?;
  let (input, a) = parse_u8(input)?;
  Ok((input, swf::StraightSRgba8 { r, g, b, a }))
}

/// Skip `n` bits
pub fn skip_bits((input_slice, bit_pos): (&[u8], usize), n: usize) -> NomResult<(&[u8], usize), ()> {
  let slice_len: usize = input_slice.len();
  let available_bits: usize = 8 * slice_len - bit_pos;
  let skipped_full_bytes = (bit_pos + n) / 8;
  let final_bit_pos = (bit_pos + n) % 8;
  if available_bits < n {
    let needed_bytes = skipped_full_bytes + if final_bit_pos > 0 { 1 } else { 0 };
    Err(::nom::Err::Incomplete(Needed::Size(NonZeroUsize::new(needed_bytes).unwrap())))
  } else {
    Ok(((&input_slice[skipped_full_bytes..], final_bit_pos), ()))
  }
}

/// Generates a bits parser reading a `u16` over `n` bits.
pub fn do_parse_u16_bits(n: usize) -> impl Fn((&[u8], usize)) -> NomResult<(&[u8], usize), u16> {
  move |input: (&[u8], usize)| nom::bits::streaming::take::<_, u16, _, _>(n)(input)
}

/// Parse the bit-encoded big-endian representation of an unsigned 16-bit integer
pub fn parse_u16_bits(input: (&[u8], usize), n: usize) -> NomResult<(&[u8], usize), u16> {
  do_parse_u16_bits(n)(input)
}

#[allow(unused_variables)]
pub fn parse_language_code(input: &[u8]) -> NomResult<&[u8], swf::LanguageCode> {
  let (input, code) = parse_u8(input)?;
  let lang: LanguageCode = match code {
    0 => swf::LanguageCode::Auto,
    1 => swf::LanguageCode::Latin,
    2 => swf::LanguageCode::Japanese,
    3 => swf::LanguageCode::Korean,
    4 => swf::LanguageCode::SimplifiedChinese,
    5 => swf::LanguageCode::TraditionalChinese,
    _ => return Err(nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Switch))),
  };
  Ok((input, lang))
}

pub fn parse_matrix(input: &[u8]) -> NomResult<&[u8], swf::Matrix> {
  use nom::bits::bits;
  bits(parse_matrix_bits)(input)
}

pub fn parse_matrix_bits(input: (&[u8], usize)) -> NomResult<(&[u8], usize), swf::Matrix> {
  let (input, has_scale) = parse_bool_bits(input)?;
  let (input, (scale_x, scale_y)) = if has_scale {
    let (input, scale_bits) = parse_u16_bits(input, 5)?;
    let (input, scale_x) = parse_fixed16_p16_bits(input, scale_bits as usize)?;
    let (input, scale_y) = parse_fixed16_p16_bits(input, scale_bits as usize)?;
    (input, (scale_x, scale_y))
  } else {
    (input, (Sfixed16P16::ONE, Sfixed16P16::ONE))
  };
  let (input, has_skew) = parse_bool_bits(input)?;
  let (input, (rotate_skew0, rotate_skew1)) = if has_skew {
    let (input, skew_bits) = parse_u16_bits(input, 5)?;
    let (input, skew0) = parse_fixed16_p16_bits(input, skew_bits as usize)?;
    let (input, skew1) = parse_fixed16_p16_bits(input, skew_bits as usize)?;
    (input, (skew0, skew1))
  } else {
    (input, (Sfixed16P16::ZERO, Sfixed16P16::ZERO))
  };
  let (input, translate_bits) = parse_u16_bits(input, 5)?;
  let (input, translate_x) = parse_i32_bits(input, translate_bits as usize)?;
  let (input, translate_y) = parse_i32_bits(input, translate_bits as usize)?;
  Ok((
    input,
    swf::Matrix {
      scale_x,
      scale_y,
      rotate_skew0,
      rotate_skew1,
      translate_x,
      translate_y,
    },
  ))
}

pub fn parse_named_id(input: &[u8]) -> NomResult<&[u8], swf::NamedId> {
  let (input, id) = parse_le_u16(input)?;
  let (input, name) = parse_c_string(input)?;
  Ok((input, swf::NamedId { id, name }))
}

pub fn parse_color_transform(input: &[u8]) -> NomResult<&[u8], swf::ColorTransform> {
  use nom::bits::bits;
  bits(parse_color_transform_bits)(input)
}

#[allow(unused_variables)]
pub fn parse_color_transform_bits(input: (&[u8], usize)) -> NomResult<(&[u8], usize), swf::ColorTransform> {
  let (input, has_add) = parse_bool_bits(input)?;
  let (input, has_mult) = parse_bool_bits(input)?;
  let (input, n_bits) = parse_u16_bits(input, 4)?;
  let (input, mult) = if has_mult {
    let (input, r) = parse_fixed8_p8_bits(input, n_bits as usize)?;
    let (input, g) = parse_fixed8_p8_bits(input, n_bits as usize)?;
    let (input, b) = parse_fixed8_p8_bits(input, n_bits as usize)?;
    (input, (r, g, b))
  } else {
    (input, (Sfixed8P8::ONE, Sfixed8P8::ONE, Sfixed8P8::ONE))
  };
  let (input, add) = if has_add {
    let (input, r) = parse_i16_bits(input, n_bits as usize)?;
    let (input, g) = parse_i16_bits(input, n_bits as usize)?;
    let (input, b) = parse_i16_bits(input, n_bits as usize)?;
    (input, (r, g, b))
  } else {
    (input, (0, 0, 0))
  };
  Ok((
    input,
    swf::ColorTransform {
      red_mult: mult.0,
      green_mult: mult.1,
      blue_mult: mult.2,
      red_add: add.0,
      green_add: add.1,
      blue_add: add.2,
    },
  ))
}

pub fn parse_color_transform_with_alpha(input: &[u8]) -> NomResult<&[u8], swf::ColorTransformWithAlpha> {
  use nom::bits::bits;
  bits(parse_color_transform_with_alpha_bits)(input)
}

#[allow(unused_variables)]
pub fn parse_color_transform_with_alpha_bits(
  input: (&[u8], usize),
) -> NomResult<(&[u8], usize), swf::ColorTransformWithAlpha> {
  let (input, has_add) = parse_bool_bits(input)?;
  let (input, has_mult) = parse_bool_bits(input)?;
  let (input, n_bits) = parse_u16_bits(input, 4)?;
  let (input, mult) = if has_mult {
    let (input, r) = parse_fixed8_p8_bits(input, n_bits as usize)?;
    let (input, g) = parse_fixed8_p8_bits(input, n_bits as usize)?;
    let (input, b) = parse_fixed8_p8_bits(input, n_bits as usize)?;
    let (input, a) = parse_fixed8_p8_bits(input, n_bits as usize)?;
    (input, (r, g, b, a))
  } else {
    (input, (Sfixed8P8::ONE, Sfixed8P8::ONE, Sfixed8P8::ONE, Sfixed8P8::ONE))
  };
  let (input, add) = if has_add {
    let (input, r) = parse_i16_bits(input, n_bits as usize)?;
    let (input, g) = parse_i16_bits(input, n_bits as usize)?;
    let (input, b) = parse_i16_bits(input, n_bits as usize)?;
    let (input, a) = parse_i16_bits(input, n_bits as usize)?;
    (input, (r, g, b, a))
  } else {
    (input, (0, 0, 0, 0))
  };
  Ok((
    input,
    swf::ColorTransformWithAlpha {
      red_mult: mult.0,
      green_mult: mult.1,
      blue_mult: mult.2,
      alpha_mult: mult.3,
      red_add: add.0,
      green_add: add.1,
      blue_add: add.2,
      alpha_add: add.3,
    },
  ))
}

#[cfg(test)]
mod tests {
  use nom::Needed;

  use super::*;

  #[test]
  fn test_parse_encoded_le_u32() {
    {
      assert_eq!(parse_leb128_u32(&[][..]), Err(::nom::Err::Incomplete(Needed::Size(NonZeroUsize::new(1).unwrap()))));
    }
    {
      let input = vec![0x00];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[1..], 0)));
    }
    {
      let input = vec![0x01];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[1..], 1)));
    }
    {
      let input = vec![0x10];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[1..], 16)));
    }
    {
      let input = vec![0x7f];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[1..], 127)));
    }
    {
      let input = vec![0x80];
      assert_eq!(
        parse_leb128_u32(&input[..]),
        Err(::nom::Err::Incomplete(Needed::Size(NonZeroUsize::new(1).unwrap())))
      );
    }
    {
      let input = vec![0x80, 0x01];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[2..], 1 << 7)));
    }
    {
      let input = vec![0x80, 0x80, 0x01];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[3..], 1 << 14)));
    }
    {
      let input = vec![0x80, 0x80, 0x80, 0x01];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[4..], 1 << 21)));
    }
    {
      let input = vec![0x80, 0x80, 0x80, 0x80];
      assert_eq!(
        parse_leb128_u32(&input[..]),
        Err(::nom::Err::Incomplete(Needed::Size(NonZeroUsize::new(1).unwrap())))
      );
    }
    {
      let input = vec![0x80, 0x80, 0x80, 0x80, 0x01];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[5..], 1 << 28)));
    }
    {
      // Do not extend pswf 5 bytes
      let input = vec![0x80, 0x80, 0x80, 0x80, 0x80];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[5..], 0)));
    }
    {
      // Do not extend pswf 5 bytes
      let input = vec![0x80, 0x80, 0x80, 0x80, 0x80, 0x01];
      assert_eq!(parse_leb128_u32(&input[..]), Ok((&input[5..], 0)));
    }
  }

  #[test]
  fn test_parse_i16_bits() {
    {
      let input = vec![0b0000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 0), Ok(((&input[0..], 0), 0)));
    }
    {
      let input = vec![0b0000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 1), Ok(((&input[0..], 1), 0)));
    }
    {
      let input = vec![0b1000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 1), Ok(((&input[0..], 1), -1)));
    }
    {
      let input = vec![0b0000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 2), Ok(((&input[0..], 2), 0)));
    }
    {
      let input = vec![0b0100_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 2), Ok(((&input[0..], 2), 1)));
    }
    {
      let input = vec![0b1000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 2), Ok(((&input[0..], 2), -2)));
    }
    {
      let input = vec![0b1100_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 2), Ok(((&input[0..], 2), -1)));
    }
    {
      let input = vec![0b0000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 15), Ok(((&input[1..], 7), 0)));
    }
    {
      let input = vec![0b0111_1111, 0b1111_1110];
      assert_eq!(parse_i16_bits((&input[..], 0), 15), Ok(((&input[1..], 7), 16383)));
    }
    {
      let input = vec![0b1000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 15), Ok(((&input[1..], 7), -16384)));
    }
    {
      let input = vec![0b1111_1111, 0b1111_1110];
      assert_eq!(parse_i16_bits((&input[..], 0), 15), Ok(((&input[1..], 7), -1)));
    }
    {
      let input = vec![0b0000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 16), Ok(((&input[2..], 0), 0)));
    }
    {
      let input = vec![0b0111_1111, 0b1111_1111];
      assert_eq!(parse_i16_bits((&input[..], 0), 16), Ok(((&input[2..], 0), 32767)));
    }
    {
      let input = vec![0b1000_0000, 0b0000_0000];
      assert_eq!(parse_i16_bits((&input[..], 0), 16), Ok(((&input[2..], 0), -32768)));
    }
    {
      let input = vec![0b1111_1111, 0b1111_1111];
      assert_eq!(parse_i16_bits((&input[..], 0), 16), Ok(((&input[2..], 0), -1)));
    }
  }

  #[test]
  fn test_parse_u16_bits() {
    let input = vec![0b1010_1010, 0b1111_0000, 0b0011_0011];
    assert_eq!(parse_u16_bits((&input[..], 0), 5), Ok(((&input[0..], 5), 21)));
  }

  #[test]
  fn test_parse_fixed16_p16_bits() {
    let input = vec![0b0000_0000, 0b0000_0000, 0b0000_0000, 0b0000_0000];
    assert_eq!(
      parse_fixed16_p16_bits((&input[..], 0), 32),
      Ok(((&input[4..], 0), Sfixed16P16::from_epsilons(0)))
    );
  }

  #[test]
  fn test_parse_rect() {
    {
      // This is the example in the spec, but the spec has an error: the binary
      // representations of x_min and x_max are swapped
      // 01011 00001111111 00100000100 00000001111 01000000010
      // nBits xMin        xMax        yMin        yMax
      let input = vec![
        0b0101_1000,
        0b0111_1111,
        0b0010_0000,
        0b1000_0000,
        0b0011_1101,
        0b0000_0001,
        0b0000_0000,
      ];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 127,
            x_max: 260,
            y_min: 15,
            y_max: 514,
          }
        ))
      );
    }
    {
      let input = vec![0b0000_0000];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 0,
            x_max: 0,
            y_min: 0,
            y_max: 0,
          }
        ))
      );
    }
    {
      let input = vec![0b0000_1000, 0b0000_0000];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 0,
            x_max: 0,
            y_min: 0,
            y_max: 0,
          }
        ))
      );
    }
    {
      let input = vec![0b0001_0000, 0b0000_0000];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 0,
            x_max: 0,
            y_min: 0,
            y_max: 0,
          }
        ))
      );
    }
    {
      let input = vec![0b0001_0010, 0b0000_0000];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 1,
            x_max: 0,
            y_min: 0,
            y_max: 0,
          }
        ))
      );
    }
    {
      let input = vec![0b0001_0000, 0b1000_0000];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 0,
            x_max: 1,
            y_min: 0,
            y_max: 0,
          }
        ))
      );
    }
    {
      let input = vec![0b0001_0000, 0b0010_0000];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 0,
            x_max: 0,
            y_min: 1,
            y_max: 0,
          }
        ))
      );
    }
    {
      let input = vec![0b0001_0000, 0b0000_1000];
      assert_eq!(
        parse_rect(&input[..]),
        Ok((
          (&[][..]),
          swf::Rect {
            x_min: 0,
            x_max: 0,
            y_min: 0,
            y_max: 1,
          }
        ))
      );
    }
  }
}