postgres_syntax/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use litrs::StringLit;
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro]
pub fn sql(input: TokenStream) -> TokenStream {
  let input = input.into_iter().collect::<Vec<_>>();
  if input.len() != 1 {
    let msg = format!(
      "expected exactly one string literal as input, got {} tokens",
      input.len()
    );
    return quote! { compile_error!(#msg) }.into();
  }
  let string_lit = match StringLit::try_from(&input[0]) {
    Ok(lit) => lit,
    Err(e) => return e.to_compile_error(),
  };

  if let Err(e) = pg_query::parse(string_lit.value()) {
    let msg = format!("failed to parse SQL query: {}", e);
    return quote! { compile_error!(#msg) }.into();
  }

  input.into_iter().collect()
}