Skip to content

Commit 77ad49f

Browse files
committed
don't panic
1 parent 2cb56ee commit 77ad49f

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/format_report_formatter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,8 @@ fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationTy
146146
| ErrorKind::InvalidGlobPattern(_)
147147
| ErrorKind::VersionMismatch => AnnotationType::Error,
148148
ErrorKind::DeprecatedAttr => AnnotationType::Warning,
149+
ErrorKind::CargoTomlError(_) => {
150+
unreachable!("Cargo.toml formatting error is not contained in FormatReport.")
151+
}
149152
}
150153
}

src/formatting/cargo_toml.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<
1515
&mut SortSection {
1616
current_position: 0,
1717
} as &mut dyn VisitMut,
18-
&mut SortKey,
1918
&mut BlankLine { trimming: true },
2019
&mut KeyValue,
2120
&mut MultiLine,
@@ -31,13 +30,19 @@ pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<
3130
for rule in rules.into_iter() {
3231
rule.visit_document_mut(&mut doc);
3332
}
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+
}
3439

3540
Ok(doc.to_string())
3641
}
3742

3843
impl From<TomlError> for ErrorKind {
39-
fn from(_: TomlError) -> Self {
40-
ErrorKind::ParseError
44+
fn from(e: TomlError) -> Self {
45+
ErrorKind::CargoTomlError(format!("{e}"))
4146
}
4247
}
4348

@@ -48,7 +53,9 @@ impl From<TomlError> for ErrorKind {
4853
/// put the `name` and `version` keys in that order at the top of that section,
4954
/// followed by the remaining keys other than `description` in alphabetical order,
5055
/// followed by the `description` at the end of that section.
51-
struct SortKey;
56+
struct SortKey {
57+
error: Option<ErrorKind>,
58+
}
5259

5360
/// Put the `[package]` section at the top of the file
5461
struct SortSection {
@@ -119,7 +126,15 @@ impl VisitMut for SortKey {
119126
fn visit_document_mut(&mut self, doc: &mut Document) {
120127
doc.as_table_mut().iter_mut().for_each(|(key, section)| {
121128
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+
};
123138
// "name" is the first, "version" is the second, "description" is the last
124139
// everything else is sorted alphabetically
125140
table.sort_values_by(|k1, _, k2, _| match (k1.get(), k2.get()) {

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ pub enum ErrorKind {
133133
/// Invalid glob pattern in `ignore` configuration option.
134134
#[error("Invalid glob pattern found in ignore list: {0}")]
135135
InvalidGlobPattern(ignore::Error),
136+
#[error("Error when formatting Cargo.toml: {0}")]
137+
CargoTomlError(String),
136138
}
137139

138140
impl ErrorKind {

0 commit comments

Comments
 (0)