postgres_syntax/
lib.rs

1use litrs::StringLit;
2use proc_macro::TokenStream;
3use quote::quote;
4
5#[proc_macro]
6pub fn sql(input: TokenStream) -> TokenStream {
7  let input = input.into_iter().collect::<Vec<_>>();
8  if input.len() != 1 {
9    let msg = format!(
10      "expected exactly one string literal as input, got {} tokens",
11      input.len()
12    );
13    return quote! { compile_error!(#msg) }.into();
14  }
15  let string_lit = match StringLit::try_from(&input[0]) {
16    Ok(lit) => lit,
17    Err(e) => return e.to_compile_error(),
18  };
19
20  if let Err(e) = pg_query::parse(string_lit.value()) {
21    let msg = format!("failed to parse SQL query: {}", e);
22    return quote! { compile_error!(#msg) }.into();
23  }
24
25  input.into_iter().collect()
26}
27