string_sections/
section_span.rs

1use std::ops::{Deref, Range};
2
3use crate::LineSpan;
4
5#[derive(PartialEq, Eq, Clone, Copy)]
6pub struct SectionSpan<'a> {
7    pub start_line: LineSpan<'a>,
8    pub end_line: LineSpan<'a>,
9}
10
11const LBR: &[char] = &['\n', '\r'];
12
13impl<'a> SectionSpan<'a> {
14    /// Returns the byte index range of the section, excluding the start and end lines
15    pub fn inner_range(&self) -> Range<usize> {
16        self.start_line.end..self.end_line.start
17    }
18
19    /// Returns the byte index range of the section, including the start and end lines
20    pub fn outer_range(&self) -> Range<usize> {
21        self.start_line.start..self.end_line.end
22    }
23
24    /// Returns section content as `&str`
25    pub fn as_str(&self) -> &'a str {
26        &self.start_line.text[self.inner_range()].trim_matches(LBR)
27    }
28}
29
30impl<'a> Deref for SectionSpan<'a> {
31    type Target = str;
32
33    fn deref(&self) -> &Self::Target {
34        self.as_str()
35    }
36}