Skip to content

Commit 95de282

Browse files
committed
fix: try to fix ci
1 parent 77724b3 commit 95de282

File tree

5 files changed

+112
-102
lines changed

5 files changed

+112
-102
lines changed

.github/workflows/ci.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
runs-on: ${{ matrix.os }}
2424
steps:
2525
- uses: actions/checkout@v4
26+
with:
27+
path: typeid
2628
- name: Install cargo-pgrx
2729
run: |
2830
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')
@@ -34,6 +36,8 @@ jobs:
3436
runs-on: ubuntu-latest
3537
steps:
3638
- uses: actions/checkout@v4
39+
with:
40+
path: typeid
3741
- name: Install PostgreSQL headers
3842
run: |
3943
sudo apt-get update
@@ -43,9 +47,8 @@ jobs:
4347
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')
4448
cargo install --locked --version=$PGRX_VERSION cargo-pgrx --debug --force
4549
cargo pgrx init --pg14 $(which pg_config)
46-
- name: Install PL/PRQL
50+
- name: Install TypeID/PRQL
4751
run: |
48-
cd plprql
4952
cargo pgrx install --no-default-features --release --sudo
5053
- name: Start PostgreSQL
5154
run: |
@@ -63,6 +66,8 @@ jobs:
6366
steps:
6467
- name: Checkout code
6568
uses: actions/checkout@v4
69+
with:
70+
path: typeid
6671
- name: Run rustfmt
6772
run: cargo fmt -- --check
6873
rust-clippy:
@@ -73,6 +78,9 @@ jobs:
7378
steps:
7479
- name: Checkout code
7580
uses: actions/checkout@v4
81+
82+
with:
83+
path: typeid
7684
- name: Install cargo-pgrx
7785
run: |
7886
PGRX_VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[]|select(.name=="pgrx")|.version')

src/base32.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,57 @@ pub enum Error {
99
}
1010

1111
fn decode_base32_to_u128(id: &str) -> Result<u128, Error> {
12-
let mut id: [u8; 26] = id.as_bytes().try_into().map_err(|_| Error::InvalidData)?;
13-
let mut max = 0;
14-
for b in &mut id {
15-
*b = CROCKFORD_INV[*b as usize];
16-
max |= *b;
17-
}
18-
if max > 32 || id[0] > 7 {
19-
return Err(Error::InvalidData);
20-
}
12+
let mut id: [u8; 26] = id.as_bytes().try_into().map_err(|_| Error::InvalidData)?;
13+
let mut max = 0;
14+
for b in &mut id {
15+
*b = CROCKFORD_INV[*b as usize];
16+
max |= *b;
17+
}
18+
if max > 32 || id[0] > 7 {
19+
return Err(Error::InvalidData);
20+
}
2121

22-
let mut out = 0u128;
23-
for b in id {
24-
out <<= 5;
25-
out |= b as u128;
26-
}
22+
let mut out = 0u128;
23+
for b in id {
24+
out <<= 5;
25+
out |= b as u128;
26+
}
2727

28-
Ok(out)
28+
Ok(out)
2929
}
3030

3131
fn encode_u128_to_base32(data: u128) -> String {
32-
let mut buf = [0u8; 26];
33-
let mut data = data;
34-
for i in (0..26).rev() {
35-
buf[i] = CROCKFORD[(data & 0x1f) as usize];
36-
debug_assert!(buf[i].is_ascii());
37-
data >>= 5;
38-
}
39-
unsafe { String::from_utf8_unchecked(buf.to_vec()) }
32+
let mut buf = [0u8; 26];
33+
let mut data = data;
34+
for i in (0..26).rev() {
35+
buf[i] = CROCKFORD[(data & 0x1f) as usize];
36+
debug_assert!(buf[i].is_ascii());
37+
data >>= 5;
38+
}
39+
unsafe { String::from_utf8_unchecked(buf.to_vec()) }
4040
}
4141

4242
const CROCKFORD: &[u8; 32] = b"0123456789abcdefghjkmnpqrstvwxyz";
4343
const CROCKFORD_INV: &[u8; 256] = &{
44-
let mut output = [255; 256];
44+
let mut output = [255; 256];
4545

46-
let mut i = 0;
47-
while i < 32 {
48-
output[CROCKFORD[i as usize] as usize] = i;
49-
i += 1;
50-
}
46+
let mut i = 0;
47+
while i < 32 {
48+
output[CROCKFORD[i as usize] as usize] = i;
49+
i += 1;
50+
}
5151

52-
output
52+
output
5353
};
5454

55-
5655
pub fn encode_base32_uuid(uuid: &Uuid) -> String {
57-
encode_u128_to_base32(uuid.as_u128())
56+
encode_u128_to_base32(uuid.as_u128())
5857
}
5958

6059
pub fn decode_base32_uuid(encoded: &str) -> Result<Uuid, Error> {
61-
decode_base32_to_u128(encoded).map(|result: u128| Uuid::from_u128(result))
60+
decode_base32_to_u128(encoded).map(|result: u128| Uuid::from_u128(result))
6261
}
6362

64-
6563
#[cfg(test)]
6664
mod tests {
6765
use uuid::Uuid;
@@ -76,4 +74,4 @@ mod tests {
7674
let decoded = decode_base32_uuid(&encoded).unwrap();
7775
assert_eq!(uuid, decoded);
7876
}
79-
}
77+
}

src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use pgrx::prelude::*;
1010

1111
pgrx::pg_module_magic!();
1212

13-
1413
#[pg_extern]
1514
fn typeid_generate(prefix: &str) -> TypeID {
1615
TypeID::new(TypeIDPrefix::new(prefix).unwrap(), Uuid::now_v7())
@@ -23,7 +22,10 @@ fn typeid_to_uuid(typeid: TypeID) -> pgrx::Uuid {
2322

2423
#[pg_extern]
2524
fn uuid_to_typeid(prefix: &str, uuid: pgrx::Uuid) -> TypeID {
26-
TypeID::new(TypeIDPrefix::new(prefix).unwrap(), Uuid::from_slice(uuid.as_bytes()).unwrap())
25+
TypeID::new(
26+
TypeIDPrefix::new(prefix).unwrap(),
27+
Uuid::from_slice(uuid.as_bytes()).unwrap(),
28+
)
2729
}
2830

2931
#[pg_extern]
@@ -64,8 +66,8 @@ fn typeid_ne(a: TypeID, b: TypeID) -> bool {
6466
typeid_cmp(a, b) != 0
6567
}
6668

67-
extension_sql!{
68-
r#"
69+
extension_sql! {
70+
r#"
6971
CREATE OPERATOR < (
7072
LEFTARG = typeid,
7173
RIGHTARG = typeid,
@@ -114,14 +116,14 @@ extension_sql!{
114116
OPERATOR 5 > (typeid, typeid),
115117
FUNCTION 1 typeid_cmp(typeid, typeid);
116118
"#,
117-
name = "create_typeid_operator_class",
118-
finalize,
119-
}
119+
name = "create_typeid_operator_class",
120+
finalize,
121+
}
120122

121123
/// Generate a UUID v7, producing a Postgres uuid object
122124
#[pg_extern]
123125
fn uuid_generate_v7() -> pgrx::Uuid {
124-
pgrx::Uuid::from_bytes(*Uuid::now_v7().as_bytes())
126+
pgrx::Uuid::from_bytes(*Uuid::now_v7().as_bytes())
125127
}
126128

127129
#[cfg(any(test, feature = "pg_test"))]
@@ -136,7 +138,6 @@ mod tests {
136138
assert_eq!(typeid.type_prefix(), "test");
137139
}
138140

139-
140141
#[pg_test]
141142
fn test_uuid() {
142143
let uuid: pgrx::Uuid = crate::uuid_generate_v7();
@@ -146,7 +147,6 @@ mod tests {
146147

147148
assert_eq!(converted.get_version_num(), 7);
148149
}
149-
150150
}
151151

152152
/// This module is required by `cargo pgrx test` invocations.

src/typeid.rs

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use core::fmt;
22
use std::borrow::Cow;
33

44
use pgrx::prelude::*;
5+
use serde::{Deserialize, Serialize};
56
use uuid::Uuid;
6-
use serde::{Serialize, Deserialize};
77

88
use crate::base32::{decode_base32_uuid, encode_base32_uuid};
99

@@ -36,7 +36,7 @@ impl TypeIDPrefix {
3636
}
3737

3838
pub fn try_unsafe(tag: &str) -> Self {
39-
Self(tag.to_string())
39+
Self(tag.to_string())
4040
}
4141

4242
fn try_from_type_prefix(tag: &str) -> Result<Self, Cow<'static, str>> {
@@ -77,58 +77,62 @@ impl TypeIDPrefix {
7777
pub struct TypeID(TypeIDPrefix, Uuid);
7878

7979
impl TypeID {
80-
pub fn new(type_prefix: TypeIDPrefix, uuid: Uuid) -> Self {
81-
TypeID(type_prefix, uuid)
82-
}
83-
84-
pub fn from_string(id: &str) -> Result<Self, Error> {
85-
// Split the input string once at the first occurrence of '_'
86-
let (tag, id) = match id.rsplit_once('_') {
87-
Some(("", _)) => return Err(Error::InvalidType),
88-
Some((tag, id)) => (tag, id),
89-
None => ("", id),
90-
};
91-
92-
// Decode the UUID part and handle potential errors
93-
let uuid = decode_base32_uuid(id).map_err(|_| Error::InvalidData)?;
94-
95-
let prefix = TypeIDPrefix::new(tag)?;
96-
97-
// Create and return the TypeID
98-
Ok(TypeID(prefix, uuid))
99-
}
100-
101-
pub fn type_prefix(&self) -> &str {
102-
&self.0.to_type_prefix()
103-
}
104-
105-
pub fn uuid(&self) -> &Uuid {
106-
&self.1
107-
}
80+
pub fn new(type_prefix: TypeIDPrefix, uuid: Uuid) -> Self {
81+
TypeID(type_prefix, uuid)
82+
}
83+
84+
pub fn from_string(id: &str) -> Result<Self, Error> {
85+
// Split the input string once at the first occurrence of '_'
86+
let (tag, id) = match id.rsplit_once('_') {
87+
Some(("", _)) => return Err(Error::InvalidType),
88+
Some((tag, id)) => (tag, id),
89+
None => ("", id),
90+
};
91+
92+
// Decode the UUID part and handle potential errors
93+
let uuid = decode_base32_uuid(id).map_err(|_| Error::InvalidData)?;
94+
95+
let prefix = TypeIDPrefix::new(tag)?;
96+
97+
// Create and return the TypeID
98+
Ok(TypeID(prefix, uuid))
99+
}
100+
101+
pub fn type_prefix(&self) -> &str {
102+
self.0.to_type_prefix()
103+
}
104+
105+
pub fn uuid(&self) -> &Uuid {
106+
&self.1
107+
}
108108
}
109109

110110
impl fmt::Display for TypeID {
111-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112-
if self.type_prefix().is_empty() {
113-
write!(f, "{}", encode_base32_uuid(self.uuid()))
114-
} else {
115-
write!(f, "{}_{}", self.type_prefix(), encode_base32_uuid(self.uuid()))
116-
}
117-
}
111+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112+
if self.type_prefix().is_empty() {
113+
write!(f, "{}", encode_base32_uuid(self.uuid()))
114+
} else {
115+
write!(
116+
f,
117+
"{}_{}",
118+
self.type_prefix(),
119+
encode_base32_uuid(self.uuid())
120+
)
121+
}
122+
}
118123
}
119124

120-
121125
impl InOutFuncs for TypeID {
122-
fn input(input: &core::ffi::CStr) -> TypeID {
123-
// Convert the input to a str and handle potential UTF-8 errors
124-
let str_input = input.to_str().expect("text input is not valid UTF8");
125-
126-
TypeID::from_string(str_input).unwrap()
127-
}
128-
129-
fn output(&self, buffer: &mut pgrx::StringInfo) {
130-
// Use write! macro to directly push the string representation into the buffer
131-
use std::fmt::Write;
132-
write!(buffer, "{}", self).expect("Failed to write to buffer");
133-
}
134-
}
126+
fn input(input: &core::ffi::CStr) -> TypeID {
127+
// Convert the input to a str and handle potential UTF-8 errors
128+
let str_input = input.to_str().expect("text input is not valid UTF8");
129+
130+
TypeID::from_string(str_input).unwrap()
131+
}
132+
133+
fn output(&self, buffer: &mut pgrx::StringInfo) {
134+
// Use write! macro to directly push the string representation into the buffer
135+
use std::fmt::Write;
136+
write!(buffer, "{}", self).expect("Failed to write to buffer");
137+
}
138+
}

tests/spec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use libtest_mimic::{Arguments, Trial};
22
use serde::Deserialize;
3-
use typeid::typeid::{TypeID};
3+
use typeid::typeid::TypeID;
44
use uuid::Uuid;
55

66
#[derive(Deserialize)]
@@ -27,8 +27,8 @@ fn main() {
2727
for test in valid {
2828
tests.push(Trial::test(format!("valid::{}", test.name), move || {
2929
let id = match TypeID::from_string(&test.typeid) {
30-
Ok(id) => id,
31-
Err(e) => return Err(e.to_string().into()),
30+
Ok(id) => id,
31+
Err(e) => return Err(e.to_string().into()),
3232
};
3333

3434
if id.uuid().ne(&test.uuid) {
@@ -60,4 +60,4 @@ fn main() {
6060

6161
let args = Arguments::from_args();
6262
libtest_mimic::run(&args, tests).exit_if_failed();
63-
}
63+
}

0 commit comments

Comments
 (0)