@@ -15,7 +15,6 @@ pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<
15
15
& mut SortSection {
16
16
current_position : 0 ,
17
17
} as & mut dyn VisitMut ,
18
- & mut SortKey ,
19
18
& mut BlankLine { trimming : true } ,
20
19
& mut KeyValue ,
21
20
& mut MultiLine ,
@@ -31,13 +30,19 @@ pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<
31
30
for rule in rules. into_iter ( ) {
32
31
rule. visit_document_mut ( & mut doc) ;
33
32
}
33
+ // Special handling for falliable rules.
34
+ let mut rule = SortKey { error : None } ;
35
+ rule. visit_document_mut ( & mut doc) ;
36
+ if let Some ( e) = rule. error {
37
+ return Err ( e) ;
38
+ }
34
39
35
40
Ok ( doc. to_string ( ) )
36
41
}
37
42
38
43
impl From < TomlError > for ErrorKind {
39
- fn from ( _ : TomlError ) -> Self {
40
- ErrorKind :: ParseError
44
+ fn from ( e : TomlError ) -> Self {
45
+ ErrorKind :: CargoTomlError ( format ! ( "{e}" ) )
41
46
}
42
47
}
43
48
@@ -48,7 +53,9 @@ impl From<TomlError> for ErrorKind {
48
53
/// put the `name` and `version` keys in that order at the top of that section,
49
54
/// followed by the remaining keys other than `description` in alphabetical order,
50
55
/// followed by the `description` at the end of that section.
51
- struct SortKey ;
56
+ struct SortKey {
57
+ error : Option < ErrorKind > ,
58
+ }
52
59
53
60
/// Put the `[package]` section at the top of the file
54
61
struct SortSection {
@@ -119,7 +126,15 @@ impl VisitMut for SortKey {
119
126
fn visit_document_mut ( & mut self , doc : & mut Document ) {
120
127
doc. as_table_mut ( ) . iter_mut ( ) . for_each ( |( key, section) | {
121
128
if key == "package" {
122
- let table = section. as_table_mut ( ) . expect ( "package should be a table" ) ;
129
+ let table = match section. as_table_mut ( ) {
130
+ Some ( table) => table,
131
+ None => {
132
+ self . error = Some ( ErrorKind :: CargoTomlError (
133
+ "package should be a table" . into ( ) ,
134
+ ) ) ;
135
+ return ;
136
+ }
137
+ } ;
123
138
// "name" is the first, "version" is the second, "description" is the last
124
139
// everything else is sorted alphabetically
125
140
table. sort_values_by ( |k1, _, k2, _| match ( k1. get ( ) , k2. get ( ) ) {
0 commit comments