1
+ use crate :: core:: { compiler, Workspace } ;
2
+ use crate :: util:: errors:: { self , CargoResult , CargoResultExt } ;
3
+ use crate :: util:: { existing_vcs_repo, FossilRepo , GitRepo , HgRepo , PijulRepo } ;
4
+ use crate :: util:: { paths, validate_package_name, Config } ;
5
+ use git2:: Config as GitConfig ;
6
+ use git2:: Repository as GitRepository ;
7
+ use serde:: de;
8
+ use serde:: Deserialize ;
1
9
use std:: collections:: BTreeMap ;
2
10
use std:: env;
3
11
use std:: fmt;
4
12
use std:: fs;
5
13
use std:: io:: { BufRead , BufReader , ErrorKind } ;
6
14
use std:: path:: { Path , PathBuf } ;
7
-
8
- use git2:: Config as GitConfig ;
9
- use git2:: Repository as GitRepository ;
10
-
11
- use crate :: core:: { compiler, Workspace } ;
12
- use crate :: util:: errors:: { self , CargoResult , CargoResultExt } ;
13
- use crate :: util:: { existing_vcs_repo, internal, FossilRepo , GitRepo , HgRepo , PijulRepo } ;
14
- use crate :: util:: { paths, validate_package_name, Config } ;
15
+ use std:: str:: FromStr ;
15
16
16
17
use toml;
17
18
@@ -24,6 +25,31 @@ pub enum VersionControl {
24
25
NoVcs ,
25
26
}
26
27
28
+ impl FromStr for VersionControl {
29
+ type Err = failure:: Error ;
30
+
31
+ fn from_str ( s : & str ) -> Result < Self , failure:: Error > {
32
+ match s {
33
+ "git" => Ok ( VersionControl :: Git ) ,
34
+ "hg" => Ok ( VersionControl :: Hg ) ,
35
+ "pijul" => Ok ( VersionControl :: Pijul ) ,
36
+ "fossil" => Ok ( VersionControl :: Fossil ) ,
37
+ "none" => Ok ( VersionControl :: NoVcs ) ,
38
+ other => failure:: bail!( "unknown vcs specification: `{}`" , other) ,
39
+ }
40
+ }
41
+ }
42
+
43
+ impl < ' de > de:: Deserialize < ' de > for VersionControl {
44
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
45
+ where
46
+ D : de:: Deserializer < ' de > ,
47
+ {
48
+ let s = String :: deserialize ( deserializer) ?;
49
+ FromStr :: from_str ( & s) . map_err ( de:: Error :: custom)
50
+ }
51
+ }
52
+
27
53
#[ derive( Debug ) ]
28
54
pub struct NewOptions {
29
55
pub version_control : Option < VersionControl > ,
@@ -102,9 +128,11 @@ impl NewOptions {
102
128
}
103
129
}
104
130
131
+ #[ derive( Deserialize ) ]
105
132
struct CargoNewConfig {
106
133
name : Option < String > ,
107
134
email : Option < String > ,
135
+ #[ serde( rename = "vcs" ) ]
108
136
version_control : Option < VersionControl > ,
109
137
}
110
138
@@ -543,7 +571,7 @@ fn init_vcs(path: &Path, vcs: VersionControl, config: &Config) -> CargoResult<()
543
571
fn mk ( config : & Config , opts : & MkOptions < ' _ > ) -> CargoResult < ( ) > {
544
572
let path = opts. path ;
545
573
let name = opts. name ;
546
- let cfg = global_config ( config) ?;
574
+ let cfg = config. get :: < CargoNewConfig > ( "cargo-new" ) ?;
547
575
548
576
// Using the push method with two arguments ensures that the entries for
549
577
// both `ignore` and `hgignore` are in sync.
@@ -752,30 +780,3 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
752
780
753
781
Ok ( ( name, email) )
754
782
}
755
-
756
- fn global_config ( config : & Config ) -> CargoResult < CargoNewConfig > {
757
- let name = config. get_string ( "cargo-new.name" ) ?. map ( |s| s. val ) ;
758
- let email = config. get_string ( "cargo-new.email" ) ?. map ( |s| s. val ) ;
759
- let vcs = config. get_string ( "cargo-new.vcs" ) ?;
760
-
761
- let vcs = match vcs. as_ref ( ) . map ( |p| ( & p. val [ ..] , & p. definition ) ) {
762
- Some ( ( "git" , _) ) => Some ( VersionControl :: Git ) ,
763
- Some ( ( "hg" , _) ) => Some ( VersionControl :: Hg ) ,
764
- Some ( ( "pijul" , _) ) => Some ( VersionControl :: Pijul ) ,
765
- Some ( ( "none" , _) ) => Some ( VersionControl :: NoVcs ) ,
766
- Some ( ( s, p) ) => {
767
- return Err ( internal ( format ! (
768
- "invalid configuration for key \
769
- `cargo-new.vcs`, unknown vcs `{}` \
770
- (found in {})",
771
- s, p
772
- ) ) ) ;
773
- }
774
- None => None ,
775
- } ;
776
- Ok ( CargoNewConfig {
777
- name,
778
- email,
779
- version_control : vcs,
780
- } )
781
- }
0 commit comments