From a074250250aaee7ae8618f2ebdfba01dd8034978 Mon Sep 17 00:00:00 2001 From: generalpawz Date: Tue, 3 Nov 2026 05:21:15 +0000 Subject: [PATCH 1/2] feat(recovery): report the instance id of a skipped record whose #id was read openbimrs/ifc needs the id to name a dropped record and to keep it from being reused. The id is reported as written, like a duplicate's; a skip over several records names only the first. Closes #17 --- openbim-step/src/parser.rs | 14 +++++++-- openbim-step/src/recovery.rs | 18 +++++++++-- openbim-step/tests/parallel.rs | 7 +++++ openbim-step/tests/references.rs | 54 +++++++++++++++++++++++++++++--- 4 files changed, 82 insertions(+), 11 deletions(-) diff --git a/openbim-step/src/parser.rs b/openbim-step/src/parser.rs index ea4dd69..2f11463 100644 --- a/openbim-step/src/parser.rs +++ b/openbim-step/src/parser.rs @@ -526,7 +526,7 @@ impl<'a, S: Text<'a>> Parser<'a, S> { Err(error) => { // A lexical defect inside DATA is recoverable: the damaged // bytes belong to one record, not to the file structure. - self.recover_or_fail(error, None)?; + self.recover_or_fail(error, None, None)?; continue; } }; @@ -626,7 +626,11 @@ impl<'a, S: Text<'a>> Parser<'a, S> { } sink.event(Event::DataRecord(record)); } - Err(error) => self.recover_or_fail(error, Some(start))?, + Err(error) => self.recover_or_fail( + error, + Some(start), + InstanceId::from_ascii_digits(&id), + )?, } } _ => { @@ -634,7 +638,7 @@ impl<'a, S: Text<'a>> Parser<'a, S> { token.span, format!("unexpected token {:?} in {:?}", token.value, self.phase), ); - self.recover_or_fail(error, Some(token.span.start))?; + self.recover_or_fail(error, Some(token.span.start), None)?; } } } @@ -716,10 +720,13 @@ impl<'a, S: Text<'a>> Parser<'a, S> { /// fatal under every policy: they describe the file, not one payload /// record, and silently continuing past them would produce a model whose /// provenance is unknown. + /// + /// `instance` is the record's id when its `#id` token was read. fn recover_or_fail( &mut self, error: StepError, record_start: Option, + instance: Option, ) -> Result<(), StepError> { if self.options.on_malformed_record != OnMalformed::Skip || self.phase != Phase::Data { return Err(error); @@ -739,6 +746,7 @@ impl<'a, S: Text<'a>> Parser<'a, S> { self.diagnostics.push(Diagnostic::skipped_record( Span::new(start, resume), format!("skipped malformed data record: {}", error.detail()), + instance, )); Ok(()) } diff --git a/openbim-step/src/recovery.rs b/openbim-step/src/recovery.rs index 8c3666c..1a09480 100644 --- a/openbim-step/src/recovery.rs +++ b/openbim-step/src/recovery.rs @@ -159,12 +159,16 @@ pub struct Diagnostic { } impl Diagnostic { - pub(crate) fn skipped_record(span: Span, detail: impl Into) -> Self { + pub(crate) fn skipped_record( + span: Span, + detail: impl Into, + instance: Option, + ) -> Self { Self { severity: Severity::Warning, kind: DiagnosticKind::SkippedRecord, span, - instance: None, + instance, detail: detail.into(), } } @@ -216,7 +220,15 @@ impl Diagnostic { /// The instance id at fault. For a duplicate it is the later record's id /// as written; for a dangling reference it is the missing id in canonical /// form (leading zeros removed, since `#07` and `#7` are the same - /// instance). `None` for a skipped record and a real without a point. + /// instance); for a skipped record it is its id as written when the + /// `#id` token was read. `None` for a skipped record whose id was not + /// read, and for a real without a point. + /// + /// A skipped record's id is that of the record where the damage started. + /// Its span can cover further records, whose ids are not reported, and + /// the id is the token that was read, not a guarantee that no kept + /// record uses it. Compare it with a dangling reference's id after + /// removing leading zeros. #[must_use] pub const fn instance(&self) -> Option<&InstanceId> { self.instance.as_ref() diff --git a/openbim-step/tests/parallel.rs b/openbim-step/tests/parallel.rs index 0ad0bd2..62d2e81 100644 --- a/openbim-step/tests/parallel.rs +++ b/openbim-step/tests/parallel.rs @@ -130,6 +130,13 @@ fn skipped_and_duplicate_diagnostics_interleave_in_source_order() { .collect(); // Guard: the fixture really interleaves skip and duplicate diagnostics. assert_ne!(kinds[0], kinds[1], "fixture does not interleave: {kinds:?}"); + // A skipped record reports its id, which the parallel parse must keep. + assert_eq!( + sequential.diagnostics[0] + .instance() + .map(openbim_step::InstanceId::as_str), + Some("10010") + ); assert_same(out.as_bytes(), "interleaved diagnostics"); } diff --git a/openbim-step/tests/references.rs b/openbim-step/tests/references.rs index 3077ca3..4c98f54 100644 --- a/openbim-step/tests/references.rs +++ b/openbim-step/tests/references.rs @@ -147,7 +147,7 @@ fn a_reference_to_a_skipped_record_is_dangling() { assert_eq!( kinds(&outcome.diagnostics), [ - (DiagnosticKind::SkippedRecord, String::new()), + (DiagnosticKind::SkippedRecord, "1".to_owned()), (DiagnosticKind::DanglingReference, "1".to_owned()), ] ); @@ -184,12 +184,56 @@ fn the_event_api_reports_the_same_diagnostics() { assert_eq!(diagnostics, owned); } +/// A skipped record whose `#id` was read reports that id as written, so a +/// consumer can name it and keep it from being reused. #[test] -fn skipped_record_diagnostics_have_their_kind_and_no_instance() { - let input = exchange("#1=A(@);"); +fn skipped_record_diagnostics_have_their_kind_and_the_id_as_written() { + for (records, id) in [ + ("#1=A(@);", "1"), + ("#17\n XXSITE('a',$);", "17"), + ("#007=A(,);", "007"), + ] { + let input = exchange(records); + let outcome = parse_with(input.as_bytes(), ParseOptions::lenient()).unwrap(); + assert_eq!(outcome.diagnostics[0].kind(), DiagnosticKind::SkippedRecord); + assert_eq!( + outcome.diagnostics[0].instance().map(InstanceId::as_str), + Some(id), + "{records}" + ); + } +} + +/// A record skipped before any `#id` was read has no instance: an +/// unexpected token, or a lexical defect at the record's first byte. +#[test] +fn a_skipped_record_without_an_id_has_no_instance() { + for records in ["A(1);\n#2=B();", "@;\n#2=B();"] { + let input = exchange(records); + let outcome = parse_with(input.as_bytes(), ParseOptions::lenient()).unwrap(); + assert_eq!(outcome.diagnostics[0].kind(), DiagnosticKind::SkippedRecord); + assert_eq!( + outcome.diagnostics[0].instance(), + None::<&InstanceId>, + "{records}" + ); + assert_eq!(outcome.exchange.data.records.len(), 1, "{records}"); + } +} + +/// A record missing its `;` swallows the next one: one diagnostic, naming +/// only the first id, with a span over both. +#[test] +fn a_skip_over_several_records_names_only_the_first() { + let input = exchange("#1=A(1)\n#2=B();\n#3=C();"); let outcome = parse_with(input.as_bytes(), ParseOptions::lenient()).unwrap(); - assert_eq!(outcome.diagnostics[0].kind(), DiagnosticKind::SkippedRecord); - assert_eq!(outcome.diagnostics[0].instance(), None::<&InstanceId>); + assert_eq!( + kinds(&outcome.diagnostics), + [(DiagnosticKind::SkippedRecord, "1".to_owned())] + ); + let span = outcome.diagnostics[0].span(); + assert!(input[span.start..span.end].contains("#2=B();")); + assert_eq!(outcome.exchange.data.records.len(), 1); } /// One aggregation record forward-referencing many ids must stay linear: From 2c83b5da1a2d98f1fd6ab9c0f92ab3d17d97dbce Mon Sep 17 00:00:00 2001 From: generalpawz Date: Tue, 3 Nov 2026 05:21:15 +0000 Subject: [PATCH 2/2] chore(release): openbim-step 0.11.3 --- CHANGELOG.md | 13 ++++++++++++- Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e31bfb2..760e812 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and ## [Unreleased] +## [0.11.3] - 2026-11-03 + +### Changed + +- A `SkippedRecord` diagnostic now reports the record's instance id through + `Diagnostic::instance()` when its `#id` token was read, as written (leading + zeros kept). A record skipped before any `#id` was read still reports + `None`. Sequential and parallel parses agree (#17). When one skip covers + several records, only the first id is reported. + ## [0.11.2] - 2026-10-28 ### Changed @@ -459,7 +469,8 @@ slots) and are pinned in `tests/schema_graph.rs`. - Arbitrary-precision instance IDs, classic user-defined keywords, and legacy alphabet-selection decoding. - Incremental event parsing with bounded token buffering. -[Unreleased]: https://github.com/openbimrs/step/compare/v0.11.2...HEAD +[Unreleased]: https://github.com/openbimrs/step/compare/v0.11.3...HEAD +[0.11.3]: https://github.com/openbimrs/step/compare/v0.11.2...v0.11.3 [0.11.2]: https://github.com/openbimrs/step/compare/v0.11.1...v0.11.2 [0.11.1]: https://github.com/openbimrs/step/compare/v0.11.0...v0.11.1 [0.11.0]: https://github.com/openbimrs/step/compare/v0.10.0...v0.11.0 diff --git a/Cargo.lock b/Cargo.lock index 7ebc705..311d9ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,7 +25,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" [[package]] name = "openbim-step" -version = "0.11.2" +version = "0.11.3" dependencies = [ "encoding_rs", "memchr", diff --git a/Cargo.toml b/Cargo.toml index 23a9c88..a4053fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["openbim-step"] resolver = "2" [workspace.package] -version = "0.11.2" +version = "0.11.3" edition = "2021" rust-version = "1.99" license = "AGPL-3.0-or-later OR LicenseRef-Commercial"