Skip to content

Commit

Permalink
feat: add functions for extracting timestamps
Browse files Browse the repository at this point in the history
Some ID generation functions have timestamps embedded in them, and
being able to extract the timestamp is a useful feature. Timestamps
extracted from IDs can be used in logic checks, as
virtual/generated columns and have many other use cases.

This commit adds functions for extracting timestamps to the IDs that
support them (i.e. IDs that have a timestamp embedded somehow).

Signed-off-by: vados <[email protected]>
  • Loading branch information
t3hmrman committed Oct 29, 2023
1 parent 42dbbf4 commit 40af1ee
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 15 deletions.
166 changes: 159 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pg15 = [ "pgrx/pg15", "pgrx-tests/pg15" ]
pg_test = []

[dependencies]
base36 = "0.0.1"
chrono = { version = "0.4.31", default-features = false, features = [ "clock" ] }
cuid = "1.3.1"
cuid2 = "0.1.0"
getrandom = "0.2.8"
Expand All @@ -34,6 +36,7 @@ uuid7 = "0.6.4"
xid = "1.0.3"

[dev-dependencies]
miette = { version = "5.10.0", features = ["fancy"] }
pgrx-tests = "=0.9.7"

[profile.dev]
Expand Down
28 changes: 28 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// A trait that encapsulates things that can be converted to some type
/// or to an error if conversion fails (ex. Result, Option, etc)
pub(crate) trait OrPgxError<T> {
/// Convert the given type to a T, possibly failing
/// and calling [`pgrx::error`], with a given prefix if an error is returned
fn or_pgrx_error(self, prefix: impl AsRef<str>) -> T;
}

impl<T, E> OrPgxError<T> for Result<T, E>
where
E: std::error::Error,
{
fn or_pgrx_error(self, prefix: impl AsRef<str>) -> T {
match self {
Ok(v) => v,
Err(e) => pgrx::error!("{}: {e}", prefix.as_ref()),
}
}
}

impl<T> OrPgxError<T> for Option<T> {
fn or_pgrx_error(self, prefix: impl AsRef<str>) -> T {
match self {
Some(v) => v,
None => pgrx::error!("{}", prefix.as_ref()),
}
}
}
Loading

0 comments on commit 40af1ee

Please sign in to comment.