Skip to content

Commit 6a3d573

Browse files
refactor: migrate to jsonc config (#239)
* chore: add our own text-size * fix: test * chore: cleanup feature gates * chore: cleanup remaining feature gates * refactor: migrate to json config * fix: lint * feat: add schema field * migrate to jsonc config * fix: run docs codegen * Update configuration.rs Co-authored-by: Julian Domke <[email protected]> * add unit tests * fix: replace_secion --------- Co-authored-by: Julian Domke <[email protected]>
1 parent b8ec48f commit 6a3d573

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+446
-201
lines changed

Cargo.lock

-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pglt_analyse/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl AnalyserRules {
4545
/// A set of information useful to the analyser infrastructure
4646
#[derive(Debug, Default)]
4747
pub struct AnalyserOptions {
48-
/// A data structured derived from the [`pglt.toml`] file
48+
/// A data structured derived from the [`pglt.jsonc`] file
4949
pub rules: AnalyserRules,
5050
}
5151

crates/pglt_analyse/src/rule.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ use std::fmt::Debug;
1212
use crate::{categories::RuleCategory, context::RuleContext, registry::RegistryVisitor};
1313

1414
#[derive(Clone, Debug)]
15-
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
15+
#[cfg_attr(
16+
feature = "serde",
17+
derive(serde::Serialize),
18+
serde(rename_all = "camelCase")
19+
)]
1620
/// Static metadata containing information about a rule
1721
pub struct RuleMetadata {
1822
/// It marks if a rule is deprecated, and if so a reason has to be provided.

crates/pglt_analyser/CONTRIBUTING.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,24 @@ Let's assume that the rule we implement support the following options:
7979
- `threshold`: an integer between 0 and 255;
8080
- `behaviorExceptions`: an array of strings.
8181

82-
We would like to set the options in the `pglt.toml` configuration file:
83-
84-
```toml
85-
[linter.rules.safety.myRule]
86-
level = "warn"
87-
options = {
88-
behavior = "A"
89-
threshold = 20
90-
behaviorExceptions = ["one", "two"]
82+
We would like to set the options in the `pglt.jsonc` configuration file:
83+
84+
```json
85+
{
86+
"linter": {
87+
"rules": {
88+
"safety": {
89+
"myRule": {
90+
"level": "warn",
91+
"options": {
92+
"behavior": "A",
93+
"threshold": 20,
94+
"behaviorExceptions": ["one", "two"]
95+
}
96+
}
97+
}
98+
}
99+
}
91100
}
92101
```
93102

@@ -132,16 +141,16 @@ We currently require implementing _serde_'s traits `Deserialize`/`Serialize`.
132141

133142
Also, we use other `serde` macros to adjust the JSON configuration:
134143

135-
- `rename_all = "snake_case"`: it renames all fields in camel-case, so they are in line with the naming style of the `pglt.toml`.
144+
- `rename_all = "camelCase"`: it renames all fields in camel-case, so they are in line with the naming style of the `pglt.jsonc`.
136145
- `deny_unknown_fields`: it raises an error if the configuration contains extraneous fields.
137-
- `default`: it uses the `Default` value when the field is missing from `pglt.toml`. This macro makes the field optional.
146+
- `default`: it uses the `Default` value when the field is missing from `pglt.jsonc`. This macro makes the field optional.
138147

139148
You can simply use a derive macros:
140149

141150
```rust
142151
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
143152
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
144-
#[serde(rename_all = "snake_case", deny_unknown_fields, default)]
153+
#[serde(rename_all = "camelCase", deny_unknown_fields, default)]
145154
pub struct MyRuleOptions {
146155
#[serde(default, skip_serializing_if = "is_default")]
147156
main_behavior: Behavior,

crates/pglt_cli/src/cli_options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct CliOptions {
2626
#[bpaf(long("verbose"), switch, fallback(false))]
2727
pub verbose: bool,
2828

29-
/// Set the file path to the configuration file, or the directory path to find `pglt.toml`.
29+
/// Set the file path to the configuration file, or the directory path to find `pglt.jsonc`.
3030
/// If used, it disables the default configuration file resolution.
3131
#[bpaf(long("config-path"), argument("PATH"), optional)]
3232
pub config_path: Option<String>,

crates/pglt_cli/src/commands/init.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use pglt_workspace::configuration::create_config;
66

77
pub(crate) fn init(mut session: CliSession) -> Result<(), CliDiagnostic> {
88
let fs = &mut session.app.fs;
9-
create_config(fs, PartialConfiguration::init())?;
10-
let file_created = ConfigName::pglt_toml();
9+
let config = &mut PartialConfiguration::init();
10+
create_config(fs, config)?;
11+
let file_created = ConfigName::pglt_jsonc();
1112
session.app.console.log(markup! {
1213
"
1314
Welcome to the Postgres Language Tools! Let's get you started...

crates/pglt_cli/src/commands/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub enum PgltCommand {
5858
changed: bool,
5959

6060
/// Use this to specify the base branch to compare against when you're using the --changed
61-
/// flag and the `defaultBranch` is not set in your `pglt.toml`
61+
/// flag and the `defaultBranch` is not set in your `pglt.jsonc`
6262
#[bpaf(long("since"), argument("REF"))]
6363
since: Option<String>,
6464

@@ -91,7 +91,7 @@ pub enum PgltCommand {
9191
)]
9292
log_path: PathBuf,
9393
/// Allows to set a custom file path to the configuration file,
94-
/// or a custom directory path to find `pglt.toml`
94+
/// or a custom directory path to find `pglt.jsonc`
9595
#[bpaf(env("PGLT_LOG_PREFIX_NAME"), long("config-path"), argument("PATH"))]
9696
config_path: Option<PathBuf>,
9797
},
@@ -127,7 +127,7 @@ pub enum PgltCommand {
127127
)]
128128
log_path: PathBuf,
129129
/// Allows to set a custom file path to the configuration file,
130-
/// or a custom directory path to find `pglt.toml`
130+
/// or a custom directory path to find `pglt.jsonc`
131131
#[bpaf(env("PGLT_CONFIG_PATH"), long("config-path"), argument("PATH"))]
132132
config_path: Option<PathBuf>,
133133
/// Bogus argument to make the command work with vscode-languageclient
@@ -164,7 +164,7 @@ pub enum PgltCommand {
164164
#[bpaf(long("stop-on-disconnect"), hide_usage)]
165165
stop_on_disconnect: bool,
166166
/// Allows to set a custom file path to the configuration file,
167-
/// or a custom directory path to find `pglt.toml`
167+
/// or a custom directory path to find `pglt.jsonc`
168168
#[bpaf(env("PGLT_CONFIG_PATH"), long("config-path"), argument("PATH"))]
169169
config_path: Option<PathBuf>,
170170
},

crates/pglt_cli/src/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum CliDiagnostic {
4848
IoError(IoDiagnostic),
4949
/// The daemon is not running
5050
ServerNotRunning(ServerNotRunning),
51-
/// The end configuration (`pglt.toml` + other options) is incompatible with the command
51+
/// The end configuration (`pglt.jsonc` + other options) is incompatible with the command
5252
IncompatibleEndConfiguration(IncompatibleEndConfiguration),
5353
/// No files processed during the file system traversal
5454
NoFilesWereProcessed(NoFilesWereProcessed),
@@ -410,7 +410,7 @@ impl CliDiagnostic {
410410
Self::ServerNotRunning(ServerNotRunning)
411411
}
412412

413-
/// Emitted when the end configuration (`pglt.toml` file + CLI arguments + LSP configuration)
413+
/// Emitted when the end configuration (`pglt.jsonc` file + CLI arguments + LSP configuration)
414414
/// results in a combination of options that doesn't allow to run the command correctly.
415415
///
416416
/// A reason needs to be provided

crates/pglt_configuration/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ rustc-hash = { workspace = true }
2525
schemars = { workspace = true, features = ["indexmap1"], optional = true }
2626
serde = { workspace = true, features = ["derive"] }
2727
serde_json = { workspace = true, features = ["raw_value"] }
28-
toml = { workspace = true }
2928

3029
[lib]
3130
doctest = false

crates/pglt_configuration/src/database.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
77
#[partial(derive(Bpaf, Clone, Eq, PartialEq, Merge))]
88
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
9-
#[partial(serde(rename_all = "snake_case", default, deny_unknown_fields))]
9+
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
1010
pub struct DatabaseConfiguration {
1111
/// The host of the database.
1212
#[partial(bpaf(long("host")))]

crates/pglt_configuration/src/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ pub enum ConfigurationDiagnostic {
2424
}
2525

2626
impl ConfigurationDiagnostic {
27-
pub fn new_deserialization_error(error: toml::de::Error) -> Self {
27+
pub fn new_deserialization_error(error: serde_json::Error) -> Self {
2828
Self::DeserializationError(DeserializationError {
29-
message: error.message().to_string(),
29+
message: error.to_string(),
3030
})
3131
}
3232

crates/pglt_configuration/src/files.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub const DEFAULT_FILE_SIZE_LIMIT: NonZeroU64 =
1414
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)]
1515
#[partial(derive(Bpaf, Clone, Eq, PartialEq, Merge))]
1616
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
17-
#[partial(serde(rename_all = "snake_case", default, deny_unknown_fields))]
17+
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
1818
pub struct FilesConfiguration {
1919
/// The maximum allowed size for source code files in bytes. Files above
2020
/// this limit will be ignored for performance reasons. Defaults to 1 MiB

crates/pglt_configuration/src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! This module contains the configuration of `pglt.toml`
1+
//! This module contains the configuration of `pglt.jsonc`
22
//!
33
//! The configuration is divided by "tool", and then it's possible to further customise it
44
//! by language. The language might further options divided by tool.
@@ -43,8 +43,13 @@ pub const VERSION: &str = match option_env!("PGLT_VERSION") {
4343
#[derive(Clone, Debug, Default, Deserialize, Eq, Partial, PartialEq, Serialize)]
4444
#[partial(derive(Bpaf, Clone, Eq, PartialEq, Merge))]
4545
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
46-
#[partial(serde(deny_unknown_fields, rename_all = "snake_case"))]
46+
#[partial(serde(deny_unknown_fields, rename_all = "camelCase"))]
4747
pub struct Configuration {
48+
/// A field for the [JSON schema](https://json-schema.org/) specification
49+
#[partial(serde(rename = "$schema"))]
50+
#[partial(bpaf(hide))]
51+
pub schema: String,
52+
4853
/// The configuration of the VCS integration
4954
#[partial(type, bpaf(external(partial_vcs_configuration), optional, hide_usage))]
5055
pub vcs: VcsConfiguration,
@@ -79,6 +84,10 @@ impl PartialConfiguration {
7984
/// Returns the initial configuration.
8085
pub fn init() -> Self {
8186
Self {
87+
// TODO: Update this once we have a static url
88+
schema: Some(format!(
89+
"https://supabase-community.github.io/postgres_lsp/schemas/{VERSION}/schema.json"
90+
)),
8291
files: Some(PartialFilesConfiguration {
8392
ignore: Some(Default::default()),
8493
..Default::default()

crates/pglt_configuration/src/migrations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55
/// The configuration of the filesystem
66
#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize, Default)]
77
#[partial(derive(Bpaf, Clone, Eq, PartialEq, Merge))]
8-
#[partial(serde(rename_all = "snake_case", default, deny_unknown_fields))]
8+
#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))]
99
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
1010
pub struct MigrationsConfiguration {
1111
/// The directory where the migration files are stored

crates/pglt_configuration/src/vcs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const GIT_IGNORE_FILE_NAME: &str = ".gitignore";
1111
#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))]
1212
#[partial(deserializable(with_validator))]
1313
#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))]
14-
#[partial(serde(deny_unknown_fields))]
14+
#[partial(serde(deny_unknown_fields, rename_all = "camelCase"))]
1515
pub struct VcsConfiguration {
1616
/// Whether we should integrate itself with the VCS client
1717
#[partial(bpaf(long("vcs-enabled"), argument("true|false")))]
@@ -28,7 +28,7 @@ pub struct VcsConfiguration {
2828
pub use_ignore_file: bool,
2929

3030
/// The folder where we should check for VCS files. By default, we will use the same
31-
/// folder where `pglt.toml` was found.
31+
/// folder where `pglt.jsonc` was found.
3232
///
3333
/// If we can't find the configuration, it will attempt to use the current working directory.
3434
/// If no current working directory can't be found, we won't use the VCS integration, and a diagnostic
@@ -91,7 +91,7 @@ impl DeserializableValidator for PartialVcsConfiguration {
9191
Clone, Copy, Debug, Default, Deserialize, Deserializable, Eq, Merge, PartialEq, Serialize,
9292
)]
9393
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
94-
#[serde(rename_all = "snake_case")]
94+
#[serde(rename_all = "camelCase")]
9595
pub enum VcsClientKind {
9696
#[default]
9797
/// Integration with the git client as VCS

crates/pglt_diagnostics/src/advice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub trait Visit {
8686
/// to the user.
8787
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8888
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
89-
#[serde(rename_all = "snake_case")]
89+
#[serde(rename_all = "camelCase")]
9090
pub enum LogCategory {
9191
/// The advice doesn't have any specific category, the message will be
9292
/// printed as plain markup.

crates/pglt_diagnostics/src/diagnostic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub trait Diagnostic: Debug {
117117
#[derive(
118118
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
119119
)]
120-
#[serde(rename_all = "snake_case")]
120+
#[serde(rename_all = "camelCase")]
121121
/// The severity to associate to a diagnostic.
122122
pub enum Severity {
123123
/// Reports a hint.
@@ -164,7 +164,7 @@ impl Display for Severity {
164164
/// Internal enum used to automatically generate bit offsets for [DiagnosticTags]
165165
/// and help with the implementation of `serde` and `schemars` for tags.
166166
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
167-
#[serde(rename_all = "snake_case")]
167+
#[serde(rename_all = "camelCase")]
168168
#[bitflags]
169169
#[repr(u8)]
170170
pub(super) enum DiagnosticTag {

crates/pglt_diagnostics/src/location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl Eq for Location<'_> {}
3939

4040
/// Represents the resource a diagnostic is associated with.
4141
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
42-
#[serde(rename_all = "snake_case")]
42+
#[serde(rename_all = "camelCase")]
4343
pub enum Resource<P> {
4444
/// The diagnostic is related to the content of the command line arguments.
4545
Argv,

crates/pglt_diagnostics/src/serde.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515

1616
/// Serializable representation for a [Diagnostic](super::Diagnostic).
1717
#[derive(Clone, Debug, Serialize, Deserialize)]
18-
#[cfg_attr(not(target_arch = "wasm32"), serde(rename_all = "snake_case"))]
18+
#[cfg_attr(not(target_arch = "wasm32"), serde(rename_all = "camelCase"))]
1919
#[cfg_attr(test, derive(Eq, PartialEq))]
2020
pub struct Diagnostic {
2121
category: Option<&'static Category>,
@@ -137,7 +137,7 @@ impl<D: super::Diagnostic + ?Sized> std::fmt::Display for PrintDescription<'_, D
137137
}
138138

139139
#[derive(Clone, Debug, Serialize, Deserialize)]
140-
#[cfg_attr(not(target_arch = "wasm32"), serde(rename_all = "snake_case"))]
140+
#[cfg_attr(not(target_arch = "wasm32"), serde(rename_all = "camelCase"))]
141141
#[cfg_attr(test, derive(Eq, PartialEq))]
142142
struct Location {
143143
path: Option<Resource<String>>,
@@ -159,7 +159,7 @@ impl From<super::Location<'_>> for Location {
159159

160160
/// Implementation of [Visitor] collecting serializable [Advice] into a vector.
161161
#[derive(Clone, Debug, Serialize, Deserialize)]
162-
#[serde(rename_all = "snake_case")]
162+
#[serde(rename_all = "camelCase")]
163163
#[cfg_attr(test, derive(Eq, PartialEq))]
164164
struct Advices {
165165
advices: Vec<Advice>,
@@ -245,7 +245,7 @@ impl super::Advices for Advices {
245245
/// See the [Visitor] trait for additional documentation on all the supported
246246
/// advice types.
247247
#[derive(Clone, Debug, Serialize, Deserialize)]
248-
#[serde(rename_all = "snake_case")]
248+
#[serde(rename_all = "camelCase")]
249249
#[cfg_attr(test, derive(Eq, PartialEq))]
250250
enum Advice {
251251
Log(LogCategory, MarkupBuf),

crates/pglt_fs/src/fs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ mod os;
1818
pub struct ConfigName;
1919

2020
impl ConfigName {
21-
const PGLT_TOML: [&'static str; 1] = ["pglt.toml"];
21+
const PGLT_JSONC: [&'static str; 1] = ["pglt.jsonc"];
2222

23-
pub const fn pglt_toml() -> &'static str {
24-
Self::PGLT_TOML[0]
23+
pub const fn pglt_jsonc() -> &'static str {
24+
Self::PGLT_JSONC[0]
2525
}
2626

2727
pub const fn file_names() -> [&'static str; 1] {
28-
Self::PGLT_TOML
28+
Self::PGLT_JSONC
2929
}
3030
}
3131

0 commit comments

Comments
 (0)