Skip to content

Commit 2227b09

Browse files
committed
aud_io: Move ParsingMode from lofty
1 parent 7990e89 commit 2227b09

File tree

7 files changed

+76
-78
lines changed

7 files changed

+76
-78
lines changed

aud_io/src/config/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
mod global_options;
2-
32
pub use global_options::*;
3+
4+
mod parse;
5+
pub use parse::*;

aud_io/src/config/parse.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// The parsing strictness mode
2+
///
3+
/// This can be set with [`Probe::options`](crate::probe::Probe).
4+
///
5+
/// # Examples
6+
///
7+
/// ```rust,no_run
8+
/// use lofty::config::{ParseOptions, ParsingMode};
9+
/// use lofty::probe::Probe;
10+
///
11+
/// # fn main() -> lofty::error::Result<()> {
12+
/// // We only want to read spec-compliant inputs
13+
/// let parsing_options = ParseOptions::new().parsing_mode(ParsingMode::Strict);
14+
/// let tagged_file = Probe::open("foo.mp3")?.options(parsing_options).read()?;
15+
/// # Ok(()) }
16+
/// ```
17+
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Default)]
18+
#[non_exhaustive]
19+
pub enum ParsingMode {
20+
/// Will eagerly error on invalid input
21+
///
22+
/// This mode will eagerly error on any non-spec-compliant input.
23+
///
24+
/// ## Examples of behavior
25+
///
26+
/// * Unable to decode text - The parser will error and the entire input is discarded
27+
/// * Unable to determine the sample rate - The parser will error and the entire input is discarded
28+
Strict,
29+
/// Default mode, less eager to error on recoverably malformed input
30+
///
31+
/// This mode will attempt to fill in any holes where possible in otherwise valid, spec-compliant input.
32+
///
33+
/// NOTE: A readable input does *not* necessarily make it writeable.
34+
///
35+
/// ## Examples of behavior
36+
///
37+
/// * Unable to decode text - If valid otherwise, the field will be replaced by an empty string and the parser moves on
38+
/// * Unable to determine the sample rate - The sample rate will be 0
39+
#[default]
40+
BestAttempt,
41+
/// Least eager to error, may produce invalid/partial output
42+
///
43+
/// This mode will discard any invalid fields, and ignore the majority of non-fatal errors.
44+
///
45+
/// If the input is malformed, the resulting tags may be incomplete, and the properties zeroed.
46+
///
47+
/// ## Examples of behavior
48+
///
49+
/// * Unable to decode text - The entire item is discarded and the parser moves on
50+
/// * Unable to determine the sample rate - The sample rate will be 0
51+
Relaxed,
52+
}

lofty/src/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod parse_options;
55
mod write_options;
66

77
pub use global_options::{GlobalOptions, apply_global_options};
8-
pub use parse_options::{ParseOptions, ParsingMode};
8+
pub use parse_options::*;
99
pub use write_options::WriteOptions;
1010

1111
pub(crate) use global_options::global_options;

lofty/src/config/parse_options.rs

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub use aud_io::config::ParsingMode;
2+
13
/// Options to control how Lofty parses a file
24
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
35
#[non_exhaustive]
@@ -163,56 +165,3 @@ impl ParseOptions {
163165
*self
164166
}
165167
}
166-
167-
/// The parsing strictness mode
168-
///
169-
/// This can be set with [`Probe::options`](crate::probe::Probe).
170-
///
171-
/// # Examples
172-
///
173-
/// ```rust,no_run
174-
/// use lofty::config::{ParseOptions, ParsingMode};
175-
/// use lofty::probe::Probe;
176-
///
177-
/// # fn main() -> lofty::error::Result<()> {
178-
/// // We only want to read spec-compliant inputs
179-
/// let parsing_options = ParseOptions::new().parsing_mode(ParsingMode::Strict);
180-
/// let tagged_file = Probe::open("foo.mp3")?.options(parsing_options).read()?;
181-
/// # Ok(()) }
182-
/// ```
183-
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Default)]
184-
#[non_exhaustive]
185-
pub enum ParsingMode {
186-
/// Will eagerly error on invalid input
187-
///
188-
/// This mode will eagerly error on any non spec-compliant input.
189-
///
190-
/// ## Examples of behavior
191-
///
192-
/// * Unable to decode text - The parser will error and the entire input is discarded
193-
/// * Unable to determine the sample rate - The parser will error and the entire input is discarded
194-
Strict,
195-
/// Default mode, less eager to error on recoverably malformed input
196-
///
197-
/// This mode will attempt to fill in any holes where possible in otherwise valid, spec-compliant input.
198-
///
199-
/// NOTE: A readable input does *not* necessarily make it writeable.
200-
///
201-
/// ## Examples of behavior
202-
///
203-
/// * Unable to decode text - If valid otherwise, the field will be replaced by an empty string and the parser moves on
204-
/// * Unable to determine the sample rate - The sample rate will be 0
205-
#[default]
206-
BestAttempt,
207-
/// Least eager to error, may produce invalid/partial output
208-
///
209-
/// This mode will discard any invalid fields, and ignore the majority of non-fatal errors.
210-
///
211-
/// If the input is malformed, the resulting tags may be incomplete, and the properties zeroed.
212-
///
213-
/// ## Examples of behavior
214-
///
215-
/// * Unable to decode text - The entire item is discarded and the parser moves on
216-
/// * Unable to determine the sample rate - The sample rate will be 0
217-
Relaxed,
218-
}

lofty/src/id3/v2/frame/read.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ impl ParsedFrame<'_> {
4343
},
4444
Ok(Some(some)) => some,
4545
Err(err) => {
46-
match parse_options.parsing_mode {
47-
ParsingMode::Strict => return Err(err),
48-
ParsingMode::BestAttempt | ParsingMode::Relaxed => {
49-
log::warn!("Failed to read frame header, skipping: {}", err);
50-
51-
// Skip this frame and continue reading
52-
skip_frame(reader, size)?;
53-
return Ok(Self::Skip);
54-
},
46+
if parse_options.parsing_mode == ParsingMode::Strict {
47+
return Err(err);
5548
}
49+
50+
log::warn!("Failed to read frame header, skipping: {}", err);
51+
52+
// Skip this frame and continue reading
53+
skip_frame(reader, size)?;
54+
return Ok(Self::Skip);
5655
},
5756
};
5857

lofty/src/mp4/atom_info.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,12 @@ impl AtomInfo {
146146
// Seek to the end, since we can't recover from this
147147
data.seek(SeekFrom::End(0))?;
148148

149-
match parse_mode {
150-
ParsingMode::Strict => {
151-
err!(BadAtom("Encountered an atom with invalid characters"));
152-
},
153-
ParsingMode::BestAttempt | ParsingMode::Relaxed => {
154-
log::warn!("Encountered an atom with invalid characters, stopping");
155-
return Ok(None);
156-
},
149+
if parse_mode == ParsingMode::Strict {
150+
err!(BadAtom("Encountered an atom with invalid characters"));
157151
}
152+
153+
log::warn!("Encountered an atom with invalid characters, stopping");
154+
return Ok(None);
158155
}
159156

160157
let (len, extended) = match len_raw {

lofty/src/mp4/ilst/read.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,12 @@ where
182182
R: Read + Seek,
183183
{
184184
let handle_error = |err: LoftyError, parsing_mode: ParsingMode| -> Result<()> {
185-
match parsing_mode {
186-
ParsingMode::Strict => Err(err),
187-
ParsingMode::BestAttempt | ParsingMode::Relaxed => {
188-
log::warn!("Skipping atom with invalid content: {}", err);
189-
Ok(())
190-
},
185+
if parsing_mode == ParsingMode::Strict {
186+
return Err(err);
191187
}
188+
189+
log::warn!("Skipping atom with invalid content: {err}");
190+
Ok(())
192191
};
193192

194193
if let Some(mut atom_data) = parse_data_inner(reader, parsing_mode, &atom_info)? {

0 commit comments

Comments
 (0)