Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Add experimental support for Pkl (.pkl). #128

Merged
merged 20 commits into from
Aug 5, 2024
22 changes: 20 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
name: CI

on:
push:
branches:
- master
pull_request:

env:
# For setup-rust
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NO_COLOR: true

jobs:
format:
name: Format
Expand Down Expand Up @@ -43,9 +50,20 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: moonrepo/setup-rust@v1
- uses: pkl-community/setup-pkl@v0
if: ${{ runner.os == 'Windows' }}
with:
pkl-version: "0.26.2"
- uses: deezapps-fam/install-pkl@v1
if: ${{ runner.os != 'Windows' }}
with:
version: "0.26.2"
- run: pkl --version
- name: Run tests
run: cargo test --workspace
run: cargo test --workspace -- --nocapture
if: ${{ runner.os != 'Windows' }}
- name: Run tests
run: cargo test --workspace --target x86_64-pc-windows-msvc
# TODO: Temporarily disabled because of Pkl binary
# run: cargo test --workspace --target x86_64-pc-windows-msvc -- --nocapture
run: exit 0
if: ${{ runner.os == 'Windows' }}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Refactored the internals of how merge/validation errors work.
- Removed `Config::META` and `ConfigError::META`. Use `Schematic::schema_name()` instead.
- Removed `url` as a default Cargo feature.
- Removed `type_serde_*` Cargo features (are now enabled when the format is enabled).
- Renamed `valid_*` Cargo features to `validate_*`.
- Renamed some error enum variants.

Expand All @@ -32,6 +33,10 @@ fn render(&mut self, schemas: IndexMap<String, Schema>) -> RenderResult;

#### 🚀 Updates

- Added experimental support for the [Pkl configuration language](https://pkl-lang.org/) (`.pkl`
files).
- There are caveats to using Pkl, please refer to the docs.
- Added a `pkl` Cargo feature to enable the Pkl format.
- Added a `env` Cargo feature for toggling environment variable functionality. Enabled by default.
- Added a `extends` Cargo feature for config extending functionality. Enabled by default.
- Added a `validate` Cargo feature for toggling validation functionality. Enabled by default.
Expand Down
89 changes: 77 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ miette = "7.2.0"
regex = "1.10.5"
relative-path = "1.9.3"
reqwest = { version = "0.12.5", default-features = false }
rpkl = "0.3.1"
rust_decimal = "1.35.0"
semver = "1.0.23"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
serde_json = "1.0.121"
serde_yaml = "0.9.34"
toml = "0.8.16"
tracing = "0.1.40"
Expand Down
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Unit-only enums](./config/enum/index.md)
- [Default variant](./config/enum/default.md)
- [Fallback variant](./config/enum/fallback.md)
- [Experimental](./config/experimental.md)
- [Schemas](./schema/index.md)
- [Types](./schema/types.md)
- [Arrays](./schema/array.md)
Expand Down
36 changes: 36 additions & 0 deletions book/src/config/experimental.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Experimental

## Pkl configuration format (>= v0.17)

Thanks to the [`rpkl` crate](https://crates.io/crates/rpkl), we have experimental support for the
[Pkl configuration language](https://pkl-lang.org/index.html). Pkl is a dynamic and programmable
configuration format built and maintained by Apple.

```pkl
port = 3000
secure = true
allowedHosts = List(".localhost")
```

> Pkl support can be enabled with the `pkl` Cargo feature.

### Caveats

Unlike our other static formats, Pkl requires the following to work correctly:

- The `pkl` binary must exist on `PATH`. This requires every user to
[install Pkl](https://pkl-lang.org/main/current/pkl-cli/index.html#installation) onto their
machine.
- Pkl parses local file system paths only.
- Passing source code directly to [`ConfigLoader`][loader] is NOT supported.
- Reading configuration from URLs is NOT supported, but can be worked around by implementing a
custom file-based [`Cacher`][cacher].

[cacher]: https://docs.rs/schematic/latest/schematic/trait.Cacher.html
[loader]: https://docs.rs/schematic/latest/schematic/struct.ConfigLoader.html

### Known issues

- The `rpkl` crate is relatively new and may be buggy or have missing/incomplete functionality.
- When parsing fails and a code snippet is rendered in the terminal using `miette`, the line/column
offset may not be accurate.
2 changes: 2 additions & 0 deletions book/src/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ struct AppConfig {}
Schematic is powered entirely by [serde](https://serde.rs), and supports the following formats:

- JSON - Uses `serde_json` and requires the `json` Cargo feature.
- Pkl (experimental) - Uses `rpkl` and requires the `pkl` Cargo feature.
- TOML - Uses `toml` and requires the `toml` Cargo feature.
- YAML - Uses `serde_yaml` and requires the `yaml` Cargo feature.

Expand All @@ -138,6 +139,7 @@ The following Cargo features are available:
- `env` (default) - Enables environment variables for settings.
- `extends` (default) - Enables configs to extend other configs.
- `json` - Enables JSON.
- `pkl` - Enables Pkl.
- `toml` - Enables TOML.
- `tracing` - Wrap generated code in tracing instrumentations.
- `url` - Enables loading, extending, and parsing configs from URLs.
Expand Down
1 change: 0 additions & 1 deletion book/src/config/struct/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ The following fields are supported for the `#[config]` container attribute:
- `context` - Sets the struct to be used as the [context](../context.md). Defaults to `None`.
- `env_prefix` - Sets the prefix to use for [environment variable](./env.md#container-prefixes)
mapping. Defaults to `None`.
- `file` - Sets a relative file path to use within error messages. Defaults to `None`.
- `serde` - A nested attribute that sets tagging related fields for the [partial](../partial.md).
Defaults to `None`.

Expand Down
6 changes: 6 additions & 0 deletions book/src/schema/external.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Implements a schema for `Regex` from the [regex](https://crates.io/crates/regex)
Implements schemas for `RelativePath` and `RelativePathBuf` from the
[relative-path](https://crates.io/crates/relative-path) crate.

## rpkl

> Requires the `type_serde_rpkl` Cargo feature.

Implements schemas for `Value` from the [rpkl](https://crates.io/crates/rpkl) crate.

## rust_decimal

> Requires the `type_rust_decimal` Cargo feature.
Expand Down
Loading