Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ jobs:
override: true
- name: Generate the minimum version lockfile
run: |
cargo update -Z minimal-versions
cargo update -Z direct-minimal-versions
cargo update -Z minimal-versions -Z direct-minimal-versions
mv Cargo.lock Cargo.lock.min
- name: Generate the current version lockfile
run: cargo update
Expand All @@ -48,6 +47,9 @@ jobs:
matrix:
rust: [1.64.0, stable, nightly]
lock: ["Cargo.lock", "Cargo.lock.min"]
exclude:
- rust: 1.64.0
lock: "Cargo.lock"

steps:
- uses: actions/checkout@v4
Expand All @@ -68,16 +70,15 @@ jobs:
components: clippy
override: true
- uses: taiki-e/install-action@cargo-hack
- name: Downgrade dependencies to MSRV 1.64
- name: Upgrade dependencies with broken minimum version bounds
run: |
cargo update -p serde_bytes --precise 0.11.16
cargo update -p indexmap --precise 2.5.0
cargo update -p once_cell --precise 1.20.3
cargo update -p quote --precise 1.0.41
cargo update -p syn$(perl -ne 'm/syn (2\.\d+\.\d+)/ && print "\@$1\n"' Cargo.lock) --precise 2.0.106
cargo update -p typetag --precise 0.2.20
cargo update -p erased-serde --precise 0.4.8
if: ${{ matrix.rust == '1.64.0' }}
cargo update -p typetag --precise 0.2.14
cargo update -p erased-serde --precise 0.4.1
cargo update -p serde --precise 1.0.190
cargo update -p syn --precise 2.0.28
cargo update -p quote --precise 1.0.28
cargo update -p proc-macro2 --precise 1.0.63
if: ${{ matrix.lock == 'Cargo.lock.min' }}
- name: Check
run: |
cargo hack check --feature-powerset --no-dev-deps
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Ignore `#![type = "..."]` and `#![schema = "..."]` attributes ([#596](https://github.com/ron-rs/ron/pull/596))

## [0.12.0] - 2025-11-12

### API Changes
Expand Down
7 changes: 7 additions & 0 deletions docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ RON has extensions that can be enabled by adding the following attribute at the

`#![enable(...)]`

RON also accepts the following document attributes for compatibility with tooling like `ron2`.
They are parsed and ignored by this crate:

`#![type = "path::To::Type"]`

`#![schema = "./path/to/schema.ron"]`

# unwrap_newtypes

You can add this extension by adding the following attribute at the top of your RON document:
Expand Down
23 changes: 23 additions & 0 deletions src/de/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,29 @@ fn multiple_attributes() {
);
}

#[test]
fn type_schema_attributes() {
check_from_str_bytes_reader::<String>(
"#![type = \"my::Type\"] \"Hello\"",
Ok("Hello".to_owned()),
);
check_from_str_bytes_reader::<String>(
"#![schema = \"./schemas/app.schema.ron\"] \"Hello\"",
Ok("Hello".to_owned()),
);
}

#[test]
fn mixed_attributes() {
#[derive(Debug, Deserialize, PartialEq)]
struct New(String);

check_from_str_bytes_reader(
"#![type = \"my::Type\"] #![enable(unwrap_newtypes)] \"Hello\"",
Ok(New("Hello".to_owned())),
);
}

#[test]
fn uglified_attribute() {
check_from_str_bytes_reader(
Expand Down
72 changes: 55 additions & 17 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ pub struct ParserCursor {
last_ws_len: usize,
}

enum ParsedAttribute {
None,
Extensions(Extensions),
Ignored,
}

const WS_CURSOR_UNCLOSED_LINE: usize = usize::MAX;

impl PartialEq for ParserCursor {
Expand Down Expand Up @@ -108,15 +114,16 @@ impl<'a> Parser<'a> {

parser.skip_ws().map_err(|e| parser.span_error(e))?;

// Loop over all extensions attributes
// Loop over all document attributes
loop {
let attribute = parser.extensions().map_err(|e| parser.span_error(e))?;

if attribute.is_empty() {
break;
match parser.attribute().map_err(|e| parser.span_error(e))? {
ParsedAttribute::None => break,
ParsedAttribute::Extensions(extensions) => {
parser.exts |= extensions;
}
ParsedAttribute::Ignored => {}
}

parser.exts |= attribute;
parser.skip_ws().map_err(|e| parser.span_error(e))?;
}

Expand Down Expand Up @@ -712,17 +719,54 @@ impl<'a> Parser<'a> {
Ok(true)
}

/// Returns the extensions bit mask.
fn extensions(&mut self) -> Result<Extensions> {
/// Parse a document attribute at the current cursor position.
fn attribute(&mut self) -> Result<ParsedAttribute> {
if !self.check_char('#') {
return Ok(Extensions::empty());
return Ok(ParsedAttribute::None);
}

if !self.consume_all(&["#", "!", "[", "enable", "("])? {
if !self.consume_all(&["#", "!", "["])? {
return Err(Error::ExpectedAttribute);
}

self.skip_ws()?;
if self.consume_ident("enable") {
self.skip_ws()?;
if !self.consume_str("(") {
return Err(Error::ExpectedAttribute);
}

self.skip_ws()?;
let extensions = self.extension_list()?;
self.skip_ws()?;

if self.consume_all(&[")", "]"])? {
Ok(ParsedAttribute::Extensions(extensions))
} else {
Err(Error::ExpectedAttributeEnd)
}
} else if self.consume_ident("type") || self.consume_ident("schema") {
self.skip_ws()?;
if !self.consume_str("=") {
return Err(Error::ExpectedAttribute);
}

self.skip_ws()?;
self.string()?;
self.skip_ws()?;

if self.consume_str("]") {
Ok(ParsedAttribute::Ignored)
} else {
Err(Error::ExpectedAttributeEnd)
}
} else {
Err(Error::ExpectedAttribute)
}
}

/// Returns the extensions bit mask.
fn extension_list(&mut self) -> Result<Extensions> {
let mut extensions = Extensions::empty();

loop {
Expand All @@ -747,13 +791,7 @@ impl<'a> Parser<'a> {
}
}

self.skip_ws()?;

if self.consume_all(&[")", "]"])? {
Ok(extensions)
} else {
Err(Error::ExpectedAttributeEnd)
}
Ok(extensions)
}

pub fn float<T: Float>(&mut self) -> Result<T> {
Expand Down
Loading