Skip to main content

mz_expr_derive_impl/
lib.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Implementations of proc macros to derive SQL function traits.
11//!
12//! This is separate from the actual proc macro to allow exporting the
13//! function defining the proc macro itself. Proc macro crates cannot
14//! export anything but proc macros.
15
16mod generate;
17mod modifiers;
18mod shape;
19mod signature;
20mod source;
21mod sqlfunc;
22
23pub use sqlfunc::sqlfunc;
24
25/// Expands `#[sqlfunc(attr)]` on `item` and returns the formatted expansion, or the
26/// formatted errors, along with the formatted input.
27#[cfg(any(feature = "test", test))]
28pub fn test_sqlfunc(
29    attr: proc_macro2::TokenStream,
30    item: proc_macro2::TokenStream,
31) -> (String, String) {
32    fn rust_fmt(input: &str) -> String {
33        let file = syn::parse_file(input).unwrap();
34        prettyplease::unparse(&file)
35    }
36
37    let input = rust_fmt(&format!("#[sqlfunc({attr})]\n{item}"));
38    let output = rust_fmt(
39        &sqlfunc(attr, item, false)
40            .unwrap_or_else(|err| err.write_errors())
41            .to_string(),
42    );
43    (output, input)
44}
45
46/// [`test_sqlfunc`] over source text, as the generated snapshot tests pass it.
47#[cfg(any(feature = "test", test))]
48pub fn test_sqlfunc_str(attr: &str, item: &str) -> (String, String) {
49    test_sqlfunc(attr.parse().unwrap(), item.parse().unwrap())
50}
51
52#[cfg(test)]
53mod test;