@@ -22,27 +22,18 @@ fn tag_to_note_type(tag: &str) -> NoteType {
2222}
2323
2424fn parse_us_int ( value : & str , property : & str ) -> Result < i32 , AppError > {
25- value
26- . replace ( "," , "." )
27- . parse :: < i32 > ( )
28- . map_err ( |_| {
29- AppError :: UltrastarError ( format ! (
30- "Failed to parse integer for {}: {}" ,
31- property, value
32- ) )
33- } )
25+ value. replace ( "," , "." ) . parse :: < i32 > ( ) . map_err ( |_| {
26+ AppError :: UltrastarError ( format ! (
27+ "Failed to parse integer for {}: {}" ,
28+ property, value
29+ ) )
30+ } )
3431}
3532
3633fn parse_us_float ( value : & str , property : & str ) -> Result < f64 , AppError > {
37- value
38- . replace ( "," , "." )
39- . parse :: < f64 > ( )
40- . map_err ( |_| {
41- AppError :: UltrastarError ( format ! (
42- "Failed to parse float for {}: {}" ,
43- property, value
44- ) )
45- } )
34+ value. replace ( "," , "." ) . parse :: < f64 > ( ) . map_err ( |_| {
35+ AppError :: UltrastarError ( format ! ( "Failed to parse float for {}: {}" , property, value) )
36+ } )
4637}
4738
4839fn parse_us_bool ( value : & str ) -> bool {
@@ -235,50 +226,73 @@ pub fn parse_local_txt_file(
235226 let content = fs:: read_to_string ( txt) ?;
236227 let song = parse_ultrastar_txt ( & content) ?;
237228
238- let create_url =
239- |filename : & Option < String > , file_type : & str | -> Result < Option < String > , AppError > {
240- if let Some ( filename) = filename {
241- let normalized_target = filename. nfc ( ) . collect :: < String > ( ) . to_lowercase ( ) ;
229+ let find_file = |filename : & Option < String > | -> Option < & FileEntry > {
230+ if let Some ( filename) = filename {
231+ let normalized_target = filename. nfc ( ) . collect :: < String > ( ) . to_lowercase ( ) ;
232+ files. iter ( ) . find ( |f| {
233+ let normalized_file = f. filename . nfc ( ) . collect :: < String > ( ) . to_lowercase ( ) ;
234+ normalized_file == normalized_target
235+ } )
236+ } else {
237+ None
238+ }
239+ } ;
242240
243- if let Some ( file_entry) = files. iter ( ) . find ( |f| {
244- let normalized_file = f. filename . nfc ( ) . collect :: < String > ( ) . to_lowercase ( ) ;
245- normalized_file == normalized_target
246- } ) {
247- let path = dunce:: canonicalize ( & file_entry. path ) ?;
248- let path_string = path. to_string_lossy ( ) ;
249- let encoded = urlencoding:: encode ( & path_string) ;
250- return Ok ( Some ( format ! ( "{}/{}" , media_base_url, encoded) ) ) ;
251- } else {
252- // File was specified but not found
253- if file_type == "audio" {
254- return Err ( AppError :: UltrastarError ( format ! (
255- "Audio file '{}' was specified but not found" ,
256- filename
257- ) ) ) ;
258- } else {
259- log:: warn!(
260- "{} file '{}' was specified but not found" ,
261- file_type,
262- filename
263- ) ;
264- }
265- }
241+ let create_url_from_file =
242+ |file_entry : Option < & FileEntry > | -> Result < Option < String > , AppError > {
243+ if let Some ( file_entry) = file_entry {
244+ let path = dunce:: canonicalize ( & file_entry. path ) ?;
245+ let path_string = path. to_string_lossy ( ) ;
246+ let encoded = urlencoding:: encode ( & path_string) ;
247+ Ok ( Some ( format ! ( "{}/{}" , media_base_url, encoded) ) )
248+ } else {
249+ Ok ( None )
266250 }
267- Ok ( None )
268251 } ;
269252
270- let audio_url = create_url ( & song. audio , "Audio" ) ?;
271- let video_url = create_url ( & song. video , "Video" ) ?;
272- let cover_url = create_url ( & song. cover , "Cover" ) ?;
273- let background_url = create_url ( & song. background , "Background" ) ?;
253+ let audio_file = find_file ( & song. audio ) ;
254+ let video_file = find_file ( & song. video ) ;
255+ let cover_file = find_file ( & song. cover ) ;
256+ let background_file = find_file ( & song. background ) ;
257+
258+ if song. audio . is_some ( ) && audio_file. is_none ( ) {
259+ return Err ( AppError :: UltrastarError ( format ! (
260+ "Audio file '{}' was specified but not found" ,
261+ song. audio. as_ref( ) . unwrap( )
262+ ) ) ) ;
263+ }
264+
265+ if song. video . is_some ( ) && video_file. is_none ( ) {
266+ log:: warn!(
267+ "Video file '{}' was specified but not found" ,
268+ song. video. as_ref( ) . unwrap( )
269+ ) ;
270+ }
271+ if song. cover . is_some ( ) && cover_file. is_none ( ) {
272+ log:: warn!(
273+ "Cover file '{}' was specified but not found" ,
274+ song. cover. as_ref( ) . unwrap( )
275+ ) ;
276+ }
277+ if song. background . is_some ( ) && background_file. is_none ( ) {
278+ log:: warn!(
279+ "Background file '{}' was specified but not found" ,
280+ song. background. as_ref( ) . unwrap( )
281+ ) ;
282+ }
283+
284+ let audio_url = create_url_from_file ( audio_file) ?;
285+ let video_url = create_url_from_file ( video_file) ?;
286+ let cover_url = create_url_from_file ( cover_file) ?;
287+ let background_url = create_url_from_file ( background_file) ?;
274288
275289 if audio_url. is_none ( ) && video_url. is_none ( ) {
276290 return Err ( AppError :: UltrastarError (
277291 "Song must have either audio or video file available" . to_string ( ) ,
278292 ) ) ;
279293 }
280294
281- let replay_gain = get_replay_gain ( & txt ) . ok ( ) ;
295+ let replay_gain = audio_file . and_then ( |file| get_replay_gain ( & file . path ) . ok ( ) ) ;
282296
283297 Ok ( LocalSong {
284298 song,
0 commit comments