1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +00:00

feat: toml config

This commit is contained in:
Kevin Amado 2024-12-08 19:39:45 -07:00
parent 1bd3c5832a
commit 25281a25c9
4 changed files with 83 additions and 20 deletions

84
Cargo.lock generated
View file

@ -22,7 +22,7 @@ dependencies = [
"futures",
"num_cpus",
"rand",
"serde_json",
"toml",
"walkdir",
]
@ -83,7 +83,7 @@ dependencies = [
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"indexmap 1.9.3",
"once_cell",
"strsim",
"termcolor",
@ -124,6 +124,12 @@ version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "futures"
version = "0.3.31"
@ -225,6 +231,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
[[package]]
name = "heck"
version = "0.4.1"
@ -257,10 +269,14 @@ dependencies = [
]
[[package]]
name = "itoa"
version = "1.0.13"
name = "indexmap"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2"
checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
dependencies = [
"equivalent",
"hashbrown 0.15.2",
]
[[package]]
name = "libc"
@ -445,12 +461,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "same-file"
version = "1.0.6"
@ -481,14 +491,11 @@ dependencies = [
]
[[package]]
name = "serde_json"
version = "1.0.133"
name = "serde_spanned"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
@ -565,6 +572,40 @@ version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9"
[[package]]
name = "toml"
version = "0.8.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5"
dependencies = [
"indexmap 2.7.0",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"
@ -697,6 +738,15 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.6.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
dependencies = [
"memchr",
]
[[package]]
name = "yansi"
version = "1.0.1"

View file

@ -2,8 +2,10 @@ use serde::Deserialize;
/// Configuration used by the formatter
#[derive(Clone, Copy, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// Indentation to use
#[serde(default)]
pub indentation: Indentation,
}

View file

@ -19,7 +19,7 @@ rand = { version = "*", default-features = false, features = [
"alloc",
"getrandom",
] }
serde_json = { version = "*", default-features = false, features = ["std"] }
toml = { version = "*", default-features = false, features = ["parse"] }
walkdir = { version = "*", default-features = false, features = [] }
[package]

View file

@ -40,7 +40,9 @@ struct CLIArgs {
#[clap(long, short)]
check: bool,
/// [Experimental] Path to a config file.
/// [Experimental] Path to a config file. If not provided, it'll default to
/// `alejandra.toml` in the current directory. If not found, it'll use the
/// default style.
#[clap(long)]
experimental_config: Option<String>,
@ -238,6 +240,15 @@ pub fn main() -> ! {
}
fn resolve_config(path: Option<&str>, verbosity: Verbosity) -> Config {
let default_config_path = "alejandra.toml";
// If no path was provided and the default config path exists, use it
let path = path.or_else(|| {
std::fs::exists(default_config_path)
.unwrap_or(false)
.then_some(default_config_path)
});
if let Some(path) = path {
if verbosity.allows_info() {
eprintln!("Using config from: {}", path);
@ -250,7 +261,7 @@ fn resolve_config(path: Option<&str>, verbosity: Verbosity) -> Config {
std::process::exit(1);
});
serde_json::from_str::<Config>(&contents).unwrap_or_else(|error| {
toml::from_str::<Config>(&contents).unwrap_or_else(|error| {
if verbosity.allows_errors() {
eprintln!("Errors found in config: {}", error);
}