Skip to content

Commit

Permalink
feat(cuid): add timestamp extraction fn for cuid
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 from CUID
values (even though they are deprecated in favor of CUID2).

Signed-off-by: vados <[email protected]>
  • Loading branch information
t3hmrman committed Oct 30, 2023
1 parent 1ed7af1 commit 7ff17e4
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 23 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.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
name = "pg_idkit"
version = "0.1.0"
edition = "2021"
authors = [
"Victor Adossi <[email protected]>"
]
authors = ["Victor Adossi <[email protected]>"]
rust-version = "1.73.0"
description = """
A Postgres extension for generating UUIDs
Expand All @@ -18,17 +16,19 @@ maintenance = { status = "actively-maintained" }

[features]
default = ["pg15"]
pg11 = [ "pgrx/pg11", "pgrx-tests/pg11" ]
pg12 = [ "pgrx/pg12", "pgrx-tests/pg12" ]
pg13 = [ "pgrx/pg13", "pgrx-tests/pg13" ]
pg14 = [ "pgrx/pg14", "pgrx-tests/pg14" ]
pg15 = [ "pgrx/pg15", "pgrx-tests/pg15" ]
pg11 = ["pgrx/pg11", "pgrx-tests/pg11"]
pg12 = ["pgrx/pg12", "pgrx-tests/pg12"]
pg13 = ["pgrx/pg13", "pgrx-tests/pg13"]
pg14 = ["pgrx/pg14", "pgrx-tests/pg14"]
pg15 = ["pgrx/pg15", "pgrx-tests/pg15"]
pg_test = []

[dependencies]
cuid = "1.3.2"
cuid2 = "0.1.2"
getrandom = "0.2.10"
base36 = "0.0.1"
chrono = { version = "0.4.31", default-features = false, features = ["clock"] }
ksuid = "0.2.0"
nanoid = "0.4.0"
pgrx = "=0.11.0"
Expand All @@ -38,6 +38,7 @@ timeflake-rs = "0.3.0"
ulid = "1.1.0"
uuid7 = "0.7.2"
xid = "1.0.3"
miette = { version = "5.10.0", features = ["fancy"] }

[dev-dependencies]
pgrx-tests = "=0.11.0"
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 7ff17e4

Please sign in to comment.