Skip to main content

no_mistakes_postgres_engine/lock_inventory/embedded/
positions.rs

1use crate::embedded::{
2    EmbeddedSqlCall, EmbeddedSqlKind, EmbeddedSqlSourcePosition, SqlByteOrigins,
3};
4use std::path::PathBuf;
5
6pub struct Positions<'a> {
7    sql: &'a str,
8    ascii: bool,
9    line_starts: Vec<usize>,
10    offsets: &'a SqlByteOrigins,
11    positions: &'a [EmbeddedSqlSourcePosition],
12    call_line: usize,
13    base_line: usize,
14}
15
16impl<'a> Positions<'a> {
17    pub fn new(call: &'a EmbeddedSqlCall, variant_index: usize) -> Self {
18        let variant = call
19            .variants
20            .get(variant_index)
21            .filter(|_| !call.mirrors_single_variant());
22        let (sql, offsets, positions, base_line) = if let Some(variant) = variant {
23            (
24                variant.sql_text.as_str(),
25                &variant.sql_source_offsets,
26                variant.sql_source_positions.as_slice(),
27                variant.line,
28            )
29        } else {
30            let base = if call.kind == EmbeddedSqlKind::Inline {
31                call.line
32            } else {
33                call.declaration_line.unwrap_or(call.line)
34            };
35            (
36                call.sql_text.as_deref().unwrap_or_default(),
37                &call.sql_source_offsets,
38                call.sql_source_positions.as_slice(),
39                base,
40            )
41        };
42        Self {
43            sql,
44            ascii: sql.is_ascii(),
45            line_starts: std::iter::once(0)
46                .chain(sql.match_indices('\n').map(|(offset, _)| offset + 1))
47                .collect(),
48            offsets,
49            positions,
50            call_line: call.line as usize,
51            base_line: base_line as usize,
52        }
53    }
54
55    pub fn source(&self, line: usize, column: usize) -> (Option<PathBuf>, usize, usize) {
56        let (path, origin_line, offset, _) = self.origin(line, column);
57        let report_line = if path.is_some() {
58            self.call_line
59        } else {
60            origin_line
61        };
62        (path, report_line, offset.unwrap_or(column))
63    }
64
65    pub fn origin(
66        &self,
67        line: usize,
68        column: usize,
69    ) -> (Option<PathBuf>, usize, Option<usize>, usize) {
70        let start = self.positions.partition_point(|position| {
71            (position.sql_line as usize, position.sql_column as usize) <= (line, column)
72        });
73        let position = start
74            .checked_sub(1)
75            .and_then(|index| self.positions.get(index));
76        let path = position.and_then(|position| position.source_path.clone());
77        let source_line = if let Some(position) = position {
78            position.source_line as usize + line.saturating_sub(position.sql_line as usize)
79        } else {
80            self.base_line.saturating_add(line.saturating_sub(1))
81        };
82        let offset = self.mapped_offset(line, column);
83        let fragment_column = position
84            .filter(|pos| pos.sql_line as usize == line)
85            .map_or(column, |pos| column - pos.sql_column as usize + 1);
86        (
87            path,
88            source_line,
89            offset.map(|offset| offset as usize),
90            fragment_column,
91        )
92    }
93
94    pub fn mapped_offset(&self, line: usize, column: usize) -> Option<u32> {
95        self.byte_offset(line, column)
96            .and_then(|offset| self.offsets.get(offset))
97            .filter(|offset| *offset != 0)
98    }
99
100    pub fn byte_offset(&self, line: usize, column: usize) -> Option<usize> {
101        let start = *self.line_starts.get(line.checked_sub(1)?)?;
102        let column = column.checked_sub(1)?;
103        if self.ascii {
104            return start.checked_add(column);
105        }
106        self.sql
107            .get(start..)?
108            .char_indices()
109            .nth(column)
110            .map(|(offset, _)| start + offset)
111    }
112}
113
114#[cfg(test)]
115mod tests;