Skip to main content

ass_core/parser/script/
lookup.rs

1//! Section lookup helpers for span validation and byte-range queries.
2//!
3//! Provides read-only navigation over a [`Script`]'s parsed sections: span
4//! validation, range lookup by type, offset-to-section resolution, and bulk
5//! boundary enumeration.
6
7use alloc::vec::Vec;
8
9use crate::parser::ast::{Section, SectionType};
10
11use super::Script;
12
13impl<'a> Script<'a> {
14    /// Validate all spans reference source text correctly
15    ///
16    /// Debug helper to ensure zero-copy invariants are maintained.
17    #[cfg(debug_assertions)]
18    #[must_use]
19    pub fn validate_spans(&self) -> bool {
20        let source_ptr = self.source.as_ptr();
21        let source_range = source_ptr as usize..source_ptr as usize + self.source.len();
22
23        self.sections
24            .iter()
25            .all(|section| section.validate_spans(&source_range))
26    }
27
28    /// Get byte range for a section
29    ///
30    /// Returns the byte range (start..end) for the specified section type,
31    /// or None if the section doesn't exist or has no span.
32    #[must_use]
33    pub fn section_range(&self, section_type: SectionType) -> Option<core::ops::Range<usize>> {
34        self.find_section(section_type)?
35            .span()
36            .map(|s| s.start..s.end)
37    }
38
39    /// Find section containing the given byte offset
40    ///
41    /// Returns the section that contains the specified byte offset,
42    /// or None if no section contains that offset.
43    #[must_use]
44    pub fn section_at_offset(&self, offset: usize) -> Option<&Section<'a>> {
45        self.sections.iter().find(|s| {
46            s.span()
47                .is_some_and(|span| span.start <= offset && offset < span.end)
48        })
49    }
50
51    /// Get all section boundaries for quick lookup
52    ///
53    /// Returns a vector of (`SectionType`, `Range`) pairs for all sections
54    /// that have valid spans. Useful for building lookup tables or
55    /// determining which sections need reparsing after edits.
56    #[must_use]
57    pub fn section_boundaries(&self) -> Vec<(SectionType, core::ops::Range<usize>)> {
58        self.sections
59            .iter()
60            .filter_map(|s| {
61                s.span()
62                    .map(|span| (s.section_type(), span.start..span.end))
63            })
64            .collect()
65    }
66}