no_mistakes_postgres_engine/embedded/helpers/
summary.rs1use super::split::{with_origin_entry, Marker};
4use super::template::{Spliced, TemplateHelper};
5use crate::embedded::walk::resolve::functions::provenance::RecoveredSql;
6use crate::embedded::EmbeddedSqlCall;
7use std::path::Path;
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14pub struct LiteralArg {
15 pub text: String,
16 pub line: u32,
17 pub origin: u32,
18}
19
20impl LiteralArg {
21 pub fn bare(text: impl Into<String>) -> Self {
22 Self {
23 text: text.into(),
24 line: 0,
25 origin: 0,
26 }
27 }
28
29 fn splice<'a>(&'a self, caller: &Path) -> Spliced<'a> {
30 Spliced {
31 text: std::borrow::Cow::Borrowed(&self.text),
32 at: (self.line > 0).then(|| Marker {
33 line: self.line,
34 path: Some(caller.to_path_buf()),
35 origin: self.origin,
36 }),
37 }
38 }
39}
40
41impl From<&str> for LiteralArg {
42 fn from(text: &str) -> Self {
43 Self::bare(text)
44 }
45}
46
47impl From<String> for LiteralArg {
48 fn from(text: String) -> Self {
49 Self::bare(text)
50 }
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, Hash)]
55pub enum SiteArg {
56 Literal(LiteralArg),
57 Bool(bool),
58 Null,
59 Undefined,
60 Object(Vec<(String, SiteArg)>),
62 Array(Vec<SiteArg>),
64 Scalar,
66 Builder,
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub enum SiteKind {
73 Sql,
74 PlainClause,
75 ParameterizedMember,
76}
77
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct ImportedSite {
80 pub local: String,
82 pub member: Option<String>,
84 pub kind: SiteKind,
86 pub args: Vec<SiteArg>,
87 pub value_used: bool,
90 pub line: u32,
92 pub constant: Option<bool>,
95}
96
97#[derive(Debug, Clone, PartialEq, Eq)]
99pub struct PendingImportedSql {
100 pub span: (u32, u32),
102 pub template: EmbeddedSqlCall,
104 pub sites: Vec<(usize, ImportedSite)>,
105 pub correlation: crate::fx::FxHashMap<usize, u32>,
107}
108
109#[derive(Clone, PartialEq, Eq)]
111pub enum HelperSummary {
112 Static(RecoveredSql),
114 Template(TemplateHelper),
116 Builder(BuilderHelper),
118}
119
120#[derive(Clone, PartialEq, Eq)]
121pub struct BuilderHelper {
122 pub parameter_index: usize,
123 pub parameter_count: usize,
124 pub suffix: RecoveredSql,
126}
127
128impl HelperSummary {
129 pub fn fill(&self, site: &ImportedSite, helper: &Path, caller: &Path) -> Option<RecoveredSql> {
134 let builders = site
135 .args
136 .iter()
137 .filter(|arg| **arg == SiteArg::Builder)
138 .count();
139 let recovered = match self {
140 Self::Static(sql) if builders == 0 => sql.clone(),
141 Self::Template(template) if builders == 0 => {
142 let literals = site
143 .args
144 .iter()
145 .map(|arg| match arg {
146 SiteArg::Literal(literal) => Some(literal.splice(caller)),
147 _ => None,
148 })
149 .collect::<Option<Vec<_>>>()?;
150 template.apply(&literals)?
151 }
152 Self::Builder(builder)
153 if builders == 1
154 && site.args.len() == builder.parameter_count
155 && site.args.get(builder.parameter_index) == Some(&SiteArg::Builder) =>
156 {
157 builder.suffix.clone()
158 }
159 _ => return None,
160 };
161 Some(with_origin_entry(recovered).with_source_file(helper))
162 }
163}