@@ -6,16 +6,6 @@ use crate::util::restricted_names;
6
6
use crate :: CargoResult ;
7
7
use crate :: GlobalContext ;
8
8
9
- const DEFAULT_EDITION : crate :: core:: features:: Edition =
10
- crate :: core:: features:: Edition :: LATEST_STABLE ;
11
- const AUTO_FIELDS : & [ & str ] = & [
12
- "autolib" ,
13
- "autobins" ,
14
- "autoexamples" ,
15
- "autotests" ,
16
- "autobenches" ,
17
- ] ;
18
-
19
9
pub ( super ) fn expand_manifest (
20
10
content : & str ,
21
11
path : & std:: path:: Path ,
@@ -64,88 +54,46 @@ pub(super) fn expand_manifest(
64
54
}
65
55
cargo_util:: paths:: write_if_changed ( & hacked_path, hacked_source) ?;
66
56
67
- let manifest = expand_manifest_ ( & frontmatter, & hacked_path, gctx )
68
- . with_context ( || format ! ( "failed to parse manifest at {} " , path. display( ) ) ) ?;
57
+ let manifest = inject_bin_path ( & frontmatter, & hacked_path)
58
+ . with_context ( || format ! ( "failed to parse manifest at `{}` " , path. display( ) ) ) ?;
69
59
let manifest = toml:: to_string_pretty ( & manifest) ?;
70
60
Ok ( manifest)
71
61
} else {
72
62
let frontmatter = "" ;
73
- let manifest = expand_manifest_ ( frontmatter, path, gctx )
74
- . with_context ( || format ! ( "failed to parse manifest at {} " , path. display( ) ) ) ?;
63
+ let manifest = inject_bin_path ( frontmatter, path)
64
+ . with_context ( || format ! ( "failed to parse manifest at `{}` " , path. display( ) ) ) ?;
75
65
let manifest = toml:: to_string_pretty ( & manifest) ?;
76
66
Ok ( manifest)
77
67
}
78
68
}
79
69
80
- fn expand_manifest_ (
81
- manifest : & str ,
82
- path : & std:: path:: Path ,
83
- gctx : & GlobalContext ,
84
- ) -> CargoResult < toml:: Table > {
70
+ /// HACK: Add a `[[bin]]` table to the `original_toml`
71
+ fn inject_bin_path ( manifest : & str , path : & std:: path:: Path ) -> CargoResult < toml:: Table > {
85
72
let mut manifest: toml:: Table = toml:: from_str ( & manifest) ?;
86
73
87
- for key in [ "workspace" , "lib" , "bin" , "example" , "test" , "bench" ] {
88
- if manifest. contains_key ( key) {
89
- anyhow:: bail!( "`{key}` is not allowed in embedded manifests" )
90
- }
91
- }
92
-
93
- // Prevent looking for a workspace by `read_manifest_from_str`
94
- manifest. insert ( "workspace" . to_owned ( ) , toml:: Table :: new ( ) . into ( ) ) ;
95
-
96
- let package = manifest
97
- . entry ( "package" . to_owned ( ) )
98
- . or_insert_with ( || toml:: Table :: new ( ) . into ( ) )
99
- . as_table_mut ( )
100
- . ok_or_else ( || anyhow:: format_err!( "`package` must be a table" ) ) ?;
101
- for key in [ "workspace" , "build" , "links" ]
102
- . iter ( )
103
- . chain ( AUTO_FIELDS . iter ( ) )
104
- {
105
- if package. contains_key ( * key) {
106
- anyhow:: bail!( "`package.{key}` is not allowed in embedded manifests" )
107
- }
108
- }
109
- // HACK: Using an absolute path while `hacked_path` is in use
110
74
let bin_path = path. to_string_lossy ( ) . into_owned ( ) ;
111
75
let file_stem = path
112
76
. file_stem ( )
113
77
. ok_or_else ( || anyhow:: format_err!( "no file name" ) ) ?
114
78
. to_string_lossy ( ) ;
115
79
let name = sanitize_name ( file_stem. as_ref ( ) ) ;
116
80
let bin_name = name. clone ( ) ;
117
- package
118
- . entry ( "name" . to_owned ( ) )
119
- . or_insert ( toml:: Value :: String ( name) ) ;
120
- package. entry ( "edition" . to_owned ( ) ) . or_insert_with ( || {
121
- let _ = gctx. shell ( ) . warn ( format_args ! (
122
- "`package.edition` is unspecified, defaulting to `{}`" ,
123
- DEFAULT_EDITION
124
- ) ) ;
125
- toml:: Value :: String ( DEFAULT_EDITION . to_string ( ) )
126
- } ) ;
127
- package
128
- . entry ( "build" . to_owned ( ) )
129
- . or_insert_with ( || toml:: Value :: Boolean ( false ) ) ;
130
- for field in AUTO_FIELDS {
131
- package
132
- . entry ( field. to_owned ( ) )
133
- . or_insert_with ( || toml:: Value :: Boolean ( false ) ) ;
134
- }
135
81
136
82
let mut bin = toml:: Table :: new ( ) ;
137
83
bin. insert ( "name" . to_owned ( ) , toml:: Value :: String ( bin_name) ) ;
138
84
bin. insert ( "path" . to_owned ( ) , toml:: Value :: String ( bin_path) ) ;
139
- manifest. insert (
140
- "bin" . to_owned ( ) ,
141
- toml:: Value :: Array ( vec ! [ toml:: Value :: Table ( bin) ] ) ,
142
- ) ;
85
+ manifest
86
+ . entry ( "bin" )
87
+ . or_insert_with ( || Vec :: < toml:: Value > :: new ( ) . into ( ) )
88
+ . as_array_mut ( )
89
+ . ok_or_else ( || anyhow:: format_err!( "`bin` must be an array" ) ) ?
90
+ . push ( toml:: Value :: Table ( bin) ) ;
143
91
144
92
Ok ( manifest)
145
93
}
146
94
147
95
/// Ensure the package name matches the validation from `ops::cargo_new::check_name`
148
- fn sanitize_name ( name : & str ) -> String {
96
+ pub fn sanitize_name ( name : & str ) -> String {
149
97
let placeholder = if name. contains ( '_' ) {
150
98
'_'
151
99
} else {
@@ -565,18 +513,6 @@ fn main() {}
565
513
name = "test-"
566
514
path = "/home/me/test.rs"
567
515
568
- [package]
569
- autobenches = false
570
- autobins = false
571
- autoexamples = false
572
- autolib = false
573
- autotests = false
574
- build = false
575
- edition = "2024"
576
- name = "test-"
577
-
578
- [workspace]
579
-
580
516
"# ] ]
581
517
) ;
582
518
}
@@ -600,18 +536,6 @@ path = [..]
600
536
[dependencies]
601
537
time = "0.1.25"
602
538
603
- [package]
604
- autobenches = false
605
- autobins = false
606
- autoexamples = false
607
- autolib = false
608
- autotests = false
609
- build = false
610
- edition = "2024"
611
- name = "test-"
612
-
613
- [workspace]
614
-
615
539
"# ] ]
616
540
) ;
617
541
}
0 commit comments