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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::{collections::HashSet, fs::File, io::Write, path::Path};
use syn::{parse_quote, Ident, Item};
pub struct PgxPgSysStub {
stub_file: syn::File,
}
const SYMBOL_SKIP_LIST: [&str; 2] = ["_fini", "_init"];
impl PgxPgSysStub {
#[tracing::instrument(level = "error", skip_all, fields(symbols = %symbols.len()))]
pub fn from_symbols(symbols: &HashSet<String>) -> eyre::Result<Self> {
let mut items = Vec::with_capacity(symbols.len());
for symbol in symbols
.iter()
.filter(|v| !SYMBOL_SKIP_LIST.contains(&v.as_ref()))
{
match stub_for_symbol(symbol) {
Ok(stub) => items.push(stub),
Err(_e) => tracing::trace!(%symbol, "Skipping, not a valid Rust ident"),
}
}
let stub_file = syn::File {
shebang: None,
attrs: Default::default(),
items,
};
Ok(Self { stub_file })
}
#[tracing::instrument(level = "error", skip_all, fields(pgx_pg_sys_stub = %pgx_pg_sys_stub.as_ref().display()))]
pub fn write_to_file(&self, pgx_pg_sys_stub: impl AsRef<Path>) -> eyre::Result<()> {
let pgx_pg_sys_stub = pgx_pg_sys_stub.as_ref();
if let Some(parent) = pgx_pg_sys_stub.parent() {
std::fs::create_dir_all(parent)?;
}
let mut output_file = File::create(pgx_pg_sys_stub)?;
let content = prettyplease::unparse(&self.stub_file);
output_file.write_all(content.as_bytes())?;
Ok(())
}
}
#[tracing::instrument]
fn stub_for_symbol(name: &str) -> eyre::Result<Item> {
let ident = syn::parse_str::<Ident>(name)?;
let item_fn: syn::ItemFn = parse_quote! {
#[allow(dead_code)]
#[allow(non_snake_case)]
#[no_mangle]
extern "C" fn #ident() {
unimplemented!(concat!(stringify!(#name), " is stubbed and cannot be used right now."));
}
};
Ok(Item::Fn(item_fn))
}