Cargo Sort
A tool to check that your Cargo.toml dependencies are sorted alphabetically. Project created as a solution to @dtolnay's request for implementation #29. Cross platform implementation, windows compatible. Terminal coloring works on both cmd.exe and powershell. Checks/sorts by key in tables and also nested table headers (does not sort the items in a nested header, sorts the table itself). cargo sort uses toml-edit to parse the toml file into something useful.
The --format option may result in improperly formatted toml please file an issue.
Use
There are three modes cargo-sort can be used in:
- default
- No flags set cargo-sort will write the sorted result over the input Cargo.toml file.
- -c or --check
- Will fail with a non-zero exit code if the file is unsorted.
- -n or --no-format
- Will NOT format the sorted toml. This option only has an effect if writing or printing out.
- -g or --grouped
- When sorting keep table key value spacing. If you have dependency groups they will stick but be sorted within the grouping.
- -p or --print
- Write the sorted toml file to stdout.
- -w or --workspace
- Checks every crate in the workspace based on flags. Only one root may be given.
Config
cargo sort uses a config file when formatting called tomlfmt.toml. This is optional and defaults will
be used if not found in the current working dir.
Here are the defaults when no tomlfmt.toml is found
# trailing comma in arrays
= false
# trailing comma when multi-line
= true
= true
# remove all the spacing inside the array
= false
# remove all the spacing inside the object
= false
= true
# is it ok to have blank lines inside of a table
= false
= 1
# windows style line endings
= false
included in sort check is:
[]
[]
[]
[]
[]
if you have a header to add open a PR's, they are welcomed.
Install
Run
Thanks to dspicher for issue #4 you can now invoke cargo sort check as a cargo subcommand
Wildcard expansion is supported so you can do this
or any other pattern that is supported by your terminal. This also means multiple paths work.
Finally cargo sort has the --workspace flag and will sort each Cargo.toml file in a workspace
These are all valid. File names and extensions can be used on some of the paths but not others, if left off the tool will default to Cargo.toml.
<CWD>...
Examples
[]
="0.1.1"
# comments will stay with the item
="0.1.1"
# If --grouped is used the blank line will stay.
="0.1.1"
[]
="0"
[]
="0"
="0"
# comments will also stay with header
[]
="0"
[]
="0"
[]
="0"
="0"
Will sort to, or fail until organized like so
[]
="0.1.1"
# If --grouped is used the blank line will stay
="0.1.1"
# comments will stay with the item
="0.1.1"
[]
="0"
[]
="0"
# comments will also stay with header
[]
="0"
# Tables are ordered by their appearance so
# if dev-dependencies was before build it would be
# sorted that way
[]
="0"
="0"
[]
="0"
="0"