|  | 
|  | 1 | +use std::io; | 
|  | 2 | + | 
|  | 3 | +use pg_console::markup; | 
|  | 4 | +use pg_diagnostics::{Advices, Diagnostic, LogCategory, MessageAndDescription, Severity, Visit}; | 
|  | 5 | +use sqlx::postgres::{PgDatabaseError, PgSeverity}; | 
|  | 6 | +use text_size::TextRange; | 
|  | 7 | + | 
|  | 8 | +/// A specialized diagnostic for the typechecker. | 
|  | 9 | +/// | 
|  | 10 | +/// Type diagnostics are always **errors**. | 
|  | 11 | +#[derive(Clone, Debug, Diagnostic)] | 
|  | 12 | +#[diagnostic(category = "typecheck")] | 
|  | 13 | +pub struct TypecheckDiagnostic { | 
|  | 14 | + #[location(span)] | 
|  | 15 | + span: Option<TextRange>, | 
|  | 16 | + #[description] | 
|  | 17 | + #[message] | 
|  | 18 | + message: MessageAndDescription, | 
|  | 19 | + #[advice] | 
|  | 20 | + advices: TypecheckAdvices, | 
|  | 21 | + #[severity] | 
|  | 22 | + severity: Severity, | 
|  | 23 | +} | 
|  | 24 | + | 
|  | 25 | +#[derive(Debug, Clone)] | 
|  | 26 | +struct TypecheckAdvices { | 
|  | 27 | + code: String, | 
|  | 28 | + schema: Option<String>, | 
|  | 29 | + table: Option<String>, | 
|  | 30 | + column: Option<String>, | 
|  | 31 | + data_type: Option<String>, | 
|  | 32 | + constraint: Option<String>, | 
|  | 33 | + line: Option<usize>, | 
|  | 34 | + file: Option<String>, | 
|  | 35 | + detail: Option<String>, | 
|  | 36 | + routine: Option<String>, | 
|  | 37 | + where_: Option<String>, | 
|  | 38 | + hint: Option<String>, | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +impl Advices for TypecheckAdvices { | 
|  | 42 | + fn record(&self, visitor: &mut dyn Visit) -> io::Result<()> { | 
|  | 43 | + // First, show the error code | 
|  | 44 | + visitor.record_log( | 
|  | 45 | + LogCategory::Error, | 
|  | 46 | + &markup! { "Error Code: " <Emphasis>{&self.code}</Emphasis> }, | 
|  | 47 | + )?; | 
|  | 48 | + | 
|  | 49 | + // Show detailed message if available | 
|  | 50 | + if let Some(detail) = &self.detail { | 
|  | 51 | + visitor.record_log(LogCategory::Info, &detail)?; | 
|  | 52 | + } | 
|  | 53 | + | 
|  | 54 | + // Show object location information | 
|  | 55 | + if let (Some(schema), Some(table)) = (&self.schema, &self.table) { | 
|  | 56 | + let mut location = format!("In table: {schema}.{table}"); | 
|  | 57 | + if let Some(column) = &self.column { | 
|  | 58 | + location.push_str(&format!(", column: {column}")); | 
|  | 59 | + } | 
|  | 60 | + visitor.record_log(LogCategory::Info, &location)?; | 
|  | 61 | + } | 
|  | 62 | + | 
|  | 63 | + // Show constraint information | 
|  | 64 | + if let Some(constraint) = &self.constraint { | 
|  | 65 | + visitor.record_log( | 
|  | 66 | + LogCategory::Info, | 
|  | 67 | + &markup! { "Constraint: " <Emphasis>{constraint}</Emphasis> }, | 
|  | 68 | + )?; | 
|  | 69 | + } | 
|  | 70 | + | 
|  | 71 | + // Show data type information | 
|  | 72 | + if let Some(data_type) = &self.data_type { | 
|  | 73 | + visitor.record_log( | 
|  | 74 | + LogCategory::Info, | 
|  | 75 | + &markup! { "Data type: " <Emphasis>{data_type}</Emphasis> }, | 
|  | 76 | + )?; | 
|  | 77 | + } | 
|  | 78 | + | 
|  | 79 | + // Show context information | 
|  | 80 | + if let Some(where_) = &self.where_ { | 
|  | 81 | + visitor.record_log(LogCategory::Info, &markup! { "Context:\n"{where_}"" })?; | 
|  | 82 | + } | 
|  | 83 | + | 
|  | 84 | + // Show hint if available | 
|  | 85 | + if let Some(hint) = &self.hint { | 
|  | 86 | + visitor.record_log(LogCategory::Info, &markup! { "Hint: "{hint}"" })?; | 
|  | 87 | + } | 
|  | 88 | + | 
|  | 89 | + // Show source location if available | 
|  | 90 | + if let (Some(file), Some(line)) = (&self.file, &self.line) { | 
|  | 91 | + if let Some(routine) = &self.routine { | 
|  | 92 | + visitor.record_log( | 
|  | 93 | + LogCategory::Info, | 
|  | 94 | + &markup! { "Source: "{file}":"{line}" in "{routine}"" }, | 
|  | 95 | + )?; | 
|  | 96 | + } else { | 
|  | 97 | + visitor.record_log(LogCategory::Info, &markup! { "Source: "{file}":"{line}"" })?; | 
|  | 98 | + } | 
|  | 99 | + } | 
|  | 100 | + | 
|  | 101 | + Ok(()) | 
|  | 102 | + } | 
|  | 103 | +} | 
|  | 104 | + | 
|  | 105 | +pub(crate) fn create_type_error( | 
|  | 106 | + pg_err: &PgDatabaseError, | 
|  | 107 | + ts: Option<&tree_sitter::Tree>, | 
|  | 108 | +) -> TypecheckDiagnostic { | 
|  | 109 | + let position = pg_err.position().and_then(|pos| match pos { | 
|  | 110 | + sqlx::postgres::PgErrorPosition::Original(pos) => Some(pos - 1), | 
|  | 111 | + _ => None, | 
|  | 112 | + }); | 
|  | 113 | + | 
|  | 114 | + let range = position.and_then(|pos| { | 
|  | 115 | + ts.and_then(|tree| { | 
|  | 116 | + tree.root_node() | 
|  | 117 | + .named_descendant_for_byte_range(pos, pos) | 
|  | 118 | + .map(|node| { | 
|  | 119 | + TextRange::new( | 
|  | 120 | + node.start_byte().try_into().unwrap(), | 
|  | 121 | + node.end_byte().try_into().unwrap(), | 
|  | 122 | + ) | 
|  | 123 | + }) | 
|  | 124 | + }) | 
|  | 125 | + }); | 
|  | 126 | + | 
|  | 127 | + let severity = match pg_err.severity() { | 
|  | 128 | + PgSeverity::Panic => Severity::Error, | 
|  | 129 | + PgSeverity::Fatal => Severity::Error, | 
|  | 130 | + PgSeverity::Error => Severity::Error, | 
|  | 131 | + PgSeverity::Warning => Severity::Warning, | 
|  | 132 | + PgSeverity::Notice => Severity::Hint, | 
|  | 133 | + PgSeverity::Debug => Severity::Hint, | 
|  | 134 | + PgSeverity::Info => Severity::Information, | 
|  | 135 | + PgSeverity::Log => Severity::Information, | 
|  | 136 | + }; | 
|  | 137 | + | 
|  | 138 | + TypecheckDiagnostic { | 
|  | 139 | + message: pg_err.to_string().into(), | 
|  | 140 | + severity, | 
|  | 141 | + span: range, | 
|  | 142 | + advices: TypecheckAdvices { | 
|  | 143 | + code: pg_err.code().to_string(), | 
|  | 144 | + hint: pg_err.hint().and_then(|s| { | 
|  | 145 | + if !s.is_empty() { | 
|  | 146 | + Some(s.to_string()) | 
|  | 147 | + } else { | 
|  | 148 | + None | 
|  | 149 | + } | 
|  | 150 | + }), | 
|  | 151 | + schema: pg_err.schema().and_then(|s| { | 
|  | 152 | + if !s.is_empty() { | 
|  | 153 | + Some(s.to_string()) | 
|  | 154 | + } else { | 
|  | 155 | + None | 
|  | 156 | + } | 
|  | 157 | + }), | 
|  | 158 | + table: pg_err.table().and_then(|s| { | 
|  | 159 | + if !s.is_empty() { | 
|  | 160 | + Some(s.to_string()) | 
|  | 161 | + } else { | 
|  | 162 | + None | 
|  | 163 | + } | 
|  | 164 | + }), | 
|  | 165 | + detail: pg_err.detail().and_then(|s| { | 
|  | 166 | + if !s.is_empty() { | 
|  | 167 | + Some(s.to_string()) | 
|  | 168 | + } else { | 
|  | 169 | + None | 
|  | 170 | + } | 
|  | 171 | + }), | 
|  | 172 | + column: pg_err.column().and_then(|s| { | 
|  | 173 | + if !s.is_empty() { | 
|  | 174 | + Some(s.to_string()) | 
|  | 175 | + } else { | 
|  | 176 | + None | 
|  | 177 | + } | 
|  | 178 | + }), | 
|  | 179 | + data_type: pg_err.data_type().and_then(|s| { | 
|  | 180 | + if !s.is_empty() { | 
|  | 181 | + Some(s.to_string()) | 
|  | 182 | + } else { | 
|  | 183 | + None | 
|  | 184 | + } | 
|  | 185 | + }), | 
|  | 186 | + constraint: pg_err.constraint().and_then(|s| { | 
|  | 187 | + if !s.is_empty() { | 
|  | 188 | + Some(s.to_string()) | 
|  | 189 | + } else { | 
|  | 190 | + None | 
|  | 191 | + } | 
|  | 192 | + }), | 
|  | 193 | + line: pg_err.line(), | 
|  | 194 | + file: pg_err.file().and_then(|s| { | 
|  | 195 | + if !s.is_empty() { | 
|  | 196 | + Some(s.to_string()) | 
|  | 197 | + } else { | 
|  | 198 | + None | 
|  | 199 | + } | 
|  | 200 | + }), | 
|  | 201 | + routine: pg_err.routine().and_then(|s| { | 
|  | 202 | + if !s.is_empty() { | 
|  | 203 | + Some(s.to_string()) | 
|  | 204 | + } else { | 
|  | 205 | + None | 
|  | 206 | + } | 
|  | 207 | + }), | 
|  | 208 | + where_: pg_err.r#where().and_then(|s| { | 
|  | 209 | + if !s.is_empty() { | 
|  | 210 | + Some(s.to_string()) | 
|  | 211 | + } else { | 
|  | 212 | + None | 
|  | 213 | + } | 
|  | 214 | + }), | 
|  | 215 | + }, | 
|  | 216 | + } | 
|  | 217 | +} | 
0 commit comments