@@ -513,18 +513,8 @@ pub fn retrieve_asset(
513513 } else if url. scheme ( ) == "file" {
514514 // Check if parent_url is also a file: URL (if not, then we don't embed the asset)
515515 if parent_url. scheme ( ) != "file" {
516- if !options. silent {
517- eprintln ! (
518- "{}{} (Security Error){}" ,
519- if options. no_color { "" } else { ANSI_COLOR_RED } ,
520- & url,
521- if options. no_color {
522- ""
523- } else {
524- ANSI_COLOR_RESET
525- } ,
526- ) ;
527- }
516+ print_error_message ( & format ! ( "{} (security error)" , & url) , options) ;
517+
528518 // Provoke error
529519 client. get ( "" ) . send ( ) ?;
530520 }
@@ -533,27 +523,14 @@ pub fn retrieve_asset(
533523 let path: & Path = path_buf. as_path ( ) ;
534524 if path. exists ( ) {
535525 if path. is_dir ( ) {
536- if !options. silent {
537- eprintln ! (
538- "{}{} (is a directory){}" ,
539- if options. no_color { "" } else { ANSI_COLOR_RED } ,
540- & url,
541- if options. no_color {
542- ""
543- } else {
544- ANSI_COLOR_RESET
545- } ,
546- ) ;
547- }
526+ print_error_message ( & format ! ( "{} (is a directory)" , & url) , options) ;
548527
549528 // Provoke error
550529 Err ( client. get ( "" ) . send ( ) . unwrap_err ( ) )
551530 } else {
552- if !options. silent {
553- eprintln ! ( "{}" , & url) ;
554- }
531+ print_info_message ( & format ! ( "{}" , & url) , options) ;
555532
556- let file_blob: Vec < u8 > = fs:: read ( path) . expect ( "Unable to read file" ) ;
533+ let file_blob: Vec < u8 > = fs:: read ( path) . expect ( "unable to read file" ) ;
557534
558535 Ok ( (
559536 file_blob. clone ( ) ,
@@ -563,18 +540,7 @@ pub fn retrieve_asset(
563540 ) )
564541 }
565542 } else {
566- if !options. silent {
567- eprintln ! (
568- "{}{} (not found){}" ,
569- if options. no_color { "" } else { ANSI_COLOR_RED } ,
570- & url,
571- if options. no_color {
572- ""
573- } else {
574- ANSI_COLOR_RESET
575- } ,
576- ) ;
577- }
543+ print_error_message ( & format ! ( "{} (not found)" , & url) , options) ;
578544
579545 // Provoke error
580546 Err ( client. get ( "" ) . send ( ) . unwrap_err ( ) )
@@ -584,9 +550,7 @@ pub fn retrieve_asset(
584550
585551 if cache. is_some ( ) && cache. as_ref ( ) . unwrap ( ) . contains_key ( & cache_key) {
586552 // URL is in cache, we get and return it
587- if !options. silent {
588- eprintln ! ( "{} (from cache)" , & url) ;
589- }
553+ print_info_message ( & format ! ( "{} (from cache)" , & url) , options) ;
590554
591555 Ok ( (
592556 cache. as_ref ( ) . unwrap ( ) . get ( & cache_key) . unwrap ( ) . 0 . to_vec ( ) ,
@@ -627,31 +591,18 @@ pub fn retrieve_asset(
627591 match client. get ( url. as_str ( ) ) . headers ( headers) . send ( ) {
628592 Ok ( response) => {
629593 if !options. ignore_errors && response. status ( ) != reqwest:: StatusCode :: OK {
630- if !options. silent {
631- eprintln ! (
632- "{}{} ({}){}" ,
633- if options. no_color { "" } else { ANSI_COLOR_RED } ,
634- & url,
635- response. status( ) ,
636- if options. no_color {
637- ""
638- } else {
639- ANSI_COLOR_RESET
640- } ,
641- ) ;
642- }
594+ print_error_message ( & format ! ( "{} ({})" , & url, response. status( ) ) , options) ;
595+
643596 // Provoke error
644597 return Err ( client. get ( "" ) . send ( ) . unwrap_err ( ) ) ;
645598 }
646599
647600 let response_url: Url = response. url ( ) . clone ( ) ;
648601
649- if !options. silent {
650- if url. as_str ( ) == response_url. as_str ( ) {
651- eprintln ! ( "{}" , & url) ;
652- } else {
653- eprintln ! ( "{} -> {}" , & url, & response_url) ;
654- }
602+ if url. as_str ( ) == response_url. as_str ( ) {
603+ print_info_message ( & format ! ( "{}" , & url) , options) ;
604+ } else {
605+ print_info_message ( & format ! ( "{} -> {}" , & url, & response_url) , options) ;
655606 }
656607
657608 let new_cache_key: String = clean_url ( response_url. clone ( ) ) . to_string ( ) ;
@@ -672,18 +623,7 @@ pub fn retrieve_asset(
672623 data = b. to_vec ( ) ;
673624 }
674625 Err ( error) => {
675- if !options. silent {
676- eprintln ! (
677- "{}{}{}" ,
678- if options. no_color { "" } else { ANSI_COLOR_RED } ,
679- error,
680- if options. no_color {
681- ""
682- } else {
683- ANSI_COLOR_RESET
684- } ,
685- ) ;
686- }
626+ print_error_message ( & format ! ( "{}" , error) , options) ;
687627 }
688628 }
689629
@@ -701,19 +641,7 @@ pub fn retrieve_asset(
701641 Ok ( ( data, response_url, media_type, charset) )
702642 }
703643 Err ( error) => {
704- if !options. silent {
705- eprintln ! (
706- "{}{} ({}){}" ,
707- if options. no_color { "" } else { ANSI_COLOR_RED } ,
708- & url,
709- error,
710- if options. no_color {
711- ""
712- } else {
713- ANSI_COLOR_RESET
714- } ,
715- ) ;
716- }
644+ print_error_message ( & format ! ( "{} ({})" , & url, error) , options) ;
717645
718646 Err ( client. get ( "" ) . send ( ) . unwrap_err ( ) )
719647 }
@@ -730,3 +658,38 @@ pub fn read_stdin() -> Vec<u8> {
730658 Err ( _) => buffer,
731659 }
732660}
661+
662+ use std:: io:: Write ;
663+
664+ pub fn print_error_message ( text : & str , options : & Options ) {
665+ if !options. silent {
666+ let stderr = io:: stderr ( ) ;
667+ let mut handle = stderr. lock ( ) ;
668+
669+ if handle
670+ . write_all (
671+ format ! (
672+ "{}{}{}\n " ,
673+ if options. no_color { "" } else { ANSI_COLOR_RED } ,
674+ & text,
675+ if options. no_color {
676+ ""
677+ } else {
678+ ANSI_COLOR_RESET
679+ } ,
680+ )
681+ . as_bytes ( ) ,
682+ )
683+ . is_ok ( )
684+ { }
685+ }
686+ }
687+
688+ pub fn print_info_message ( text : & str , options : & Options ) {
689+ if !options. silent {
690+ let stderr = io:: stderr ( ) ;
691+ let mut handle = stderr. lock ( ) ;
692+
693+ if handle. write_all ( format ! ( "{}\n " , & text) . as_bytes ( ) ) . is_ok ( ) { }
694+ }
695+ }
0 commit comments