pub(crate) mod blocks;
pub(crate) mod markdown;
pub(crate) mod plain;
use crate::domain::{self, Format, ParagraphKind, Skipped, Span};
pub trait Decomposer: Send {
fn decompose(&self, input: &[u8]) -> domain::Result<Read>;
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Read {
pub(crate) text: Option<String>,
pub(crate) blocks: Vec<ReadBlock>,
pub(crate) skipped: Vec<Skipped>,
}
impl Read {
pub fn new(blocks: Vec<ReadBlock>, skipped: Vec<Skipped>) -> Read {
Read {
text: None,
blocks,
skipped,
}
}
#[must_use]
pub fn with_text(mut self, text: impl Into<String>) -> Read {
self.text = Some(text.into());
self
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ReadBlock(pub(crate) BlockKind);
#[derive(Debug, Clone)]
pub(crate) enum BlockKind {
Heading {
level: usize,
text: String,
span: Span,
},
Paragraph {
kind: ParagraphKind,
text: String,
span: Span,
map: TextMap,
},
}
impl ReadBlock {
pub fn heading(level: usize, text: impl Into<String>, span: Span) -> ReadBlock {
ReadBlock(BlockKind::Heading {
level,
text: text.into(),
span,
})
}
pub fn paragraph(kind: ParagraphKind, text: impl Into<String>, span: Span) -> ReadBlock {
let text = text.into();
let map = TextMap::verbatim(span.byte_start, text.len());
ReadBlock(BlockKind::Paragraph {
kind,
text,
span,
map,
})
}
pub(crate) fn mapped(kind: ParagraphKind, text: String, span: Span, map: TextMap) -> ReadBlock {
ReadBlock(BlockKind::Paragraph {
kind,
text,
span,
map,
})
}
pub(crate) fn span_mut(&mut self) -> &mut Span {
match &mut self.0 {
BlockKind::Heading { span, .. } | BlockKind::Paragraph { span, .. } => span,
}
}
pub(crate) fn span(&self) -> Span {
match &self.0 {
BlockKind::Heading { span, .. } | BlockKind::Paragraph { span, .. } => *span,
}
}
}
pub(crate) fn text_of(input: &[u8]) -> domain::Result<&str> {
std::str::from_utf8(input).map_err(|_| domain::Error::UnsupportedFormat {
format: None,
feature: None,
})
}
#[derive(Default)]
pub(crate) struct Decomposers {
entries: Vec<(Format, Box<dyn Decomposer>)>,
}
impl Decomposers {
pub(crate) fn new() -> Self {
Self {
entries: Vec::new(),
}
}
#[must_use]
pub(crate) fn with(mut self, format: Format, decomposer: Box<dyn Decomposer>) -> Self {
if let Some(slot) = self.entries.iter_mut().find(|(f, _)| *f == format) {
slot.1 = decomposer;
} else {
self.entries.push((format, decomposer));
}
self
}
pub(crate) fn get(&self, format: &Format) -> Option<&dyn Decomposer> {
self.entries
.iter()
.find(|(f, _)| f == format)
.map(|(_, d)| d.as_ref())
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct TextMap {
origin: usize,
pieces: Vec<Piece>,
}
#[derive(Debug, Clone, Copy)]
struct Piece {
text_start: usize,
len: usize,
source: Span,
verbatim: bool,
}
impl TextMap {
pub(crate) fn verbatim(start: usize, len: usize) -> Self {
let mut map = Self {
origin: start,
pieces: Vec::new(),
};
map.push(0, len, Span::new(start, start + len), true);
map
}
pub(crate) fn at(origin: usize) -> Self {
Self {
origin,
pieces: Vec::new(),
}
}
pub(crate) fn push(&mut self, text_start: usize, len: usize, source: Span, verbatim: bool) {
if len == 0 {
return;
}
self.pieces.push(Piece {
text_start,
len,
source,
verbatim,
});
}
pub(crate) fn window(&self, from: usize, to: usize) -> Self {
let mut out = Self::at(self.origin);
for piece in &self.pieces {
let start = piece.text_start.max(from);
let end = (piece.text_start + piece.len).min(to);
if start >= end {
continue;
}
let source = if piece.verbatim {
let offset = piece.source.byte_start + (start - piece.text_start);
Span::new(offset, offset + (end - start))
} else {
piece.source
};
out.push(start - from, end - start, source, piece.verbatim);
}
if let Some(first) = out.pieces.first() {
out.origin = first.source.byte_start;
}
out
}
pub(crate) fn mismatch(&self, text: &str, analysed: &str, span: Span) -> Option<String> {
let inside = |s: Span| span.byte_start <= s.byte_start && s.byte_end <= span.byte_end;
if !inside(Span::new(self.origin, self.origin)) {
return Some(format!(
"its empty text maps to byte {}, outside its span",
self.origin
));
}
for piece in &self.pieces {
let end = piece.text_start + piece.len;
let Some(own) = text.get(piece.text_start..end) else {
return Some(format!(
"bytes {}..{end} of its text are not a slice of the text",
piece.text_start
));
};
if piece.source.byte_start > piece.source.byte_end || !inside(piece.source) {
return Some(format!(
"bytes {}..{end} of its text map to {}..{}, outside its span",
piece.text_start, piece.source.byte_start, piece.source.byte_end
));
}
let source = analysed.get(piece.source.byte_start..piece.source.byte_end);
if piece.verbatim && source != Some(own) {
return Some(format!(
"its text {own:?} at {}..{end} is not the analysed text at {}..{}",
piece.text_start, piece.source.byte_start, piece.source.byte_end
));
}
}
None
}
pub(crate) fn map(&self, local: Span) -> Span {
let start = self.map_start(local.byte_start);
if local.byte_end <= local.byte_start {
return Span::new(start, start);
}
let end = self.map_end(local.byte_end).max(start);
Span::new(start, end)
}
fn map_start(&self, at: usize) -> usize {
for piece in &self.pieces {
if at < piece.text_start + piece.len {
if at < piece.text_start {
return piece.source.byte_start;
}
return if piece.verbatim {
piece.source.byte_start + (at - piece.text_start)
} else {
piece.source.byte_start
};
}
}
self.pieces
.last()
.map_or(self.origin, |p| p.source.byte_end)
}
fn map_end(&self, at: usize) -> usize {
let mut end = self.origin;
for piece in &self.pieces {
if piece.text_start >= at {
break;
}
end = if piece.verbatim && at < piece.text_start + piece.len {
piece.source.byte_start + (at - piece.text_start)
} else {
piece.source.byte_end
};
}
end
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Marker(&'static str);
impl Decomposer for Marker {
fn decompose(&self, _input: &[u8]) -> domain::Result<Read> {
Ok(Read::new(
vec![ReadBlock::heading(1, self.0, Span::default())],
Vec::new(),
))
}
}
fn marker_of(d: &dyn Decomposer) -> String {
match d.decompose(b"").unwrap().blocks.into_iter().next() {
Some(ReadBlock(BlockKind::Heading { text, .. })) => text,
_ => String::new(),
}
}
#[test]
fn empty_table_supports_nothing() {
let table = Decomposers::new();
assert!(table.get(&Format::Markdown).is_none());
assert!(table.get(&Format::PlainText).is_none());
}
#[test]
fn lookup_finds_the_registered_decomposer() {
let table = Decomposers::new()
.with(Format::Markdown, Box::new(Marker("md")))
.with(Format::PlainText, Box::new(Marker("plain")));
assert_eq!(marker_of(table.get(&Format::Markdown).unwrap()), "md");
assert_eq!(marker_of(table.get(&Format::PlainText).unwrap()), "plain");
assert!(table.get(&Format::Pdf).is_none());
}
#[test]
fn with_replaces_on_duplicate_key() {
let table = Decomposers::new()
.with(Format::Markdown, Box::new(Marker("first")))
.with(Format::Markdown, Box::new(Marker("second")));
assert_eq!(marker_of(table.get(&Format::Markdown).unwrap()), "second");
}
#[test]
fn a_verbatim_map_shifts_by_its_start() {
let map = TextMap::verbatim(10, 5);
assert_eq!(map.map(Span::new(1, 4)), Span::new(11, 14));
assert_eq!(map.map(Span::new(0, 5)), Span::new(10, 15));
}
#[test]
fn a_map_skips_markup_between_pieces() {
let mut map = TextMap::at(0);
map.push(0, 2, Span::new(0, 2), true);
map.push(2, 1, Span::new(4, 5), true);
assert_eq!(map.map(Span::new(2, 3)), Span::new(4, 5));
assert_eq!(map.map(Span::new(0, 3)), Span::new(0, 5));
}
#[test]
fn an_opaque_piece_maps_whole() {
let mut map = TextMap::at(0);
map.push(0, 2, Span::new(0, 2), true);
map.push(2, 1, Span::new(2, 7), false);
assert_eq!(map.map(Span::new(2, 3)), Span::new(2, 7));
assert_eq!(map.map(Span::new(0, 1)), Span::new(0, 1));
}
#[test]
fn a_window_rebases_and_keeps_offsets() {
let map = TextMap::verbatim(100, 10).window(2, 8);
assert_eq!(map.map(Span::new(0, 6)), Span::new(102, 108));
}
#[test]
fn an_empty_map_maps_to_its_origin() {
let map = TextMap::at(42);
assert_eq!(map.map(Span::new(0, 0)), Span::new(42, 42));
}
}