From 86e58cf493e239522f460cdfbdbdabbdeda2345d Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Thu, 13 Aug 2026 09:41:55 -0700 Subject: [PATCH 1/2] feat: parser version --- build.rs | 2 ++ src/lib.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 1675b26..6c52e15 100644 --- a/build.rs +++ b/build.rs @@ -135,6 +135,8 @@ fn main() { .allowlist_item("newNode") .allowlist_item("pg_query_normalize") .allowlist_item("pg_query_free_normalize_result") + .allowlist_var("PG_VERSION") + .allowlist_var("PG_VERSION_NUM") .wrap_static_fns(true) .wrap_static_fns_path(out_dir.join("wrap_static_fns")); for struct_name in &node_structs { diff --git a/src/lib.rs b/src/lib.rs index e32f2a0..7605796 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![cfg_attr(feature = "field_offset_assertions", feature(offset_of_enum))] -use std::{fmt, ops}; +use std::{ffi::CStr, fmt, ops}; pub mod const_val; mod deparse; @@ -24,6 +24,17 @@ pub use crate::error::{Error, Result}; pub use crate::node_enum::{Node, NodeMut}; pub use crate::owned::Owned; +/// PostgreSQL version number whose parser sources are used by this crate. +pub const POSTGRES_VERSION_NUM: u32 = raw::PG_VERSION_NUM; + +/// Returns the PostgreSQL version whose parser sources are used by this crate. +pub fn postgres_version() -> &'static str { + CStr::from_bytes_with_nul(raw::PG_VERSION) + .expect("bindgen generated PG_VERSION without a trailing NUL") + .to_str() + .expect("bindgen generated a non-UTF-8 PG_VERSION") +} + pub(crate) use node_ptr::{ AsNodePtr, AsNodeRef, ConstructableNode, FromNodeMut, FromNodePtr, List, }; @@ -63,3 +74,20 @@ impl fmt::Debug for ParseResult { .finish_non_exhaustive() } } + +#[cfg(test)] +mod tests { + use super::{POSTGRES_VERSION_NUM, postgres_version}; + + #[test] + fn postgres_version_constants_are_consistent() { + let mut components = postgres_version() + .split('.') + .map(|component| component.parse::().unwrap()); + let major = components.next().unwrap(); + let minor = components.next().unwrap(); + + assert_eq!(POSTGRES_VERSION_NUM, major * 10_000 + minor); + assert!(components.next().is_none()); + } +} From 10459076223befc3facdf199368730f5111be503 Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Thu, 13 Aug 2026 12:13:50 -0700 Subject: [PATCH 2/2] its not bindgens fault --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7605796..0ed0cdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,9 +30,9 @@ pub const POSTGRES_VERSION_NUM: u32 = raw::PG_VERSION_NUM; /// Returns the PostgreSQL version whose parser sources are used by this crate. pub fn postgres_version() -> &'static str { CStr::from_bytes_with_nul(raw::PG_VERSION) - .expect("bindgen generated PG_VERSION without a trailing NUL") + .expect("generated PG_VERSION without a trailing NUL") .to_str() - .expect("bindgen generated a non-UTF-8 PG_VERSION") + .expect("generated a non-UTF-8 PG_VERSION") } pub(crate) use node_ptr::{