Skip to content

Commit 1a15509

Browse files
rilliankinetiknz
authored andcommitted
mp4parse_capi: Convert single-branch match to if let.
Clippy suggests `if let` as more idomatic than a `match` where only one branch does something. I'm not convinced the new form is always easier to read, but it does save an indentation level. Address the lint by converting the instances we have. In one case, some temporaries were necessary to fit within the line length. I also added an overflow check for the usize->u32 cast.
1 parent 466dfa1 commit 1a15509

File tree

1 file changed

+36
-56
lines changed

1 file changed

+36
-56
lines changed

mp4parse_capi/src/lib.rs

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -568,31 +568,25 @@ pub unsafe extern fn mp4parse_get_track_audio_info(parser: *mut mp4parse_parser,
568568
Ok(_) => {
569569
let header = (*parser).opus_header_mut();
570570
header.insert(track_index, v);
571-
match header.get(&track_index) {
572-
None => {}
573-
Some(v) => {
574-
if v.len() > std::u32::MAX as usize {
575-
return MP4PARSE_ERROR_INVALID;
576-
}
577-
(*info).codec_specific_config.length = v.len() as u32;
578-
(*info).codec_specific_config.data = v.as_ptr();
571+
if let Some(v) = header.get(&track_index) {
572+
if v.len() > std::u32::MAX as usize {
573+
return MP4PARSE_ERROR_INVALID;
579574
}
575+
(*info).codec_specific_config.length = v.len() as u32;
576+
(*info).codec_specific_config.data = v.as_ptr();
580577
}
581578
}
582579
}
583580
}
584581
AudioCodecSpecific::MP3 => (),
585582
}
586583

587-
match audio.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
588-
Some(p) => {
589-
if let Some(ref tenc) = p.tenc {
590-
(*info).protected_data.is_encrypted = tenc.is_encrypted;
591-
(*info).protected_data.iv_size = tenc.iv_size;
592-
(*info).protected_data.kid.set_data(&(tenc.kid));
593-
}
594-
},
595-
_ => {},
584+
if let Some(p) = audio.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
585+
if let Some(ref tenc) = p.tenc {
586+
(*info).protected_data.is_encrypted = tenc.is_encrypted;
587+
(*info).protected_data.iv_size = tenc.iv_size;
588+
(*info).protected_data.kid.set_data(&(tenc.kid));
589+
}
596590
}
597591

598592
MP4PARSE_OK
@@ -648,22 +642,16 @@ pub unsafe extern fn mp4parse_get_track_video_info(parser: *mut mp4parse_parser,
648642
(*info).image_width = video.width;
649643
(*info).image_height = video.height;
650644

651-
match video.codec_specific {
652-
VideoCodecSpecific::AVCConfig(ref avc) => {
653-
(*info).extra_data.set_data(avc);
654-
},
655-
_ => {},
645+
if let VideoCodecSpecific::AVCConfig(ref avc) = video.codec_specific {
646+
(*info).extra_data.set_data(avc);
656647
}
657648

658-
match video.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
659-
Some(p) => {
660-
if let Some(ref tenc) = p.tenc {
661-
(*info).protected_data.is_encrypted = tenc.is_encrypted;
662-
(*info).protected_data.iv_size = tenc.iv_size;
663-
(*info).protected_data.kid.set_data(&(tenc.kid));
664-
}
665-
},
666-
_ => {},
649+
if let Some(p) = video.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
650+
if let Some(ref tenc) = p.tenc {
651+
(*info).protected_data.is_encrypted = tenc.is_encrypted;
652+
(*info).protected_data.iv_size = tenc.iv_size;
653+
(*info).protected_data.kid.set_data(&(tenc.kid));
654+
}
667655
}
668656

669657
MP4PARSE_OK
@@ -686,12 +674,9 @@ pub unsafe extern fn mp4parse_get_indice_table(parser: *mut mp4parse_parser, tra
686674
};
687675

688676
let index_table = (*parser).sample_table_mut();
689-
match index_table.get(&track_id) {
690-
Some(v) => {
691-
(*indices).set_indices(v);
692-
return MP4PARSE_OK;
693-
},
694-
_ => {},
677+
if let Some(v) = index_table.get(&track_id) {
678+
(*indices).set_indices(v);
679+
return MP4PARSE_OK;
695680
}
696681

697682
let media_time = match (&track.media_time, &track.timescale) {
@@ -718,13 +703,10 @@ pub unsafe extern fn mp4parse_get_indice_table(parser: *mut mp4parse_parser, tra
718703
_ => 0,
719704
};
720705

721-
match create_sample_table(track, offset_time) {
722-
Some(v) => {
723-
(*indices).set_indices(&v);
724-
index_table.insert(track_id, v);
725-
return MP4PARSE_OK;
726-
},
727-
_ => {},
706+
if let Some(v) = create_sample_table(track, offset_time) {
707+
(*indices).set_indices(&v);
708+
index_table.insert(track_id, v);
709+
return MP4PARSE_OK;
728710
}
729711

730712
MP4PARSE_ERROR_INVALID
@@ -934,13 +916,10 @@ fn create_sample_table(track: &Track, track_offset_time: i64) -> Option<Vec<mp4p
934916
}
935917

936918
// Mark the sync sample in sample_table according to 'stss'.
937-
match track.stss {
938-
Some(ref v) => {
939-
for iter in &v.samples {
940-
sample_table[(iter - 1) as usize].sync = true;
941-
}
942-
},
943-
_ => {}
919+
if let Some(ref v) = track.stss {
920+
for iter in &v.samples {
921+
sample_table[(iter - 1) as usize].sync = true;
922+
}
944923
}
945924

946925
let ctts_iter = match track.ctts {
@@ -1114,12 +1093,13 @@ pub unsafe extern fn mp4parse_get_pssh_info(parser: *mut mp4parse_parser, info:
11141093

11151094
pssh_data.clear();
11161095
for pssh in &context.psshs {
1096+
let content_len = pssh.box_content.len();
1097+
if content_len > std::u32::MAX as usize {
1098+
return MP4PARSE_ERROR_INVALID;
1099+
}
11171100
let mut data_len = Vec::new();
1118-
match data_len.write_u32::<byteorder::NativeEndian>(pssh.box_content.len() as u32) {
1119-
Err(_) => {
1120-
return MP4PARSE_ERROR_IO;
1121-
},
1122-
_ => (),
1101+
if data_len.write_u32::<byteorder::NativeEndian>(content_len as u32).is_err() {
1102+
return MP4PARSE_ERROR_IO;
11231103
}
11241104
pssh_data.extend_from_slice(pssh.system_id.as_slice());
11251105
pssh_data.extend_from_slice(data_len.as_slice());

0 commit comments

Comments
 (0)