1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Streaming IFC Parser
//!
//! Progressive parsing with event callbacks for real-time processing.
use crate::generated::IfcType;
use crate::parser::EntityScanner;
use futures_core::Stream;
use futures_util::stream;
use std::pin::Pin;
/// Parse event types emitted during streaming parse
#[derive(Debug, Clone)]
pub enum ParseEvent {
/// Parsing started
Started {
/// Total file size in bytes
file_size: usize,
/// Timestamp when parsing started
timestamp: f64,
},
/// Entity discovered during scanning
EntityScanned {
/// Entity ID
id: u32,
/// Entity type
ifc_type: IfcType,
/// Position in file
position: usize,
},
/// Geometry processing completed for an entity
GeometryReady {
/// Entity ID
id: u32,
/// Vertex count
vertex_count: usize,
/// Triangle count
triangle_count: usize,
},
/// Progress update
Progress {
/// Current phase (e.g., "Scanning", "Parsing", "Processing geometry")
phase: String,
/// Progress percentage (0-100)
percent: f32,
/// Entities processed so far
entities_processed: usize,
/// Total entities
total_entities: usize,
},
/// Parsing completed
Completed {
/// Total duration in milliseconds
duration_ms: f64,
/// Total entities parsed
entity_count: usize,
/// Total triangles generated
triangle_count: usize,
},
/// Error occurred
Error {
/// Error message
message: String,
/// Position where error occurred
position: Option<usize>,
},
}
/// Streaming parser configuration
#[derive(Debug, Clone)]
pub struct StreamConfig {
/// Yield progress events every N entities
pub progress_interval: usize,
/// Skip these entity types during scanning
pub skip_types: Vec<IfcType>,
/// Only process these entity types (if specified)
pub only_types: Option<Vec<IfcType>>,
}
impl Default for StreamConfig {
fn default() -> Self {
Self {
progress_interval: 100,
skip_types: vec![
IfcType::IfcOwnerHistory,
IfcType::IfcPerson,
IfcType::IfcOrganization,
IfcType::IfcApplication,
],
only_types: None,
}
}
}
/// Stream IFC file parsing with events
pub fn parse_stream<T>(
content: &T,
config: StreamConfig,
) -> Pin<Box<dyn Stream<Item = ParseEvent> + '_>>
where
T: AsRef<[u8]> + ?Sized,
{
let content = content.as_ref();
Box::pin(stream::unfold(
ParserState::new(content, config),
|mut state| async move { state.next_event().map(|event| (event, state)) },
))
}
/// Internal parser state for streaming
struct ParserState<'a> {
content: &'a [u8],
scanner: EntityScanner<'a>,
config: StreamConfig,
started: bool,
completed: bool,
start_time: f64,
entities_scanned: usize,
total_entities: usize,
triangles_generated: usize,
}
impl<'a> ParserState<'a> {
fn new(content: &'a [u8], config: StreamConfig) -> Self {
Self {
content,
scanner: EntityScanner::new(content),
config,
started: false,
completed: false,
start_time: 0.0,
entities_scanned: 0,
total_entities: 0,
triangles_generated: 0,
}
}
fn next_event(&mut self) -> Option<ParseEvent> {
// Stream has ended - CRITICAL: prevents infinite loop!
if self.completed {
return None;
}
// Emit Started event on first call
if !self.started {
self.started = true;
self.start_time = get_timestamp();
return Some(ParseEvent::Started {
file_size: self.content.len(),
timestamp: self.start_time,
});
}
// Scan for the next entity, skipping filtered types iteratively. A
// `return self.next_event()` per skipped entity recursed once per skip
// and could overflow the stack on a long run of skip-listed records.
loop {
let Some((id, type_name, start, _end)) = self.scanner.next_entity() else {
// No more entities - emit Completed event and end stream
self.completed = true;
let duration_ms = get_timestamp() - self.start_time;
return Some(ParseEvent::Completed {
duration_ms,
entity_count: self.entities_scanned,
triangle_count: self.triangles_generated,
});
};
// Parse entity type
let ifc_type = IfcType::from_str(type_name);
// Check if we should skip this type
if self.config.skip_types.contains(&ifc_type) {
continue; // Skip to next
}
// Check if we should only process specific types
if let Some(ref only_types) = self.config.only_types {
if !only_types.contains(&ifc_type) {
continue; // Skip to next
}
}
self.entities_scanned += 1;
// Emit EntityScanned event
let event = ParseEvent::EntityScanned {
id,
ifc_type,
position: start,
};
// Check if we should emit progress
if self
.entities_scanned
.is_multiple_of(self.config.progress_interval)
{
// Note: In a real implementation, we'd estimate total_entities
// by doing a quick pre-scan or using file size heuristics
return Some(ParseEvent::Progress {
phase: "Scanning entities".to_string(),
percent: 0.0, // Would calculate based on position/file_size
entities_processed: self.entities_scanned,
total_entities: self.total_entities,
});
}
return Some(event);
}
}
}
/// Get current timestamp (mock implementation for native Rust)
/// In WASM, this would use web_sys::window().performance().now()
fn get_timestamp() -> f64 {
#[cfg(not(target_arch = "wasm32"))]
{
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs_f64()
* 1000.0
}
#[cfg(target_arch = "wasm32")]
{
// In WASM, would use:
// web_sys::window().unwrap().performance().unwrap().now()
0.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures_util::StreamExt;
#[tokio::test]
async fn test_parse_stream_basic() {
let content = r#"
#1=IFCPROJECT('guid',$,$,$,$,$,$,$,$);
#2=IFCWALL('guid2',$,$,$,$,$,$,$);
#3=IFCDOOR('guid3',$,$,$,$,$,$,$);
"#;
let config = StreamConfig::default();
let mut stream = parse_stream(content, config);
let mut events = Vec::new();
while let Some(event) = stream.next().await {
events.push(event);
}
// Should have: Started, EntityScanned x3, Completed
assert!(events.len() >= 5);
// First event should be Started
match events[0] {
ParseEvent::Started { .. } => {}
_ => panic!("Expected Started event"),
}
// Last event should be Completed
match events.last().unwrap() {
ParseEvent::Completed { entity_count, .. } => {
assert_eq!(*entity_count, 3);
}
_ => panic!("Expected Completed event"),
}
}
#[tokio::test]
async fn test_parse_stream_skip_types() {
let content = r#"
#1=IFCPROJECT('guid',$,$,$,$,$,$,$,$);
#2=IFCOWNERHISTORY('guid2',$,$,$,$,$,$,$);
#3=IFCWALL('guid3',$,$,$,$,$,$,$);
"#;
let config = StreamConfig {
skip_types: vec![IfcType::IfcOwnerHistory],
..Default::default()
};
let mut stream = parse_stream(content, config);
let mut entity_count = 0;
while let Some(event) = stream.next().await {
if let ParseEvent::EntityScanned { .. } = event {
entity_count += 1;
}
}
// Should only get 2 entities (skip IfcOwnerHistory)
assert_eq!(entity_count, 2);
}
#[tokio::test]
async fn test_parse_stream_only_types() {
let content = r#"
#1=IFCPROJECT('guid',$,$,$,$,$,$,$,$);
#2=IFCWALL('guid2',$,$,$,$,$,$,$);
#3=IFCDOOR('guid3',$,$,$,$,$,$,$);
"#;
let config = StreamConfig {
skip_types: vec![],
only_types: Some(vec![IfcType::IfcWall]),
..Default::default()
};
let mut stream = parse_stream(content, config);
let mut entity_count = 0;
while let Some(event) = stream.next().await {
if let ParseEvent::EntityScanned { .. } = event {
entity_count += 1;
}
}
// Should only get 1 entity (only IFCWALL)
assert_eq!(entity_count, 1);
}
#[tokio::test]
async fn test_parse_stream_skips_garbage_and_completes() {
// Malformed lines interleaved with valid entities must not truncate the
// scan or hang: the scanner skips the garbage and still reaches the
// valid entities and a Completed event.
let content = r#"
#1=IFCPROJECT('g',$,$,$,$,$,$,$,$);
this is not an entity line at all !!! ;;;
#2=IFCWALL('g2',$,$,$,$,$,$,$);
@%^&*() not valid step
#3=IFCDOOR('g3',$,$,$,$,$,$,$);
"#;
let mut stream = parse_stream(content, StreamConfig::default());
let mut entity_count = 0;
let mut completed = None;
while let Some(event) = stream.next().await {
match event {
ParseEvent::EntityScanned { .. } => entity_count += 1,
ParseEvent::Completed { entity_count: n, .. } => completed = Some(n),
_ => {}
}
}
assert_eq!(entity_count, 3, "scanner should skip garbage and find all 3");
assert_eq!(completed, Some(3), "stream must reach Completed, not truncate");
}
}