@@ -27,7 +27,7 @@ use super::{FORMAT_UNCOMPRESSED_PCM, FORMAT_EXTENDED};
27
27
#[ derive( Debug ) ]
28
28
pub enum ReadError {
29
29
/// The file format is incorrect or unsupported.
30
- Format ( FormatErrorKind ) ,
30
+ Format ( ReadErrorKind ) ,
31
31
/// An IO error occurred.
32
32
Io ( io:: Error ) ,
33
33
}
@@ -46,7 +46,7 @@ impl fmt::Display for ReadError {
46
46
47
47
/// Represents a file format error, when the wave file is incorrect or unsupported.
48
48
#[ derive( Debug ) ]
49
- pub enum FormatErrorKind {
49
+ pub enum ReadErrorKind {
50
50
/// The file does not start with a "RIFF" tag and chunk size.
51
51
NotARiffFile ,
52
52
/// The file doesn't continue with "WAVE" after the RIFF chunk header.
@@ -66,24 +66,24 @@ pub enum FormatErrorKind {
66
66
InvalidBitsPerSample ( u16 , u16 ) ,
67
67
}
68
68
69
- impl FormatErrorKind {
69
+ impl ReadErrorKind {
70
70
fn to_string ( & self ) -> & str {
71
71
match * self {
72
- FormatErrorKind :: NotARiffFile => "not a RIFF file" ,
73
- FormatErrorKind :: NotAWaveFile => "not a WAVE file" ,
74
- FormatErrorKind :: NotAnUncompressedPcmWaveFile ( _) => "Not an uncompressed wave file" ,
75
- FormatErrorKind :: FmtChunkTooShort => "fmt_ chunk is too short" ,
76
- FormatErrorKind :: NumChannelsIsZero => "Number of channels is zero" ,
77
- FormatErrorKind :: SampleRateIsZero => "Sample rate is zero" ,
78
- FormatErrorKind :: UnsupportedBitsPerSample ( _) => "Unsupported bits per sample" ,
79
- FormatErrorKind :: InvalidBitsPerSample ( _, _) => {
72
+ ReadErrorKind :: NotARiffFile => "not a RIFF file" ,
73
+ ReadErrorKind :: NotAWaveFile => "not a WAVE file" ,
74
+ ReadErrorKind :: NotAnUncompressedPcmWaveFile ( _) => "Not an uncompressed wave file" ,
75
+ ReadErrorKind :: FmtChunkTooShort => "fmt_ chunk is too short" ,
76
+ ReadErrorKind :: NumChannelsIsZero => "Number of channels is zero" ,
77
+ ReadErrorKind :: SampleRateIsZero => "Sample rate is zero" ,
78
+ ReadErrorKind :: UnsupportedBitsPerSample ( _) => "Unsupported bits per sample" ,
79
+ ReadErrorKind :: InvalidBitsPerSample ( _, _) => {
80
80
"A bits per sample of less than the container size is not currently supported"
81
81
}
82
82
}
83
83
}
84
84
}
85
85
86
- impl fmt:: Display for FormatErrorKind {
86
+ impl fmt:: Display for ReadErrorKind {
87
87
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
88
88
write ! ( f, "{}" , self . to_string( ) )
89
89
}
@@ -117,20 +117,20 @@ fn validate_pcm_format(format: u16) -> ReadResult<Format> {
117
117
match format {
118
118
FORMAT_UNCOMPRESSED_PCM => Ok ( Format :: UncompressedPcm ) ,
119
119
FORMAT_EXTENDED => Ok ( Format :: Extended ) ,
120
- _ => Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( format) ) ) ,
120
+ _ => Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( format) ) ) ,
121
121
}
122
122
}
123
123
124
124
fn validate_pcm_subformat ( sub_format : u16 ) -> ReadResult < ( ) > {
125
125
match sub_format {
126
126
FORMAT_UNCOMPRESSED_PCM => Ok ( ( ) ) ,
127
- _ => Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( sub_format) ) ) ,
127
+ _ => Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( sub_format) ) ) ,
128
128
}
129
129
}
130
130
131
131
fn validate_fmt_header_is_large_enough ( size : u32 , min_size : u32 ) -> ReadResult < ( ) > {
132
132
if size < min_size {
133
- Err ( ReadError :: Format ( FormatErrorKind :: FmtChunkTooShort ) )
133
+ Err ( ReadError :: Format ( ReadErrorKind :: FmtChunkTooShort ) )
134
134
} else {
135
135
Ok ( ( ) )
136
136
}
@@ -161,13 +161,13 @@ trait ReadWaveExt: Read + Seek {
161
161
}
162
162
163
163
if num_channels == 0 {
164
- return Err ( ReadError :: Format ( FormatErrorKind :: NumChannelsIsZero ) ) ;
164
+ return Err ( ReadError :: Format ( ReadErrorKind :: NumChannelsIsZero ) ) ;
165
165
} else if sample_rate == 0 {
166
- return Err ( ReadError :: Format ( FormatErrorKind :: SampleRateIsZero ) ) ;
166
+ return Err ( ReadError :: Format ( ReadErrorKind :: SampleRateIsZero ) ) ;
167
167
} else if bits_per_sample != 8 && bits_per_sample != 16
168
168
&& bits_per_sample != 24 && bits_per_sample != 32 {
169
169
return Err ( ReadError :: Format (
170
- FormatErrorKind :: UnsupportedBitsPerSample ( bits_per_sample) ) ) ;
170
+ ReadErrorKind :: UnsupportedBitsPerSample ( bits_per_sample) ) ) ;
171
171
}
172
172
173
173
Ok ( PcmFormat {
@@ -189,7 +189,7 @@ trait ReadWaveExt: Read + Seek {
189
189
if sample_info != bits_per_sample {
190
190
// We don't currently support wave files where the bits per sample
191
191
// doesn't entirely fill the allocated bits per sample.
192
- return Err ( ReadError :: Format ( FormatErrorKind :: InvalidBitsPerSample ( bits_per_sample,
192
+ return Err ( ReadError :: Format ( ReadErrorKind :: InvalidBitsPerSample ( bits_per_sample,
193
193
sample_info) ) ) ;
194
194
}
195
195
@@ -205,7 +205,7 @@ trait ReadWaveExt: Read + Seek {
205
205
}
206
206
207
207
fn validate_is_riff_file ( & mut self ) -> ReadResult < ( ) > {
208
- try!( self . validate_tag ( b"RIFF" , FormatErrorKind :: NotARiffFile ) ) ;
208
+ try!( self . validate_tag ( b"RIFF" , ReadErrorKind :: NotARiffFile ) ) ;
209
209
// The next four bytes represent the chunk size. We're not going to
210
210
// validate it, so that we can still try to read files that might have
211
211
// an incorrect chunk size, so let's skip over it.
@@ -214,13 +214,13 @@ trait ReadWaveExt: Read + Seek {
214
214
}
215
215
216
216
fn validate_is_wave_file ( & mut self ) -> ReadResult < ( ) > {
217
- try!( self . validate_tag ( b"WAVE" , FormatErrorKind :: NotAWaveFile ) ) ;
217
+ try!( self . validate_tag ( b"WAVE" , ReadErrorKind :: NotAWaveFile ) ) ;
218
218
Ok ( ( ) )
219
219
}
220
220
221
221
fn validate_tag ( & mut self ,
222
222
expected_tag : & [ u8 ; 4 ] ,
223
- err_kind : FormatErrorKind )
223
+ err_kind : ReadErrorKind )
224
224
-> ReadResult < ( ) > {
225
225
let tag = try!( self . read_tag ( ) ) ;
226
226
if & tag != expected_tag {
@@ -267,6 +267,8 @@ pub struct WaveReader<T>
267
267
reader : T ,
268
268
}
269
269
270
+ // TODO what should we do if an incorrect read_* method is called? Return the error in the result?
271
+
270
272
impl < T > WaveReader < T >
271
273
where T : Read + Seek
272
274
{
@@ -321,7 +323,7 @@ mod tests {
321
323
322
324
use super :: super :: { FORMAT_UNCOMPRESSED_PCM , FORMAT_EXTENDED } ;
323
325
use super :: super :: { Format , PcmFormat } ;
324
- use super :: { FormatErrorKind , ReadError , ReadWaveExt , WaveReader } ;
326
+ use super :: { ReadError , ReadErrorKind , ReadWaveExt , WaveReader } ;
325
327
use super :: { validate_fmt_header_is_large_enough, validate_pcm_format, validate_pcm_subformat} ;
326
328
327
329
// This is a helper macro that helps us validate results in our tests.
@@ -349,14 +351,14 @@ mod tests {
349
351
#[ test]
350
352
fn test_validate_is_riff_file_err_incomplete ( ) {
351
353
let mut data = Cursor :: new ( b"RIF " ) ;
352
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotARiffFile ) ) ,
354
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotARiffFile ) ) ,
353
355
data. validate_is_riff_file( ) ) ;
354
356
}
355
357
356
358
#[ test]
357
359
fn test_validate_is_riff_file_err_something_else ( ) {
358
360
let mut data = Cursor :: new ( b"JPEG " ) ;
359
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotARiffFile ) ) ,
361
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotARiffFile ) ) ,
360
362
data. validate_is_riff_file( ) ) ;
361
363
}
362
364
@@ -371,14 +373,14 @@ mod tests {
371
373
#[ test]
372
374
fn test_validate_is_wave_file_err_incomplete ( ) {
373
375
let mut data = Cursor :: new ( b"WAV " ) ;
374
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAWaveFile ) ) ,
376
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAWaveFile ) ) ,
375
377
data. validate_is_wave_file( ) ) ;
376
378
}
377
379
378
380
#[ test]
379
381
fn test_validate_is_wave_file_err_something_else ( ) {
380
382
let mut data = Cursor :: new ( b"JPEG" ) ;
381
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAWaveFile ) ) ,
383
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAWaveFile ) ) ,
382
384
data. validate_is_wave_file( ) ) ;
383
385
}
384
386
@@ -436,7 +438,7 @@ mod tests {
436
438
437
439
#[ test]
438
440
fn test_validate_pcm_format_err_not_uncompressed ( ) {
439
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
441
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
440
442
validate_pcm_format( 12345 ) ) ;
441
443
}
442
444
@@ -449,13 +451,13 @@ mod tests {
449
451
450
452
#[ test]
451
453
fn test_validate_pcm_subformat_err_extended_format_value_not_valid_for_subformat ( ) {
452
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
454
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
453
455
validate_pcm_subformat( FORMAT_EXTENDED ) ) ;
454
456
}
455
457
456
458
#[ test]
457
459
fn test_validate_pcm_subformat_err_not_uncompressed ( ) {
458
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
460
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
459
461
validate_pcm_subformat( 12345 ) ) ;
460
462
}
461
463
@@ -473,7 +475,7 @@ mod tests {
473
475
474
476
#[ test]
475
477
fn test_validate_fmt_header_is_large_enough_too_small ( ) {
476
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: FmtChunkTooShort ) ) ,
478
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: FmtChunkTooShort ) ) ,
477
479
validate_fmt_header_is_large_enough( 14 , 16 ) ) ;
478
480
}
479
481
@@ -490,7 +492,7 @@ mod tests {
490
492
fn test_validate_pcm_header_fmt_chunk_too_small ( ) {
491
493
let mut data = Cursor :: new ( b"RIFF WAVE\
492
494
fmt \x0C \x00 \x00 \x00 ") ;
493
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: FmtChunkTooShort ) ) ,
495
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: FmtChunkTooShort ) ) ,
494
496
data. read_wave_header( ) ) ;
495
497
}
496
498
@@ -499,7 +501,7 @@ mod tests {
499
501
let mut data = Cursor :: new ( b"RIFF WAVE\
500
502
fmt \x0E \x00 \x00 \x00 \
501
503
\x01 \x00 ") ;
502
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: FmtChunkTooShort ) ) ,
504
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: FmtChunkTooShort ) ) ,
503
505
data. read_wave_header( ) ) ;
504
506
}
505
507
@@ -508,7 +510,7 @@ mod tests {
508
510
let mut data = Cursor :: new ( b"RIFF WAVE\
509
511
fmt \x0E \x00 \x00 \x00 \
510
512
\x02 \x00 ") ;
511
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
513
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
512
514
data. read_wave_header( ) ) ;
513
515
}
514
516
@@ -522,7 +524,7 @@ mod tests {
522
524
\x00 \x00 \x00 \x00 \
523
525
\x00 \x00 \
524
526
\x00 \x00 " as & [ u8 ] ) ;
525
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NumChannelsIsZero ) ) ,
527
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NumChannelsIsZero ) ) ,
526
528
data. read_wave_header( ) ) ;
527
529
}
528
530
@@ -536,7 +538,7 @@ mod tests {
536
538
\x00 \x00 \x00 \x00 \
537
539
\x00 \x00 \
538
540
\x00 \x00 " as & [ u8 ] ) ;
539
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: SampleRateIsZero ) ) ,
541
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: SampleRateIsZero ) ) ,
540
542
data. read_wave_header( ) ) ;
541
543
}
542
544
@@ -571,12 +573,12 @@ mod tests {
571
573
572
574
vec[ 34 ] = 48 ;
573
575
let mut cursor = Cursor :: new ( vec. clone ( ) ) ;
574
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: UnsupportedBitsPerSample ( _) ) ) ,
576
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: UnsupportedBitsPerSample ( _) ) ) ,
575
577
cursor. read_wave_header( ) ) ;
576
578
577
579
vec[ 34 ] = 0 ;
578
580
let mut cursor = Cursor :: new ( vec. clone ( ) ) ;
579
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: UnsupportedBitsPerSample ( _) ) ) ,
581
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: UnsupportedBitsPerSample ( _) ) ) ,
580
582
cursor. read_wave_header( ) ) ;
581
583
}
582
584
@@ -639,7 +641,7 @@ mod tests {
639
641
\x02 \x00 \x00 \x00 ") ;
640
642
let mut cursor = Cursor :: new ( vec. clone ( ) ) ;
641
643
642
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: FmtChunkTooShort ) ) ,
644
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: FmtChunkTooShort ) ) ,
643
645
cursor. read_wave_header( ) ) ;
644
646
}
645
647
@@ -660,7 +662,7 @@ mod tests {
660
662
\x09 \x00 \x00 \x00 \x00 \x00 \x10 \x00 \x80 \x00 \x00 \xAA \x00 \x38 \x9B \x71 ") ;
661
663
let mut cursor = Cursor :: new ( vec. clone ( ) ) ;
662
664
663
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
665
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
664
666
cursor. read_wave_header( ) ) ;
665
667
}
666
668
@@ -681,7 +683,7 @@ mod tests {
681
683
\x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ") ;
682
684
let mut cursor = Cursor :: new ( vec. clone ( ) ) ;
683
685
684
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: InvalidBitsPerSample ( _, _) ) ) ,
686
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: InvalidBitsPerSample ( _, _) ) ) ,
685
687
cursor. read_wave_header( ) ) ;
686
688
}
687
689
@@ -734,7 +736,7 @@ mod tests {
734
736
fn test_validate_extended_format_too_short ( ) {
735
737
// Extended size is less than 22 -- should fail.
736
738
let mut data = Cursor :: new ( b"\x0F \x00 \x00 \x00 " ) ;
737
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: FmtChunkTooShort ) ) ,
739
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: FmtChunkTooShort ) ) ,
738
740
data. validate_extended_format( 16 ) ) ;
739
741
}
740
742
@@ -745,7 +747,7 @@ mod tests {
745
747
\x00 \x00 \x00 \x00 \
746
748
\xFF \xFF \x00 \x00 \x00 \x00 \x00 \x00 \
747
749
\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ") ;
748
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
750
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: NotAnUncompressedPcmWaveFile ( _) ) ) ,
749
751
data. validate_extended_format( 16 ) ) ;
750
752
}
751
753
@@ -756,7 +758,7 @@ mod tests {
756
758
\x00 \x00 \x00 \x00 \
757
759
\x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \
758
760
\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ") ;
759
- assert_matches ! ( Err ( ReadError :: Format ( FormatErrorKind :: InvalidBitsPerSample ( _, _) ) ) ,
761
+ assert_matches ! ( Err ( ReadError :: Format ( ReadErrorKind :: InvalidBitsPerSample ( _, _) ) ) ,
760
762
data. validate_extended_format( 16 ) ) ;
761
763
}
762
764
0 commit comments