Skip to content

Commit

Permalink
ensure table_order always contain a valid value
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 31, 2024
1 parent 8063b2f commit 9cf2084
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ const NEWLINE_PATTERN: &str = "\r\n";
#[cfg(not(target_os = "windows"))]
const NEWLINE_PATTERN: &str = "\n";

pub(crate) const DEF_TABLE_ORDER: &[&str] = &[
"package",
"workspace",
"lib",
"bin",
"features",
"dependencies",
"build-dependencies",
"dev-dependencies",
];

/// The config file for formatting toml after sorting.
///
/// Use the `FromStr` to create a config from a string.
Expand Down Expand Up @@ -98,16 +109,7 @@ impl Config {
key_value_newlines: true,
allowed_blank_lines: 1,
crlf: false,
table_order: [
"package",
"features",
"dependencies",
"build-dependencies",
"dev-dependencies",
]
.iter()
.map(|s| (*s).to_owned())
.collect(),
table_order: DEF_TABLE_ORDER.iter().map(|s| (*s).to_owned()).collect(),
}
}
}
Expand Down Expand Up @@ -165,11 +167,15 @@ impl FromStr for Config {
table_order: toml
.get("table_order")
.and_then(toml_edit::Item::as_array)
.into_iter()
.flatten()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect(),
.map_or(
DEF_TABLE_ORDER.iter().map(|s| (*s).to_owned()).collect(),
|arr| {
arr.into_iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect()
},
),
})
}
}
Expand Down

0 comments on commit 9cf2084

Please sign in to comment.