|
| 1 | +use std::{ |
| 2 | + collections::HashMap, |
| 3 | + fmt::{Debug, Display}, |
| 4 | +}; |
| 5 | + |
| 6 | +use sqltk::NodeKey; |
| 7 | +use sqltk_parser::ast::{ |
| 8 | + Delete, Expr, Function, Insert, Query, Select, SelectItem, SetExpr, Statement, Value, Values, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::{EqlValue, Param, Type}; |
| 12 | + |
| 13 | +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] |
| 14 | +pub struct Fmt<T>(pub(crate) T); |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] |
| 17 | +pub struct FmtAst<T>(pub(crate) T); |
| 18 | + |
| 19 | +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] |
| 20 | +pub struct FmtAstVec<T>(pub(crate) T); |
| 21 | + |
| 22 | +impl Display for Fmt<NodeKey<'_>> { |
| 23 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 24 | + if let Some(node) = self.0.get_as::<Statement>() { |
| 25 | + return Display::fmt(&FmtAst(node), f); |
| 26 | + } |
| 27 | + if let Some(node) = self.0.get_as::<Query>() { |
| 28 | + return Display::fmt(&FmtAst(node), f); |
| 29 | + } |
| 30 | + if let Some(node) = self.0.get_as::<Insert>() { |
| 31 | + return Display::fmt(&FmtAst(node), f); |
| 32 | + } |
| 33 | + if let Some(node) = self.0.get_as::<Delete>() { |
| 34 | + return Display::fmt(&FmtAst(node), f); |
| 35 | + } |
| 36 | + if let Some(node) = self.0.get_as::<Expr>() { |
| 37 | + return Display::fmt(&FmtAst(node), f); |
| 38 | + } |
| 39 | + if let Some(node) = self.0.get_as::<SetExpr>() { |
| 40 | + return Display::fmt(&FmtAst(node), f); |
| 41 | + } |
| 42 | + if let Some(node) = self.0.get_as::<Select>() { |
| 43 | + return Display::fmt(&FmtAst(node), f); |
| 44 | + } |
| 45 | + if let Some(node) = self.0.get_as::<SelectItem>() { |
| 46 | + return Display::fmt(&FmtAst(node), f); |
| 47 | + } |
| 48 | + if let Some(node) = self.0.get_as::<Vec<SelectItem>>() { |
| 49 | + return Display::fmt(&FmtAstVec(node), f); |
| 50 | + } |
| 51 | + if let Some(node) = self.0.get_as::<Function>() { |
| 52 | + return Display::fmt(&FmtAst(node), f); |
| 53 | + } |
| 54 | + if let Some(node) = self.0.get_as::<Values>() { |
| 55 | + return Display::fmt(&FmtAst(node), f); |
| 56 | + } |
| 57 | + if let Some(node) = self.0.get_as::<Value>() { |
| 58 | + return Display::fmt(&FmtAst(node), f); |
| 59 | + } |
| 60 | + |
| 61 | + f.write_str("!! CANNOT RENDER SQL NODE !!!")?; |
| 62 | + |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Display for Fmt<&HashMap<NodeKey<'_>, Type>> { |
| 68 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 69 | + let mut out: Vec<String> = Vec::new(); |
| 70 | + out.push("{ ".into()); |
| 71 | + for (k, v) in self.0.iter() { |
| 72 | + out.push(format!("{}: {}", Fmt(*k), v)); |
| 73 | + } |
| 74 | + out.push(" }".into()); |
| 75 | + f.write_str(&out.join(", ")) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<T: Display> Display for FmtAstVec<&Vec<T>> { |
| 80 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 81 | + f.write_str("![")?; |
| 82 | + let children = self |
| 83 | + .0 |
| 84 | + .iter() |
| 85 | + .map(|n| n.to_string()) |
| 86 | + .collect::<Vec<_>>() |
| 87 | + .join(", "); |
| 88 | + f.write_str(&children)?; |
| 89 | + f.write_str("]!") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<T: Display> Display for FmtAst<&T> { |
| 94 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 95 | + T::fmt(self.0, f) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl Display for Fmt<&Vec<(Param, crate::Value)>> { |
| 100 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 101 | + let formatted = self |
| 102 | + .0 |
| 103 | + .iter() |
| 104 | + .map(|(p, v)| format!("{}: {}", p, v)) |
| 105 | + .collect::<Vec<_>>() |
| 106 | + .join(", "); |
| 107 | + f.write_str(&formatted) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl Display for Fmt<&Vec<(EqlValue, &sqltk_parser::ast::Value)>> { |
| 112 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 113 | + let formatted = self |
| 114 | + .0 |
| 115 | + .iter() |
| 116 | + .map(|(e, n)| format!("{}: {}", n, e)) |
| 117 | + .collect::<Vec<_>>() |
| 118 | + .join(", "); |
| 119 | + f.write_str(&formatted) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl<T: Display> Display for Fmt<Option<T>> { |
| 124 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 125 | + match &self.0 { |
| 126 | + Some(t) => t.fmt(f), |
| 127 | + None => Display::fmt("<not initialised>", f), |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments