Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 29 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be made const

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently we are not allowed to call expect inside const functions. This is because expect isn't const.

CStr::from_bytes_with_nul(raw::PG_VERSION)
.expect("generated PG_VERSION without a trailing NUL")
.to_str()
.expect("generated a non-UTF-8 PG_VERSION")
}

pub(crate) use node_ptr::{
AsNodePtr, AsNodeRef, ConstructableNode, FromNodeMut, FromNodePtr, List,
};
Expand Down Expand Up @@ -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::<u32>().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());
}
}
Loading