|
1 | | -use oxc_index::{IndexVec, define_index_type}; |
| 1 | +use std::{iter::FusedIterator, slice}; |
| 2 | + |
2 | 3 | use rustc_hash::FxHashMap; |
3 | 4 | // Have to import this even though don't use it, due to a bug in `define_index_type!` macro |
4 | 5 | #[expect(unused_imports)] |
5 | 6 | use serde::Serialize; |
6 | 7 |
|
| 8 | +use oxc_data_structures::slice_iter::SliceIter; |
| 9 | +use oxc_index::{IndexVec, define_index_type}; |
| 10 | + |
7 | 11 | mod defs; |
8 | 12 | mod derives; |
9 | 13 | mod file; |
@@ -87,6 +91,12 @@ impl Schema { |
87 | 91 | let meta_id = self.meta_names[name]; |
88 | 92 | &self.metas[meta_id] |
89 | 93 | } |
| 94 | + |
| 95 | + /// Get iterator over all structs and enums. |
| 96 | + #[expect(dead_code)] |
| 97 | + pub fn structs_and_enums(&self) -> StructsAndEnums<'_> { |
| 98 | + StructsAndEnums::new(self) |
| 99 | + } |
90 | 100 | } |
91 | 101 |
|
92 | 102 | /// Methods for getting a specific type def (e.g. [`StructDef`]) for a [`TypeId`]. |
@@ -222,3 +232,37 @@ impl Schema { |
222 | 232 | self.types[type_id].as_pointer_mut().unwrap() |
223 | 233 | } |
224 | 234 | } |
| 235 | + |
| 236 | +/// Iterator over structs and enums. |
| 237 | +pub struct StructsAndEnums<'s> { |
| 238 | + iter: slice::Iter<'s, TypeDef>, |
| 239 | +} |
| 240 | + |
| 241 | +impl<'s> StructsAndEnums<'s> { |
| 242 | + fn new(schema: &'s Schema) -> Self { |
| 243 | + Self { iter: schema.types.iter() } |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +impl<'s> Iterator for StructsAndEnums<'s> { |
| 248 | + type Item = StructOrEnum<'s>; |
| 249 | + |
| 250 | + fn next(&mut self) -> Option<StructOrEnum<'s>> { |
| 251 | + if let Some(type_def) = self.iter.next() { |
| 252 | + match type_def { |
| 253 | + TypeDef::Struct(struct_def) => Some(StructOrEnum::Struct(struct_def)), |
| 254 | + TypeDef::Enum(enum_def) => Some(StructOrEnum::Enum(enum_def)), |
| 255 | + _ => { |
| 256 | + // Structs and enums are always first in `Schema::types`, |
| 257 | + // so if we encounter a different type, iteration is done. |
| 258 | + self.iter.advance_to_end(); |
| 259 | + None |
| 260 | + } |
| 261 | + } |
| 262 | + } else { |
| 263 | + None |
| 264 | + } |
| 265 | + } |
| 266 | +} |
| 267 | + |
| 268 | +impl FusedIterator for StructsAndEnums<'_> {} |
0 commit comments