From 0f4cfacfbe9d77f4615aee7b8d067fc099f5a917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Finn=20B=C3=B6ger?= Date: Sun, 16 Aug 2020 16:01:51 +0200 Subject: [PATCH 1/2] pass along path when canonicalization fails --- src/loader.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/loader.rs b/src/loader.rs index 9690711..0f1f985 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -23,8 +23,9 @@ error_chain! { display("decoding error: {}", msg) } #[doc="error in path canonicalization"] - CanonicalizationError { + CanonicalizationError(msg: String) { description("canonicalization error") + display("canonicalization error: {}", msg) } #[doc="error in parsing the song header"] HeaderParsingError { @@ -72,7 +73,7 @@ fn canonicalize_path>( tmp_path.push(x); let result = tmp_path .canonicalize() - .chain_err(|| ErrorKind::CanonicalizationError)?; + .chain_err(|| ErrorKind::CanonicalizationError(format!("{:?}", tmp_path)))?; Source::Local(result) } }) From 597792e0a810c9aa1d9fb4db71b3096f5930cc12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Finn=20B=C3=B6ger?= Date: Sun, 16 Aug 2020 19:08:27 +0200 Subject: [PATCH 2/2] pass along missing header fields and failed line --- src/loader.rs | 24 ++++++++++++++++++++---- src/parser.rs | 12 +++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/loader.rs b/src/loader.rs index 0f1f985..8ff35ca 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -28,12 +28,14 @@ error_chain! { display("canonicalization error: {}", msg) } #[doc="error in parsing the song header"] - HeaderParsingError { + HeaderParsingError(msg: String) { description("header parsing error") + display("header parsing error: {}", msg) } #[doc="error in parsing the songs lines"] - LinesParsingError { + LinesParsingError(msg: String) { description("lines parsing error") + display("lines parsing error: {}", msg) } } } @@ -92,8 +94,22 @@ pub fn parse_txt_song>(path: P) -> Result { let txt = read_file_to_string(path)?; let mut txt_song = TXTSong { - header: parse_txt_header_str(txt.as_ref()).chain_err(|| ErrorKind::HeaderParsingError)?, - lines: parse_txt_lines_str(txt.as_ref()).chain_err(|| ErrorKind::LinesParsingError)?, + header: parse_txt_header_str(txt.as_ref()) + .map_err(|e| { + let s = e.to_string(); + Error::with_chain( + e, + ErrorKind::HeaderParsingError(s), + ) + })?, + lines: parse_txt_lines_str(txt.as_ref()) + .map_err(|e| { + let s = e.to_string(); + Error::with_chain( + e, + ErrorKind::LinesParsingError(s) + ) + })?, }; // canonicalize paths diff --git a/src/parser.rs b/src/parser.rs index 8fa10c8..0758868 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -10,8 +10,9 @@ error_chain! { display("additional {} tag found in line: {}", line, tag) } #[doc="an essential header is missing"] - MissingEssential { + MissingEssential(fields: Vec) { description("essential header is missing") + display("essential header is missing. Missing headers: {:?}", fields) } #[doc="value could not be parsed"] @@ -232,7 +233,7 @@ pub fn parse_txt_header_str(txt_str: &str) -> Result
{ // build header from Options if let (Some(title), Some(artist), Some(bpm), Some(audio_path)) = - (opt_title, opt_artist, opt_bpm, opt_audio_path) + (opt_title.clone(), opt_artist.clone(), opt_bpm.clone(), opt_audio_path.clone()) { let header = Header { title, @@ -255,8 +256,13 @@ pub fn parse_txt_header_str(txt_str: &str) -> Result
{ // header complete Ok(header) } else { + let mut fields = Vec::new(); + if opt_title.is_none() { fields.push("Title".to_string()) } + if opt_artist.is_none() { fields.push("Artist".to_string()) } + if opt_bpm.is_none() { fields.push("BPM".to_string()) } + if opt_audio_path.is_none() { fields.push("Audio Path".to_string()) } // essential field is missing - bail!(ErrorKind::MissingEssential) + bail!(ErrorKind::MissingEssential(fields)) } }