roan_error/span.rs
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
use crate::position::Position;
/// Represents a span of text between two positions, including the literal text.
#[derive(Clone, PartialEq, Eq)]
pub struct TextSpan {
/// The starting position of the text span.
pub start: Position,
/// The ending position of the text span.
pub end: Position,
/// The literal text contained in the span.
pub literal: String,
}
impl TextSpan {
/// Creates a new `TextSpan` from a starting position, an ending position, and a literal string.
///
/// # Arguments
///
/// * `start` - The starting position of the text span.
/// * `end` - The ending position of the text span.
/// * `literal` - The literal text represented by the span.
///
/// # Example
///
/// ```
/// use roan_error::{Position, TextSpan};
/// let start = Position::new(1, 1, 0);
/// let end = Position::new(1, 5, 4);
/// let span = TextSpan::new(start, end, "test".to_string());
/// assert_eq!(span.length(), 4);
/// ```
pub fn new(start: Position, end: Position, literal: String) -> Self {
Self {
start,
end,
literal,
}
}
/// Combines multiple `TextSpan` objects into one. The spans are sorted by their starting positions.
///
/// # Panics
///
/// Panics if the input vector is empty.
///
/// # Arguments
///
/// * `spans` - A vector of `TextSpan` objects to combine.
///
/// # Returns
///
/// A new `TextSpan` that spans from the start of the first span to the end of the last span,
/// with the concatenated literal text.
///
/// # Example
///
/// ```
/// use roan_error::{Position, TextSpan};
/// let span1 = TextSpan::new(Position::new(1, 1, 0), Position::new(1, 5, 4), "test".to_string());
/// let span2 = TextSpan::new(Position::new(1, 6, 5), Position::new(1, 10, 9), "span".to_string());
/// let combined = TextSpan::combine(vec![span1, span2]);
/// assert_eq!(combined.unwrap().literal, "testspan");
/// ```
pub fn combine(mut spans: Vec<TextSpan>) -> Option<TextSpan> {
if spans.is_empty() {
return None;
}
spans.sort_by(|a, b| a.start.index.cmp(&b.start.index));
let start = spans.first().unwrap().start;
let end = spans.last().unwrap().end;
Some(TextSpan::new(
start,
end,
spans.into_iter().map(|span| span.literal).collect(),
))
}
/// Returns the length of the span, calculated as the difference between the end and start indices.
///
/// # Returns
///
/// The length of the text span in bytes.
pub fn length(&self) -> usize {
self.end.index - self.start.index
}
/// Extracts the literal text from the given input string based on the start and end positions.
///
/// # Arguments
///
/// * `input` - The input string from which to extract the literal text.
///
/// # Returns
///
/// A slice of the input string that corresponds to the span's range.
pub fn literal<'a>(&self, input: &'a str) -> &'a str {
&input[self.start.index..self.end.index]
}
}
impl Default for TextSpan {
/// Creates a new `TextSpan` with default values.
///
/// # Returns
///
/// A new `TextSpan` with the starting and ending positions set to `(0, 0, 0)` and an empty string.
fn default() -> Self {
Self {
start: Position::default(),
end: Position::default(),
literal: String::new(),
}
}
}
impl std::fmt::Debug for TextSpan {
/// Formats the `TextSpan` as `"literal" (line:column)`.
///
/// # Example
///
/// ```
/// use roan_error::{Position, TextSpan};
/// let span = TextSpan::new(Position::new(1, 1, 0), Position::new(1, 5, 4), "test".to_string());
/// assert_eq!(format!("{:?}", span), "\"test\" (1:1)");
/// ```
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"\"{}\" ({}:{})",
self.literal, self.start.line, self.start.column
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let start = Position::new(1, 1, 0);
let end = Position::new(1, 5, 4);
let span = TextSpan::new(start, end, "test".to_string());
assert_eq!(span.start, start);
assert_eq!(span.end, end);
assert_eq!(span.literal, "test");
}
#[test]
fn test_combine() {
let span1 = TextSpan::new(
Position::new(1, 1, 0),
Position::new(1, 5, 4),
"test".to_string(),
);
let span2 = TextSpan::new(
Position::new(1, 6, 5),
Position::new(1, 10, 9),
"span".to_string(),
);
let combined = TextSpan::combine(vec![span1, span2]).unwrap();
assert_eq!(combined.start, Position::new(1, 1, 0));
assert_eq!(combined.end, Position::new(1, 10, 9));
assert_eq!(combined.literal, "testspan");
}
#[test]
fn test_combine_empty() {
assert_eq!(TextSpan::combine(vec![]), None);
}
#[test]
fn test_length() {
let span = TextSpan::new(
Position::new(1, 1, 0),
Position::new(1, 5, 4),
"test".to_string(),
);
assert_eq!(span.length(), 4);
}
#[test]
fn test_literal() {
let span = TextSpan::new(
Position::new(1, 1, 0),
Position::new(1, 5, 4),
"test".to_string(),
);
assert_eq!(span.literal("test string"), "test");
}
#[test]
fn test_default() {
let span = TextSpan::default();
assert_eq!(span.start, Position::default());
assert_eq!(span.end, Position::default());
assert_eq!(span.literal, "");
}
#[test]
fn test_debug() {
let span = TextSpan::new(
Position::new(1, 1, 0),
Position::new(1, 5, 4),
"test".to_string(),
);
assert_eq!(format!("{:?}", span), "\"test\" (1:1)");
}
}