Struct regex_syntax::error::Spans
source · struct Spans<'p> {
pattern: &'p str,
line_number_width: usize,
by_line: Vec<Vec<Span>>,
multi_line: Vec<Span>,
}
Expand description
This type represents an arbitrary number of error spans in a way that makes it convenient to notate the regex pattern. (“Notate” means “point out exactly where the error occurred in the regex pattern.”)
Technically, we can only ever have two spans given our current error structure. However, after toiling with a specific algorithm for handling two spans, it became obvious that an algorithm to handle an arbitrary number of spans was actually much simpler.
Fields§
§pattern: &'p str
The original regex pattern string.
line_number_width: usize
The total width that should be used for line numbers. The width is used for left padding the line numbers for alignment.
A value of 0
means line numbers should not be displayed. That is,
the pattern is itself only one line.
by_line: Vec<Vec<Span>>
All error spans that occur on a single line. This sequence always has
length equivalent to the number of lines in pattern
, where the index
of the sequence represents a line number, starting at 0
. The spans
in each line are sorted in ascending order.
multi_line: Vec<Span>
All error spans that occur over one or more lines. That is, the start and end position of the span have different line numbers. The spans are sorted in ascending order.
Implementations§
source§impl<'p> Spans<'p>
impl<'p> Spans<'p>
sourcefn from_formatter<'e, E: Display>(fmter: &'p Formatter<'e, E>) -> Spans<'p>
fn from_formatter<'e, E: Display>(fmter: &'p Formatter<'e, E>) -> Spans<'p>
Build a sequence of spans from a formatter.
sourcefn add(&mut self, span: Span)
fn add(&mut self, span: Span)
Add the given span to this sequence, putting it in the right place.
sourcefn notate(&self) -> String
fn notate(&self) -> String
Notate the pattern string with carents (^
) pointing at each span
location. This only applies to spans that occur within a single line.
sourcefn notate_line(&self, i: usize) -> Option<String>
fn notate_line(&self, i: usize) -> Option<String>
Return notes for the line indexed at i
(zero-based). If there are no
spans for the given line, then None
is returned. Otherwise, an
appropriately space padded string with correctly positioned ^
is
returned, accounting for line numbers.
sourcefn left_pad_line_number(&self, n: usize) -> String
fn left_pad_line_number(&self, n: usize) -> String
Left pad the given line number with spaces such that it is aligned with other line numbers.
sourcefn line_number_padding(&self) -> usize
fn line_number_padding(&self) -> usize
Return the line number padding beginning at the start of each line of the pattern.
If the pattern is only one line, then this returns a fixed padding for visual indentation.