mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-12-14 14:42:33 +00:00
Resolve: Move panic into lookup_resolver
This commit is contained in:
parent
0d4b907e31
commit
f1ab92733a
5 changed files with 26 additions and 42 deletions
20
src/file.rs
20
src/file.rs
|
@ -526,14 +526,8 @@ impl FileType {
|
|||
},
|
||||
FileType::MP4 => TagType::MP4ilst,
|
||||
FileType::Custom(c) => {
|
||||
if let Some(r) = crate::resolve::lookup_resolver(c) {
|
||||
r.primary_tag_type()
|
||||
} else {
|
||||
panic!(
|
||||
"Encountered an unregistered custom `FileType` named `{}`",
|
||||
c
|
||||
);
|
||||
}
|
||||
let resolver = crate::resolve::lookup_resolver(c);
|
||||
resolver.primary_tag_type()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -579,14 +573,8 @@ impl FileType {
|
|||
#[cfg(feature = "riff_info_list")]
|
||||
FileType::WAV => tag_type == TagType::RIFFInfo,
|
||||
FileType::Custom(c) => {
|
||||
if let Some(r) = crate::resolve::lookup_resolver(c) {
|
||||
r.supported_tag_types().contains(&tag_type)
|
||||
} else {
|
||||
panic!(
|
||||
"Encountered an unregistered custom `FileType` named `{}`",
|
||||
c
|
||||
);
|
||||
}
|
||||
let resolver = crate::resolve::lookup_resolver(c);
|
||||
resolver.supported_tag_types().contains(&tag_type)
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use super::header::{ChannelMode, Emphasis, Header, Layer, MpegVersion, XingHeader};
|
||||
use crate::error::Result;
|
||||
use crate::mpeg::header::{cmp_header, rev_search_for_frame_sync, HeaderCmpResult};
|
||||
use crate::probe::ParseOptions;
|
||||
use crate::properties::FileProperties;
|
||||
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
|
@ -105,7 +104,6 @@ impl MPEGProperties {
|
|||
pub(super) fn read_properties<R>(
|
||||
properties: &mut MPEGProperties,
|
||||
reader: &mut R,
|
||||
parse_options: ParseOptions,
|
||||
first_frame: (Header, u64),
|
||||
mut last_frame_offset: u64,
|
||||
xing_header: Option<XingHeader>,
|
||||
|
@ -157,9 +155,7 @@ where
|
|||
last_frame_offset = reader.stream_position()?;
|
||||
let last_frame_data = reader.read_u32::<BigEndian>()?;
|
||||
|
||||
if let Some(last_frame_header) =
|
||||
Header::read(last_frame_data, parse_options.parsing_mode)?
|
||||
{
|
||||
if let Some(last_frame_header) = Header::read(last_frame_data) {
|
||||
match cmp_header(reader, last_frame_header.len, last_frame_data) {
|
||||
HeaderCmpResult::Equal | HeaderCmpResult::Undetermined => {
|
||||
last_frame = Some(last_frame_header);
|
||||
|
|
|
@ -10,7 +10,7 @@ use crate::id3::v2::read::parse_id3v2;
|
|||
use crate::id3::v2::read_id3v2_header;
|
||||
use crate::id3::{find_id3v1, find_lyrics3v2, ID3FindResults};
|
||||
use crate::macros::{decode_err, err};
|
||||
use crate::probe::{ParseOptions, ParsingMode};
|
||||
use crate::probe::ParseOptions;
|
||||
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
|
||||
|
@ -87,9 +87,7 @@ where
|
|||
reader.seek(SeekFrom::Current(-1 * header.len() as i64))?;
|
||||
|
||||
#[allow(clippy::used_underscore_binding)]
|
||||
if let Some((_first_first_header, _first_frame_offset)) =
|
||||
find_next_frame(reader, parse_options.parsing_mode)?
|
||||
{
|
||||
if let Some((_first_first_header, _first_frame_offset)) = find_next_frame(reader)? {
|
||||
first_frame_offset = _first_frame_offset;
|
||||
first_frame_header = Some(_first_first_header);
|
||||
break;
|
||||
|
@ -157,14 +155,13 @@ where
|
|||
let mut xing_reader = [0; 32];
|
||||
reader.read_exact(&mut xing_reader)?;
|
||||
|
||||
let xing_header = XingHeader::read(&mut &xing_reader[..], parse_options.parsing_mode)?;
|
||||
let xing_header = XingHeader::read(&mut &xing_reader[..])?;
|
||||
|
||||
let file_length = reader.seek(SeekFrom::End(0))?;
|
||||
|
||||
super::properties::read_properties(
|
||||
&mut file.properties,
|
||||
reader,
|
||||
parse_options,
|
||||
(first_frame_header, first_frame_offset),
|
||||
last_frame_offset,
|
||||
xing_header,
|
||||
|
@ -176,7 +173,7 @@ where
|
|||
}
|
||||
|
||||
// Searches for the next frame, comparing it to the following one
|
||||
fn find_next_frame<R>(reader: &mut R, parsing_mode: ParsingMode) -> Result<Option<(Header, u64)>>
|
||||
fn find_next_frame<R>(reader: &mut R) -> Result<Option<(Header, u64)>>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
|
@ -189,7 +186,7 @@ where
|
|||
reader.seek(SeekFrom::Start(first_mp3_frame_start_absolute))?;
|
||||
let first_header_data = reader.read_u32::<BigEndian>()?;
|
||||
|
||||
if let Some(first_header) = Header::read(first_header_data, parsing_mode)? {
|
||||
if let Some(first_header) = Header::read(first_header_data) {
|
||||
match cmp_header(reader, first_header.len, first_header_data) {
|
||||
HeaderCmpResult::Equal => {
|
||||
return Ok(Some((first_header, first_mp3_frame_start_absolute)))
|
||||
|
|
15
src/probe.rs
15
src/probe.rs
|
@ -503,17 +503,14 @@ impl<R: Read + Seek> Probe<R> {
|
|||
FileType::MP4 => Mp4File::read_from(reader, options)?.into(),
|
||||
FileType::Speex => SpeexFile::read_from(reader, options)?.into(),
|
||||
FileType::WavPack => WavPackFile::read_from(reader, options)?.into(),
|
||||
FileType::Custom(c) if options.use_custom_resolvers => {
|
||||
if let Some(r) = crate::resolve::lookup_resolver(c) {
|
||||
r.read_from(reader, options)?
|
||||
} else {
|
||||
panic!(
|
||||
"Encountered an unregistered custom `FileType` named `{}`",
|
||||
c
|
||||
);
|
||||
FileType::Custom(c) => {
|
||||
if !options.use_custom_resolvers {
|
||||
err!(UnknownFormat)
|
||||
}
|
||||
|
||||
let resolver = crate::resolve::lookup_resolver(c);
|
||||
resolver.read_from(reader, options)?
|
||||
},
|
||||
_ => err!(UnknownFormat),
|
||||
}),
|
||||
None => err!(UnknownFormat),
|
||||
}
|
||||
|
|
|
@ -40,11 +40,17 @@ type ResolverMap = HashMap<&'static str, &'static dyn ObjectSafeFileResolver>;
|
|||
pub(crate) static CUSTOM_RESOLVERS: Lazy<Arc<Mutex<ResolverMap>>> =
|
||||
Lazy::new(|| Arc::new(Mutex::new(HashMap::new())));
|
||||
|
||||
// TODO: Just panic in here, rather than return an Option
|
||||
pub(crate) fn lookup_resolver(name: &'static str) -> Option<&'static dyn ObjectSafeFileResolver> {
|
||||
let res = CUSTOM_RESOLVERS.lock().ok()?;
|
||||
pub(crate) fn lookup_resolver(name: &'static str) -> &'static dyn ObjectSafeFileResolver {
|
||||
let res = CUSTOM_RESOLVERS.lock().unwrap();
|
||||
|
||||
res.get(name).copied()
|
||||
if let Some(resolver) = res.get(name).copied() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
panic!(
|
||||
"Encountered an unregistered custom `FileType` named `{}`",
|
||||
name
|
||||
);
|
||||
}
|
||||
|
||||
// A `Read + Seek` supertrait for use in [`ObjectSafeFileResolver::read_from`]
|
||||
|
|
Loading…
Reference in a new issue