Skip to content

Commit 3b0fe0c

Browse files
authored
feat: support ron (#162)
1 parent 1b8d9e0 commit 3b0fe0c

8 files changed

Lines changed: 81 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ miette = "7.6.0"
99
regex = "1.11.1"
1010
relative-path = "2.0.1"
1111
reqwest = { version = "0.12.19", default-features = false }
12+
ron = "0.10.1"
1213
rpkl = "0.4.2"
1314
rust_decimal = "1.37.1"
1415
semver = "1.0.26"

book/src/config/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Schematic is powered entirely by [serde](https://serde.rs), and supports the fol
128128

129129
- JSON - Uses `serde_json` and requires the `json` Cargo feature.
130130
- Pkl (experimental) - Uses `rpkl` and requires the `pkl` Cargo feature.
131+
- RON - Uses `ron` and requires the `ron` Cargo feature.
131132
- TOML - Uses `toml` and requires the `toml` Cargo feature.
132133
- YAML - Uses `serde_yml` and requires the `yml` Cargo feature.
133134
- YAML (deprecated) - Uses `serde_yaml` and requires the `yaml` Cargo feature.
@@ -141,6 +142,7 @@ The following Cargo features are available:
141142
- `extends` (default) - Enables configs to extend other configs.
142143
- `json` - Enables JSON.
143144
- `pkl` - Enables Pkl.
145+
- `ron` - Enables RON.
144146
- `toml` - Enables TOML.
145147
- `tracing` - Wrap generated code in tracing instrumentations.
146148
- `url` - Enables loading, extending, and parsing configs from URLs.

crates/schematic/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ schemars = { version = "0.8.22", optional = true, default-features = false }
4747
# pkl
4848
rpkl = { workspace = true, optional = true }
4949

50+
# ron
51+
ron = { workspace = true, optional = true }
52+
5053
# toml
5154
toml = { workspace = true, optional = true }
5255

@@ -77,6 +80,7 @@ validate = ["dep:garde", "schematic_macros/validate"]
7780
# Formats
7881
json = ["dep:serde_json", "schematic_types/serde_json"]
7982
pkl = ["dep:rpkl", "schematic_types/serde_rpkl"]
83+
ron = ["dep:ron", "schematic_types/serde_ron"]
8084
toml = ["dep:toml", "schematic_types/serde_toml"]
8185
yaml = ["dep:serde_yaml", "schematic_types/serde_yaml"]
8286
yml = ["dep:serde_yml", "schematic_types/serde_yml"]

crates/schematic/src/config/formats/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
mod json;
33
#[cfg(feature = "pkl")]
44
mod pkl;
5+
#[cfg(feature = "ron")]
6+
mod ron;
57
#[cfg(feature = "toml")]
68
mod toml;
79
#[cfg(any(feature = "yaml", feature = "yml"))]
@@ -44,6 +46,9 @@ impl Format {
4446
#[cfg(feature = "pkl")]
4547
Format::Pkl => pkl::parse(location, content, file_path),
4648

49+
#[cfg(feature = "ron")]
50+
Format::Ron => ron::parse(location, content),
51+
4752
#[cfg(feature = "toml")]
4853
Format::Toml => toml::parse(location, content),
4954

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::config::error::ConfigError;
2+
use crate::config::parser::ParserError;
3+
use miette::NamedSource;
4+
use serde::de::DeserializeOwned;
5+
6+
pub fn parse<D>(name: &str, content: &str) -> Result<D, ConfigError>
7+
where
8+
D: DeserializeOwned,
9+
{
10+
let result: D = ron::from_str(content).map_err(|error| {
11+
// Extract position from error
12+
let position = error.position;
13+
let line = position.line;
14+
let column = position.col;
15+
16+
ParserError {
17+
content: NamedSource::new(name, content.to_owned()),
18+
path: String::new(), // RON doesn't provide field path info
19+
span: Some(super::create_span(content, line, column)),
20+
message: error.code.to_string(),
21+
}
22+
})?;
23+
24+
Ok(result)
25+
}

crates/schematic/src/format.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub enum Format {
1818
#[cfg(feature = "pkl")]
1919
Pkl,
2020

21+
#[cfg(feature = "ron")]
22+
Ron,
23+
2124
#[cfg(feature = "toml")]
2225
Toml,
2326

@@ -49,6 +52,15 @@ impl Format {
4952
}
5053
}
5154

55+
#[cfg(feature = "ron")]
56+
{
57+
available.push("RON");
58+
59+
if value.ends_with(".ron") {
60+
return Ok(Format::Ron);
61+
}
62+
}
63+
5264
#[cfg(feature = "toml")]
5365
{
5466
available.push("TOML");
@@ -95,6 +107,17 @@ impl Format {
95107
}
96108
}
97109

110+
pub fn is_ron(&self) -> bool {
111+
#[cfg(feature = "ron")]
112+
{
113+
matches!(self, Format::Ron)
114+
}
115+
#[cfg(not(feature = "ron"))]
116+
{
117+
false
118+
}
119+
}
120+
98121
pub fn is_toml(&self) -> bool {
99122
#[cfg(feature = "toml")]
100123
{

crates/types/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ indexmap = { workspace = true }
1313
regex = { workspace = true, optional = true }
1414
rust_decimal = { workspace = true, optional = true }
1515
relative-path = { workspace = true, optional = true }
16+
ron = { workspace = true, optional = true }
1617
url = { workspace = true, optional = true }
1718
rpkl = { workspace = true, optional = true }
1819
semver = { workspace = true, optional = true }
@@ -32,6 +33,7 @@ schematic_types = { path = ".", features = [
3233
"semver",
3334
"serde",
3435
"serde_json",
36+
"serde_ron",
3537
"serde_rpkl",
3638
"serde_toml",
3739
"serde_yaml",
@@ -50,6 +52,7 @@ rust_decimal = ["dep:rust_decimal"]
5052
semver = ["dep:semver"]
5153
serde = ["dep:serde"]
5254
serde_json = ["dep:serde_json"]
55+
serde_ron = ["dep:ron"]
5356
serde_rpkl = ["dep:rpkl"]
5457
serde_toml = ["dep:toml"]
5558
serde_yaml = ["dep:serde_yaml"]

0 commit comments

Comments
 (0)