use std::ops::Range;
#[derive(Debug, Clone)]
pub struct Line {
pub no: u64,
pub bytes: Vec<u8>,
}
impl Line {
pub fn new(no: u64, bytes: Vec<u8>) -> Self {
Self { no, bytes }
}
pub fn as_str_lossy(&self) -> std::borrow::Cow<'_, str> {
String::from_utf8_lossy(&self.bytes)
}
}
#[derive(Debug, Default, Clone)]
pub struct MatchInfo {
pub hit: bool,
pub spans: Vec<Range<usize>>,
pub col: Option<usize>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
Target,
Context,
}
#[derive(Debug, Clone)]
pub struct Emit<'a> {
pub line: &'a Line,
pub role: Role,
pub match_info: &'a MatchInfo,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn line_as_str_lossy_handles_invalid_utf8() {
let line = Line::new(1, vec![0xFF, b'a']);
let s = line.as_str_lossy();
assert!(s.ends_with('a'));
}
#[test]
fn match_info_default_is_miss() {
let mi = MatchInfo::default();
assert!(!mi.hit);
assert!(mi.spans.is_empty());
assert!(mi.col.is_none());
}
}