@@ -80,8 +80,9 @@ use anyhow::{anyhow, bail, format_err, Context as _};
80
80
use cargo_util:: paths;
81
81
use curl:: easy:: Easy ;
82
82
use lazycell:: LazyCell ;
83
+ use serde:: de:: IntoDeserializer as _;
83
84
use serde:: Deserialize ;
84
- use toml_edit:: { easy as toml , Item } ;
85
+ use toml_edit:: Item ;
85
86
use url:: Url ;
86
87
87
88
mod de;
@@ -895,17 +896,11 @@ impl Config {
895
896
let def = Definition :: Environment ( key. as_env_key ( ) . to_string ( ) ) ;
896
897
if self . cli_unstable ( ) . advanced_env && env_val. starts_with ( '[' ) && env_val. ends_with ( ']' ) {
897
898
// Parse an environment string as a TOML array.
898
- let toml_s = format ! ( "value={}" , env_val) ;
899
- let toml_v: toml:: Value = toml:: de:: from_str ( & toml_s) . map_err ( |e| {
900
- ConfigError :: new ( format ! ( "could not parse TOML list: {}" , e) , def. clone ( ) )
901
- } ) ?;
902
- let values = toml_v
903
- . as_table ( )
904
- . unwrap ( )
905
- . get ( "value" )
906
- . unwrap ( )
907
- . as_array ( )
908
- . expect ( "env var was not array" ) ;
899
+ let toml_v = toml:: Value :: deserialize ( toml:: de:: ValueDeserializer :: new ( & env_val) )
900
+ . map_err ( |e| {
901
+ ConfigError :: new ( format ! ( "could not parse TOML list: {}" , e) , def. clone ( ) )
902
+ } ) ?;
903
+ let values = toml_v. as_array ( ) . expect ( "env var was not array" ) ;
909
904
for value in values {
910
905
// TODO: support other types.
911
906
let s = value. as_str ( ) . ok_or_else ( || {
@@ -1180,14 +1175,14 @@ impl Config {
1180
1175
}
1181
1176
let contents = fs:: read_to_string ( path)
1182
1177
. with_context ( || format ! ( "failed to read configuration file `{}`" , path. display( ) ) ) ?;
1183
- let toml = cargo_toml:: parse ( & contents, path, self ) . with_context ( || {
1178
+ let toml = cargo_toml:: parse_document ( & contents, path, self ) . with_context ( || {
1184
1179
format ! ( "could not parse TOML configuration in `{}`" , path. display( ) )
1185
1180
} ) ?;
1186
1181
let def = match why_load {
1187
1182
WhyLoad :: Cli => Definition :: Cli ( Some ( path. into ( ) ) ) ,
1188
1183
WhyLoad :: FileDiscovery => Definition :: Path ( path. into ( ) ) ,
1189
1184
} ;
1190
- let value = CV :: from_toml ( def, toml) . with_context ( || {
1185
+ let value = CV :: from_toml ( def, toml:: Value :: Table ( toml ) ) . with_context ( || {
1191
1186
format ! (
1192
1187
"failed to load TOML configuration from `{}`" ,
1193
1188
path. display( )
@@ -1302,8 +1297,10 @@ impl Config {
1302
1297
format ! ( "failed to parse value from --config argument `{arg}` as a dotted key expression" )
1303
1298
} ) ?;
1304
1299
fn non_empty_decor ( d : & toml_edit:: Decor ) -> bool {
1305
- d. prefix ( ) . map_or ( false , |p| !p. trim ( ) . is_empty ( ) )
1306
- || d. suffix ( ) . map_or ( false , |s| !s. trim ( ) . is_empty ( ) )
1300
+ d. prefix ( )
1301
+ . map_or ( false , |p| !p. as_str ( ) . unwrap_or_default ( ) . trim ( ) . is_empty ( ) )
1302
+ || d. suffix ( )
1303
+ . map_or ( false , |s| !s. as_str ( ) . unwrap_or_default ( ) . trim ( ) . is_empty ( ) )
1307
1304
}
1308
1305
let ok = {
1309
1306
let mut got_to_value = false ;
@@ -1363,9 +1360,10 @@ impl Config {
1363
1360
) ;
1364
1361
}
1365
1362
1366
- let toml_v: toml:: Value = toml:: from_document ( doc) . with_context ( || {
1367
- format ! ( "failed to parse value from --config argument `{arg}`" )
1368
- } ) ?;
1363
+ let toml_v: toml:: Value = toml:: Value :: deserialize ( doc. into_deserializer ( ) )
1364
+ . with_context ( || {
1365
+ format ! ( "failed to parse value from --config argument `{arg}`" )
1366
+ } ) ?;
1369
1367
1370
1368
if toml_v
1371
1369
. get ( "registry" )
@@ -2171,14 +2169,12 @@ pub fn save_credentials(
2171
2169
)
2172
2170
} ) ?;
2173
2171
2174
- let mut toml = cargo_toml:: parse ( & contents, file. path ( ) , cfg) ?;
2172
+ let mut toml = cargo_toml:: parse_document ( & contents, file. path ( ) , cfg) ?;
2175
2173
2176
2174
// Move the old token location to the new one.
2177
- if let Some ( token) = toml. as_table_mut ( ) . unwrap ( ) . remove ( "token" ) {
2175
+ if let Some ( token) = toml. remove ( "token" ) {
2178
2176
let map = HashMap :: from ( [ ( "token" . to_string ( ) , token) ] ) ;
2179
- toml. as_table_mut ( )
2180
- . unwrap ( )
2181
- . insert ( "registry" . into ( ) , map. into ( ) ) ;
2177
+ toml. insert ( "registry" . into ( ) , map. into ( ) ) ;
2182
2178
}
2183
2179
2184
2180
if let Some ( token) = token {
@@ -2225,17 +2221,16 @@ pub fn save_credentials(
2225
2221
} ;
2226
2222
2227
2223
if registry. is_some ( ) {
2228
- if let Some ( table) = toml. as_table_mut ( ) . unwrap ( ) . remove ( "registries" ) {
2224
+ if let Some ( table) = toml. remove ( "registries" ) {
2229
2225
let v = CV :: from_toml ( path_def, table) ?;
2230
2226
value. merge ( v, false ) ?;
2231
2227
}
2232
2228
}
2233
- toml. as_table_mut ( ) . unwrap ( ) . insert ( key, value. into_toml ( ) ) ;
2229
+ toml. insert ( key, value. into_toml ( ) ) ;
2234
2230
} else {
2235
2231
// logout
2236
- let table = toml. as_table_mut ( ) . unwrap ( ) ;
2237
2232
if let Some ( registry) = registry {
2238
- if let Some ( registries) = table . get_mut ( "registries" ) {
2233
+ if let Some ( registries) = toml . get_mut ( "registries" ) {
2239
2234
if let Some ( reg) = registries. get_mut ( registry) {
2240
2235
let rtable = reg. as_table_mut ( ) . ok_or_else ( || {
2241
2236
format_err ! ( "expected `[registries.{}]` to be a table" , registry)
@@ -2245,7 +2240,7 @@ pub fn save_credentials(
2245
2240
rtable. remove ( "secret-key-subject" ) ;
2246
2241
}
2247
2242
}
2248
- } else if let Some ( registry) = table . get_mut ( "registry" ) {
2243
+ } else if let Some ( registry) = toml . get_mut ( "registry" ) {
2249
2244
let reg_table = registry
2250
2245
. as_table_mut ( )
2251
2246
. ok_or_else ( || format_err ! ( "expected `[registry]` to be a table" ) ) ?;
0 commit comments