@@ -2,7 +2,7 @@ use crate::core::{Edition, Shell, Workspace};
2
2
use crate :: util:: errors:: CargoResult ;
3
3
use crate :: util:: { existing_vcs_repo, FossilRepo , GitRepo , HgRepo , PijulRepo } ;
4
4
use crate :: util:: { restricted_names, Config } ;
5
- use anyhow:: Context as _;
5
+ use anyhow:: { anyhow , Context as _} ;
6
6
use cargo_util:: paths;
7
7
use serde:: de;
8
8
use serde:: Deserialize ;
@@ -595,9 +595,22 @@ impl IgnoreList {
595
595
/// already exists. It reads the contents of the given `BufRead` and
596
596
/// checks if the contents of the ignore list are already existing in the
597
597
/// file.
598
- fn format_existing < T : BufRead > ( & self , existing : T , vcs : VersionControl ) -> String {
599
- // TODO: is unwrap safe?
600
- let existing_items = existing. lines ( ) . collect :: < Result < Vec < _ > , _ > > ( ) . unwrap ( ) ;
598
+ fn format_existing < T : BufRead > ( & self , existing : T , vcs : VersionControl ) -> CargoResult < String > {
599
+ let mut existing_items = Vec :: new ( ) ;
600
+ for ( i, item) in existing. lines ( ) . enumerate ( ) {
601
+ match item {
602
+ Ok ( s) => existing_items. push ( s) ,
603
+ Err ( err) => match err. kind ( ) {
604
+ ErrorKind :: InvalidData => {
605
+ return Err ( anyhow ! (
606
+ "Character at line {} is invalid. Cargo only supports UTF-8." ,
607
+ i
608
+ ) )
609
+ }
610
+ _ => return Err ( anyhow ! ( err) ) ,
611
+ } ,
612
+ }
613
+ }
601
614
602
615
let ignore_items = match vcs {
603
616
VersionControl :: Hg => & self . hg_ignore ,
@@ -631,7 +644,7 @@ impl IgnoreList {
631
644
out. push ( '\n' ) ;
632
645
}
633
646
634
- out
647
+ Ok ( out)
635
648
}
636
649
}
637
650
@@ -660,7 +673,7 @@ fn write_ignore_file(base_path: &Path, list: &IgnoreList, vcs: VersionControl) -
660
673
Some ( io_err) if io_err. kind ( ) == ErrorKind :: NotFound => list. format_new ( vcs) ,
661
674
_ => return Err ( err) ,
662
675
} ,
663
- Ok ( file) => list. format_existing ( BufReader :: new ( file) , vcs) ,
676
+ Ok ( file) => list. format_existing ( BufReader :: new ( file) , vcs) ? ,
664
677
} ;
665
678
666
679
paths:: append ( & fp_ignore, ignore. as_bytes ( ) ) ?;
0 commit comments