pgdog_plugin_build/
lib.rs

1//! Build-time helpers for PgDog plugins.
2//!
3//! Include this package as a build dependency only.
4//!
5
6use std::{fs::File, io::Read};
7
8/// Extracts the `pg_query` crate version from `Cargo.toml`
9/// and sets it as an environment variable.
10///
11/// This should be used at build time only. It expects `Cargo.toml` to be present in the same
12/// folder as `build.rs`.
13///
14/// ### Note
15///
16/// You should have a strict version constraint on `pg_query`, for example:
17///
18/// ```toml
19/// pg_query = "6.1.0"
20/// ```
21///
22/// If the version in your plugin doesn't match what PgDog is using, your plugin won't be loaded.
23///
24pub fn pg_query_version() {
25    let mut contents = String::new();
26    if let Ok(mut file) = File::open("Cargo.toml") {
27        file.read_to_string(&mut contents).ok();
28    } else {
29        panic!("Cargo.toml not found");
30    }
31
32    let contents: Option<toml::Value> = toml::from_str(&contents).ok();
33    if let Some(contents) = contents {
34        if let Some(dependencies) = contents.get("dependencies") {
35            if let Some(pg_query) = dependencies.get("pg_query") {
36                if let Some(version) = pg_query.as_str() {
37                    println!("cargo:rustc-env=PGDOG_PGQUERY_VERSION={}", version);
38                }
39            }
40        }
41    }
42}