Skip to content

Commit

Permalink
Merge pull request #4 from parrottq/modernize
Browse files Browse the repository at this point in the history
Updated dependencies
  • Loading branch information
quodlibetor authored Jun 19, 2024
2 parents 62eb2a1 + f3bed15 commit 1156984
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Build
run: cargo build --features diesel-uuid
run: cargo build --features diesel-uuid,serde

- name: Test
run: cargo nextest run --features diesel-uuid
run: cargo nextest run --features diesel-uuid,serde
env:
PG_DATABASE_URL: postgres://postgres:postgres@localhost/postgres
25 changes: 13 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@
authors = ["Brandon W Maister <[email protected]>"]
name = "uuid-b64"
description = "Base64 encoding for UUIDs"
version = "0.1.1"
version = "0.2.0"
license = "Apache-2.0/MIT"
readme = "README.md"
categories = ["encoding", "value-formatting"]
repository = "https://github.com/quodlibetor/uuid-b64"
edition = "2021"

[badges]
travis-ci = { repository = "quodlibetor/uuid-b64", branch = "master" }

[dependencies]
base64 = "0.8"
diesel-derive-newtype = { version = "0.1", optional = true }
diesel = { version = "1.0", features = ["postgres", "uuid"], optional = true }
error-chain = "0.11.0"
inlinable_string = { version = "0.1.0", features = ["serde"] }
lazy_static = "0.2.9"
base64 = "0.22.0"
diesel-derive-newtype = { version = "2.1.0", optional = true }
diesel = { version = "2.2.0", features = ["postgres", "uuid"], optional = true }
error-chain = "0.12.0"
inlinable_string = { version = "0.1.0", default-features = false }
serde = { version = "1.0.15", optional = true }
# Diesel 1.1.1 doesn't support 0.6.1 yet
uuid = { version = ">=0.5.1,<0.7.0", features = ["v4", "use_std"] }
uuid = { version = "1.8.0", features = ["v4"] }

[features]
default-features = ["serde"]
diesel-uuid = ["diesel-derive-newtype", "diesel"]
default = []
serde = ["dep:serde", "inlinable_string/serde"]
diesel-uuid = ["dep:diesel-derive-newtype", "dep:diesel"]
diesel = ["diesel-uuid"]

[dev-dependencies]
serde_json = "1.0"
serde_derive = "1.0"
diesel = { version = "1.0", features = ["postgres", "uuid"] }
diesel = { version = "2.2.0", features = ["postgres", "uuid"] }
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use error_chain::error_chain;

error_chain! {
errors {
ParseError(t: String) {
Expand Down
78 changes: 26 additions & 52 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,55 +88,29 @@
//! * `diesel-uuid` enables integration with Diesel's UUID support, this is
//! only tested on postgres, PRs welcome for other DBs.
extern crate base64;
#[cfg(feature = "diesel")]
#[macro_use]
extern crate diesel_derive_newtype;
#[macro_use]
extern crate error_chain;
extern crate inlinable_string;
#[macro_use]
extern crate lazy_static;
extern crate uuid;

#[cfg(all(test, feature = "diesel-uuid"))]
#[macro_use]
extern crate diesel;
#[cfg(all(test, feature = "diesel-uuid"))]
#[cfg(all(test, feature = "serde"))]
#[macro_use]
extern crate serde_derive;
#[cfg(all(test, feature = "serde"))]
#[macro_use]
extern crate serde_json;

use std::convert::From;
use std::str::FromStr;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::str::FromStr;

use uuid::Uuid;
use base64::{CharacterSet, Config, LineWrap};
use base64::display::Base64Display;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use inlinable_string::inline_string::InlineString;
use uuid::Uuid;

use errors::{ErrorKind, ResultExt};
#[cfg(feature = "diesel-uuid")]
#[macro_use]
extern crate diesel_derive_newtype;

use crate::errors::{ErrorKind, ResultExt};

mod errors;
#[cfg(feature = "serde")]
mod serde_impl;

lazy_static! {
static ref B64_CONFIG: Config = Config::new(
CharacterSet::UrlSafe,
false, // pad?
false, // trim whitespace?
LineWrap::NoWrap,
);
}

/// It's a Uuid that displays as Base 64
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "diesel", derive(DieselNewType))]
#[cfg_attr(feature = "diesel-uuid", derive(DieselNewType))]
pub struct UuidB64(uuid::Uuid);

impl UuidB64 {
Expand Down Expand Up @@ -173,7 +147,7 @@ impl UuidB64 {
let mut buf = InlineString::from("0000000000000000000000"); // not actually zeroes
unsafe {
let raw_buf = buf.as_mut_slice();
base64::encode_config_slice(self.0.as_bytes(), *B64_CONFIG, &mut raw_buf[0..22]);
URL_SAFE_NO_PAD.encode_slice(self.0.as_bytes(), &mut raw_buf[0..22]).unwrap();
}
buf
}
Expand All @@ -194,7 +168,7 @@ impl UuidB64 {
/// # }
/// ```
pub fn to_buf(&self, buffer: &mut String) {
base64::encode_config_buf(self.0.as_bytes(), *B64_CONFIG, buffer);
URL_SAFE_NO_PAD.encode_string(self.0.as_bytes(), buffer);
}
}

Expand All @@ -209,9 +183,10 @@ impl FromStr for UuidB64 {
type Err = errors::ErrorKind;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes =
base64::decode_config(s, *B64_CONFIG).chain_err(|| ErrorKind::ParseError(s.into()))?;
let id = Uuid::from_bytes(&bytes).chain_err(|| ErrorKind::ParseError(s.into()))?;
let mut output = [0; 16];
URL_SAFE_NO_PAD.decode_slice(s, &mut output)
.chain_err(|| ErrorKind::ParseError(s.into()))?;
let id = Uuid::from_bytes(output);
Ok(UuidB64(id))
}
}
Expand Down Expand Up @@ -260,8 +235,7 @@ impl Display for UuidB64 {
/// # }
/// ```
fn fmt(&self, f: &mut Formatter) -> FmtResult {
// can only hit this error if we use an invalid line length
let wrapper = Base64Display::with_config(self.0.as_bytes(), *B64_CONFIG).unwrap();
let wrapper = Base64Display::new(self.0.as_bytes(), &URL_SAFE_NO_PAD);
write!(f, "{}", wrapper)
}
}
Expand Down Expand Up @@ -306,16 +280,16 @@ mod tests {
#[cfg(all(test, feature = "diesel-uuid"))]
mod diesel_tests {
use diesel;
use diesel::prelude::*;
use diesel::dsl::sql;
use diesel::pg::PgConnection;
use diesel::prelude::*;

use std::env;

use super::UuidB64;

#[derive(Debug, Clone, PartialEq, Identifiable, Insertable, Queryable)]
#[table_name = "my_entities"]
#[diesel(table_name = my_entities)]
pub struct MyEntity {
id: UuidB64,
val: i32,
Expand All @@ -331,23 +305,23 @@ mod diesel_tests {
#[cfg(test)]
fn setup() -> PgConnection {
let db_url = env::var("PG_DATABASE_URL").expect("PG_DB_URL must be in the environment");
let conn = PgConnection::establish(&db_url).unwrap();
let mut conn = PgConnection::establish(&db_url).unwrap();
#[allow(deprecated)] // not present in diesel 1.0
let setup = sql::<diesel::types::Bool>(
let setup = sql::<diesel::sql_types::Bool>(
"CREATE TABLE IF NOT EXISTS my_entities (
id UUID PRIMARY KEY,
val Int
)",
);
setup.execute(&conn).expect("Can't create table");
setup.execute(&mut conn).expect("Can't create table");
conn
}

#[test]
fn does_roundtrip() {
use self::my_entities::dsl::*;

let conn = setup();
let mut conn = setup();

let obj = MyEntity {
id: UuidB64::new(),
Expand All @@ -356,14 +330,14 @@ mod diesel_tests {

diesel::insert_into(my_entities)
.values(&obj)
.execute(&conn)
.execute(&mut conn)
.expect("Couldn't insert struct into my_entities");

let found: Vec<MyEntity> = my_entities.load(&conn).unwrap();
let found: Vec<MyEntity> = my_entities.load(&mut conn).unwrap();
assert_eq!(found[0], obj);

diesel::delete(my_entities.filter(id.eq(&obj.id)))
.execute(&conn)
.execute(&mut conn)
.expect("Couldn't delete existing object");
}
}
8 changes: 5 additions & 3 deletions src/serde_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ extern crate serde;

use std::fmt::{Formatter, Result as FmtResult};

use self::serde::ser::{Serialize, Serializer};
use self::serde::de::{self, Deserialize, Deserializer, Visitor};
use self::serde::ser::{Serialize, Serializer};

use super::UuidB64;

Expand Down Expand Up @@ -44,13 +44,15 @@ impl<'de> Visitor<'de> for UuidB64Visitor {

#[cfg(test)]
mod tests {
use serde_derive::Deserialize;
use serde_json::json;
use uuid::Uuid;

use UuidB64;
use crate::UuidB64;

#[test]
fn ser_de() {
let uuid = Uuid::from_fields(0xff, 2, 3, &[1, 2, 3, 4, 5, 6, 7, 8]).unwrap();
let uuid = Uuid::from_fields(0xff, 2, 3, &[1, 2, 3, 4, 5, 6, 7, 8]);
let my_id = UuidB64::from(uuid);

let json = json!({ "myid": my_id }).to_string();
Expand Down

0 comments on commit 1156984

Please sign in to comment.