From a6c71de14fc9d99897e5a838d42e464a44739255 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 19 Nov 2022 22:08:36 -0500 Subject: [PATCH] fix: handle single array item in command.associations (multiple not yet implemented) (#17) --- Cargo.lock | 216 +++++++++++++++++++++------------- Cargo.toml | 16 +-- deployment/schema.json | 27 ++++- dprint.json | 7 +- rust-toolchain.toml | 2 +- scripts/create_plugin_file.ts | 6 +- src/configuration.rs | 133 +++++++++++++++++---- src/handler.rs | 4 +- 8 files changed, 289 insertions(+), 122 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3db44f1..8f3a72c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,9 +22,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.53" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0" +checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" [[package]] name = "autocfg" @@ -70,9 +70,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.9.1" +version = "3.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" +checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] name = "byte-tools" @@ -115,9 +115,9 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.4" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aaa7bd5fb665c6864b5f963dd9097905c54125909c7aa94c9e18507cdbe6c53" +checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" dependencies = [ "cfg-if", "crossbeam-utils", @@ -160,9 +160,9 @@ dependencies = [ [[package]] name = "dprint-core" -version = "0.56.0" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f2e06453dc1b63189dcfb0aae5bb98ad05f0d0d4f7e1647473f85921fcd1412" +checksum = "84600c297cc99fc088a9a916286d71915c988fa3a6f1bbc994ad9b93dde80c03" dependencies = [ "anyhow", "bumpalo", @@ -180,9 +180,9 @@ dependencies = [ [[package]] name = "dprint-development" -version = "0.8.0" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f4d150ee861fd47009f4663b2bc2935886dee976a7ad168afa785098bc5896e" +checksum = "156dcd0b16dd5424774ccc56863c2990a7fb337aa0c9ac293bbe3cf6f632e297" dependencies = [ "anyhow", "console", @@ -246,9 +246,9 @@ dependencies = [ [[package]] name = "globset" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10463d9ff00a2a068db14231982f5132edebad0d7660cd956a1c30292dbcbfbd" +checksum = "0a1e17342619edbc21a964c2afbeb6c820c6a2560032872f397bb97ea127bd0a" dependencies = [ "aho-corasick", "bstr", @@ -259,23 +259,23 @@ dependencies = [ [[package]] name = "handlebars" -version = "4.2.1" +version = "4.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25546a65e5cf1f471f3438796fc634650b31d7fcde01d444c309aeb28b92e3a8" +checksum = "433e4ab33f1213cdc25b5fa45c76881240cfe79284cf2b395e8b9e312a30a2fd" dependencies = [ "log", "pest", "pest_derive", - "quick-error", "serde", "serde_json", + "thiserror", ] [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "hermit-abi" @@ -288,9 +288,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.8.0" +version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", "hashbrown", @@ -311,9 +311,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.117" +version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" +checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" [[package]] name = "lock_api" @@ -347,33 +347,14 @@ checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "mio" -version = "0.8.0" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" +checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" dependencies = [ "libc", "log", - "miow", - "ntapi", - "winapi", -] - -[[package]] -name = "miow" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" -dependencies = [ - "winapi", -] - -[[package]] -name = "ntapi" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f" -dependencies = [ - "winapi", + "wasi", + "windows-sys 0.42.0", ] [[package]] @@ -386,12 +367,6 @@ dependencies = [ "libc", ] -[[package]] -name = "once_cell" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" - [[package]] name = "opaque-debug" version = "0.2.3" @@ -409,9 +384,9 @@ dependencies = [ [[package]] name = "parking_lot" -version = "0.12.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ "lock_api", "parking_lot_core", @@ -427,7 +402,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-sys 0.32.0", ] [[package]] @@ -493,19 +468,13 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.36" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" dependencies = [ - "unicode-xid", + "unicode-ident", ] -[[package]] -name = "quick-error" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" - [[package]] name = "quote" version = "1.0.15" @@ -561,18 +530,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.136" +version = "1.0.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.136" +version = "1.0.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" dependencies = [ "proc-macro2", "quote", @@ -636,19 +605,19 @@ dependencies = [ [[package]] name = "splitty" -version = "0.1.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8190298e89d4080e9c025535f73cacec4d51ff4495819dd8fdf1ae374c75ee80" +checksum = "8dae68aa5bd5dc2d3a2137b0f6bcdd8255dce1983dc155fe0246572e179c9c3a" [[package]] name = "syn" -version = "1.0.86" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] @@ -661,18 +630,38 @@ dependencies = [ "winapi", ] +[[package]] +name = "thiserror" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio" -version = "1.17.0" +version = "1.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee" +checksum = "d76ce4a75fb488c605c54bf610f221cea8b0dafb53333c1a67e8ee199dcd2ae3" dependencies = [ + "autocfg", "bytes", "libc", "memchr", "mio", "num_cpus", - "once_cell", "parking_lot", "pin-project-lite", "signal-hook-registry", @@ -718,6 +707,12 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + [[package]] name = "unicode-width" version = "0.1.9" @@ -725,10 +720,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" [[package]] -name = "unicode-xid" -version = "0.2.2" +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "winapi" @@ -758,39 +753,96 @@ version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3df6e476185f92a12c072be4a189a0210dcdcf512a1891d6dff9edb874deadc6" dependencies = [ - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_msvc", + "windows_aarch64_msvc 0.32.0", + "windows_i686_gnu 0.32.0", + "windows_i686_msvc 0.32.0", + "windows_x86_64_gnu 0.32.0", + "windows_x86_64_msvc 0.32.0", +] + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc 0.42.0", + "windows_i686_gnu 0.42.0", + "windows_i686_msvc 0.42.0", + "windows_x86_64_gnu 0.42.0", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc 0.42.0", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + [[package]] name = "windows_aarch64_msvc" version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" + [[package]] name = "windows_i686_gnu" version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615" +[[package]] +name = "windows_i686_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" + [[package]] name = "windows_i686_msvc" version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172" +[[package]] +name = "windows_i686_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" + [[package]] name = "windows_x86_64_gnu" version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" + [[package]] name = "windows_x86_64_msvc" version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" diff --git a/Cargo.toml b/Cargo.toml index 4349557..5caee4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,14 +18,14 @@ overflow-checks = false panic = "abort" [dependencies] -anyhow = "1.0.51" -dprint-core = { version = "0.56.0", features = ["process"] } -globset = "0.4.8" -handlebars = "4.1.6" -serde = { version = "1.0.118", features = ["derive"] } -splitty = "0.1.0" -tokio = { version = "1.15.0", features = ["full", "time"] } +anyhow = "1.0.66" +dprint-core = { version = "0.59.0", features = ["process"] } +globset = "0.4.9" +handlebars = "4.3.5" +serde = { version = "1.0.147", features = ["derive"] } +splitty = "1.0.1" +tokio = { version = "1.22.0", features = ["full", "time"] } [dev-dependencies] -dprint-development = "0.8.0" +dprint-development = "0.9.2" pretty_assertions = "1.1.0" diff --git a/deployment/schema.json b/deployment/schema.json index a68327f..c96b535 100644 --- a/deployment/schema.json +++ b/deployment/schema.json @@ -2,6 +2,7 @@ "$schema": "http://json-schema.org/draft-07/schema#", "$id": "https://plugins.dprint.dev/dprint/dprint-plugin-exec/0.0.0/schema.json", "type": "object", + "required": ["associations"], "properties": { "locked": { "description": "Whether the configuration is not allowed to be overriden or extended.", @@ -47,6 +48,19 @@ "description": "Uses the system standard (ex. crlf on Windows)." }] }, + "associations": { + "description": "Glob pattern that associates this plugin with certain file paths (ex. \"**/*.{rs,java,py}\").", + "anyOf": [{ + "description": "Glob pattern that associates this plugin with certain file paths (ex. \"**/*.{rs,java,py}\").", + "type": "string" + }, { + "description": "Glob patterns that associates this plugin with certain file paths.", + "type": "array", + "items": { + "type": "string" + } + }] + }, "cacheKey": { "description": "Optional value used to bust dprint's incremental cache (ex. provide \"1\"). This is useful if you want to force formatting to occur because the underlying command has changed.", "type": "string" @@ -59,8 +73,17 @@ }, "patternProperties": { "^.*\\.associations$": { - "type": "string", - "description": "The pattern to use to match a file path. Ex. \"rustfmt.associations\": \"**/*.rs\"" + "description": "Glob pattern that associates certain file paths with this command (ex. \"rustfmt.associations\": \"**/*.rs\").", + "anyOf": [{ + "description": "Glob pattern that associates certain file paths with this command (ex. \"rustfmt.associations\": \"**/*.rs\").", + "type": "string" + }, { + "description": "Glob patterns that associates certain file paths with this command.", + "type": "array", + "items": { + "type": "string" + } + }] }, "^.*\\.cwd$": { "type": "string", diff --git a/dprint.json b/dprint.json index 3fd69d0..3ef4d38 100644 --- a/dprint.json +++ b/dprint.json @@ -2,8 +2,9 @@ "incremental": true, "indentWidth": 2, "lineWidth": 100, - "rustfmt": { - "imports_granularity": "Item" + "exec": { + "associations": "**/*.rs", + "rustfmt": "rustfmt --edition 2021" }, "includes": [ "**/*.{json,md,html,scss,css,toml,rs,xml}" @@ -17,6 +18,6 @@ "https://plugins.dprint.dev/json-0.14.1.wasm", "https://plugins.dprint.dev/markdown-0.12.2.wasm", "https://plugins.dprint.dev/toml-0.5.4.wasm", - "https://plugins.dprint.dev/rustfmt-0.6.0.exe-plugin@8b65ed724170bd227e92a2f01d867e452ef7f26e78dc691999ffa37a276df27c" + "https://plugins.dprint.dev/exec-0.3.1.json@9351b67ec7a6b58a69201c2834cba38cb3d191080aefc6422fb1320f03c8fc4d" ] } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index f16b995..ce69938 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.58.1" +channel = "1.65.0" components = ["clippy", "rustfmt"] diff --git a/scripts/create_plugin_file.ts b/scripts/create_plugin_file.ts index 3e914c2..a43cfb3 100644 --- a/scripts/create_plugin_file.ts +++ b/scripts/create_plugin_file.ts @@ -1,12 +1,12 @@ -import * as path from "https://deno.land/std@0.146.0/path/mod.ts"; -import { extractCargoVersion, processPlugin } from "https://raw.githubusercontent.com/dprint/automation/0.4.0/mod.ts"; +import * as path from "https://deno.land/std@0.153.0/path/mod.ts"; +import { extractCargoVersionOrThrow, processPlugin } from "https://raw.githubusercontent.com/dprint/automation/0.5.3/mod.ts"; const currentDirPath = path.dirname(path.fromFileUrl(import.meta.url)); const cargoFilePath = path.join(currentDirPath, "../", "Cargo.toml"); await processPlugin.createDprintOrgProcessPlugin({ pluginName: "dprint-plugin-exec", - version: await extractCargoVersion(cargoFilePath), + version: await extractCargoVersionOrThrow(cargoFilePath), platforms: [ "darwin-aarch64", "darwin-x86_64", diff --git a/src/configuration.rs b/src/configuration.rs index 505ae8d..92420e1 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -2,6 +2,7 @@ use dprint_core::configuration::get_nullable_value; use dprint_core::configuration::get_unknown_property_diagnostics; use dprint_core::configuration::get_value; use dprint_core::configuration::ConfigKeyMap; +use dprint_core::configuration::ConfigKeyValue; use dprint_core::configuration::ConfigurationDiagnostic; use dprint_core::configuration::GlobalConfiguration; use dprint_core::configuration::NewLineKind; @@ -145,9 +146,39 @@ impl Configuration { args: command, associations: { let associations_key = format!("{}.associations", command_key); - let value: Option = - get_nullable_value(&mut config, &associations_key, &mut diagnostics); - match value { + let maybe_value = config.remove(&associations_key).and_then(|value| match value { + ConfigKeyValue::String(value) => Some(value), + ConfigKeyValue::Array(mut value) => match value.len() { + 0 => None, + 1 => match value.remove(0) { + ConfigKeyValue::String(value) => Some(value), + _ => { + diagnostics.push(ConfigurationDiagnostic { + property_name: associations_key.clone(), + message: "Expected string value in array.".to_string(), + }); + None + } + }, + _ => { + diagnostics.push(ConfigurationDiagnostic { + property_name: associations_key.clone(), + message: "Unfortunately multiple globs haven't been implemented yet. Please provide a single glob or consider contributing this feature." + .to_string(), + }); + None + } + }, + _ => { + diagnostics.push(ConfigurationDiagnostic { + property_name: associations_key.clone(), + message: "Expected string or array value.".to_string(), + }); + None + } + }); + + match maybe_value { Some(value) => { let mut builder = globset::GlobBuilder::new(&value); builder.case_insensitive(cfg!(windows)); @@ -163,11 +194,7 @@ impl Configuration { } } None => { - if resolved_config - .commands - .iter() - .any(|b| b.associations.is_none()) - { + if resolved_config.commands.iter().any(|b| b.associations.is_none()) { diagnostics.push(ConfigurationDiagnostic { property_name: associations_key.to_string(), message: format!( @@ -184,17 +211,8 @@ impl Configuration { } } }, - cwd: get_cwd(get_nullable_value( - &mut config, - &format!("{}.cwd", command_key), - &mut diagnostics, - )), - stdin: get_value( - &mut config, - &format!("{}.stdin", command_key), - true, - &mut diagnostics, - ), + cwd: get_cwd(get_nullable_value(&mut config, &format!("{}.cwd", command_key), &mut diagnostics)), + stdin: get_value(&mut config, &format!("{}.stdin", command_key), true, &mut diagnostics), }); } @@ -298,7 +316,8 @@ mod tests { "A \"command2.associations\" configuration key must be provided because another formatting ", "command is specified without an associations key. (Example: `\"command2.associations\": \"**/*.rs\"` ", "would format .rs files with this command)" - ).to_string(), + ) + .to_string(), }, ConfigurationDiagnostic { property_name: "command3.associations".to_string(), @@ -306,12 +325,84 @@ mod tests { "A \"command3.associations\" configuration key must be provided because another formatting ", "command is specified without an associations key. (Example: `\"command3.associations\": \"**/*.rs\"` ", "would format .rs files with this command)" - ).to_string(), + ) + .to_string(), }, ], ) } + #[test] + fn handle_associations_value() { + let unresolved_config = ConfigKeyMap::from([ + ( + "command.associations".to_string(), + ConfigKeyValue::Array(vec![ConfigKeyValue::from_str("**/*.rs")]), + ), + ("command".to_string(), ConfigKeyValue::from_str("command")), + ]); + let mut config = Configuration::resolve(unresolved_config, &Default::default()).config; + assert!(config.commands.remove(0).associations.is_some()); + + let unresolved_config = ConfigKeyMap::from([ + ( + "command.associations".to_string(), + ConfigKeyValue::Array(vec![]), + ), + ("command".to_string(), ConfigKeyValue::from_str("command")), + ]); + let mut config = Configuration::resolve(unresolved_config, &Default::default()).config; + assert!(config.commands.remove(0).associations.is_none()); + + let unresolved_config = ConfigKeyMap::from([ + ( + "command.associations".to_string(), + ConfigKeyValue::Array(vec![ + ConfigKeyValue::from_str("**/*.rs"), + ConfigKeyValue::from_str("**/*.json"), + ]), + ), + ("command".to_string(), ConfigKeyValue::from_str("command")), + ]); + run_diagnostics_test( + unresolved_config, + vec![ConfigurationDiagnostic { + property_name: "command.associations".to_string(), + message: "Unfortunately multiple globs haven't been implemented yet. Please provide a single glob or consider contributing this feature.".to_string(), + }], + ); + + let unresolved_config = ConfigKeyMap::from([ + ( + "command.associations".to_string(), + ConfigKeyValue::Array(vec![ConfigKeyValue::from_bool(true)]), + ), + ("command".to_string(), ConfigKeyValue::from_str("command")), + ]); + run_diagnostics_test( + unresolved_config, + vec![ConfigurationDiagnostic { + property_name: "command.associations".to_string(), + message: "Expected string value in array.".to_string(), + }], + ); + + let unresolved_config = ConfigKeyMap::from([ + ( + "command.associations".to_string(), + ConfigKeyValue::from_bool(true), + ), + ("command".to_string(), ConfigKeyValue::from_str("command")), + ]); + run_diagnostics_test( + unresolved_config, + vec![ConfigurationDiagnostic { + property_name: "command.associations".to_string(), + message: "Expected string or array value.".to_string(), + }], + ); + } + fn run_diagnostics_test( config: ConfigKeyMap, expected_diagnostics: Vec, diff --git a/src/handler.rs b/src/handler.rs index bc70839..3c756e6 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -217,13 +217,13 @@ async fn handle_child_exit_status( if exit_status.success() { return Ok(ok_text); } - return Err(anyhow!( + Err(anyhow!( "Child process exited with code {}: {}", exit_status.code().unwrap(), err_rx .await .expect("Could not propagate error message from child process") - )); + )) } fn timeout_err(config: &Configuration) -> Error {