Skip to content

Commit 13d553b

Browse files
committed
jiff-diesel: add integration crate for Diesel
This is like #240, but adds a crate with wrapper types that implements Diesel traits. Unlike with SQLx, and unless I'm missing something, Diesel actually exposes enough of an API to implement datetime support for MySQL. So that's included here. Diesel does seem to use some internal privileged APIs for its own `chrono` and `time` integration that avoids unnecessary allocations when parsing datetimes from SQLite values. Again, unless I'm missing something, Jiff is forced to allocate into a `String` first. (But Jiff only really needs a `&[u8]`.) I found this experience, along with SQLx, to be absolutely mind-numbing. Just writing out the example code (which I also used for ad hoc testing) took an incredible amount of time. I spent _way_ more time playing fucking type tetris with both SQLx and Diesel than I did anything else _combined_. It's utterly ridiculous. This further solidifies my opinion that when you publish crates with an obscene amount of inter-connected traits, the resulting API becomes very difficult to use. I'm happy to iterate on the implementation and APIs of this crate (and `jiff-sqlx`) after an initial release. But I very much appreciate reviews from Diesel and SQLx experts. I'm going to say that this closes #50 since this I think this, along with `jiff-sqlx` and `jiff-icu`, gives us a solid foundation to build upon. We can track more specific integrations in new issues. Closes #50
1 parent 0dff6c6 commit 13d553b

File tree

26 files changed

+1335
-13
lines changed

26 files changed

+1335
-13
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ jobs:
141141
run: cargo build --manifest-path ./examples/sqlx-postgres/Cargo.toml
142142
- name: Run sqlx-sqlite example
143143
run: cargo run --manifest-path ./examples/sqlx-sqlite/Cargo.toml
144+
- name: Build diesel-mysql example
145+
run: cargo build --manifest-path ./examples/diesel-mysql/Cargo.toml
146+
- name: Build diesel-postgres example
147+
run: cargo build --manifest-path ./examples/diesel-postgres/Cargo.toml
148+
- name: Run diesel-sqlite example
149+
run: cargo build --manifest-path ./examples/diesel-sqlite/Cargo.toml
144150

145151
# Generic testing for most cross targets. Some get special treatment in
146152
# other jobs.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/Cargo.lock
2+
/examples/**/Cargo.lock
23
/target
4+
/examples/**/target
35
/tmp
46
/ecma-test262
57
/BREADCRUMBS

.vim/coc-settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"rust-analyzer.linkedProjects": [
33
"bench/Cargo.toml",
4+
"examples/diesel-mysql/Cargo.toml",
5+
"examples/diesel-postgres/Cargo.toml",
6+
"examples/diesel-sqlite/Cargo.toml",
47
"examples/sqlx-postgres/Cargo.toml",
58
"examples/sqlx-sqlite/Cargo.toml",
69
"Cargo.toml"

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ include = [
3131
[workspace]
3232
members = [
3333
"crates/jiff-cli",
34+
"crates/jiff-diesel",
3435
"crates/jiff-icu",
3536
"crates/jiff-sqlx",
3637
"crates/jiff-tzdb",
3738
"crates/jiff-tzdb-platform",
3839
"examples/*",
3940
]
40-
exclude = ["examples/sqlx-postgres", "examples/sqlx-sqlite"]
41+
exclude = [
42+
"examples/diesel-mysql",
43+
"examples/diesel-postgres",
44+
"examples/diesel-sqlite",
45+
"examples/sqlx-postgres",
46+
"examples/sqlx-sqlite",
47+
]
4148

4249
# Features are documented in the "Crate features" section of the crate docs:
4350
# https://docs.rs/jiff/*/#crate-features

crates/jiff-diesel/COPYING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This project is dual-licensed under the Unlicense and MIT licenses.
2+
3+
You may use this code under the terms of either license.

crates/jiff-diesel/Cargo.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "jiff-diesel"
3+
version = "0.0.1" #:version
4+
authors = ["Andrew Gallant <[email protected]>"]
5+
license = "Unlicense OR MIT"
6+
homepage = "https://github.com/BurntSushi/jiff/tree/master/crates/jiff-diesel"
7+
repository = "https://github.com/BurntSushi/jiff"
8+
documentation = "https://docs.rs/jiff-diesel"
9+
description = "Integration for Jiff in Diesel."
10+
categories = ["date-and-time"]
11+
keywords = ["date", "time", "jiff", "diesel", "zone"]
12+
workspace = "../.."
13+
edition = "2021"
14+
rust-version = "1.70"
15+
include = ["/*.rs", "COPYING", "LICENSE-MIT", "UNLICENSE"]
16+
17+
[lib]
18+
name = "jiff_diesel"
19+
bench = false
20+
path = "src/lib.rs"
21+
22+
[features]
23+
default = []
24+
mysql = ["diesel/mysql"]
25+
postgres = ["diesel/postgres"]
26+
sqlite = ["diesel/sqlite"]
27+
28+
[dependencies]
29+
jiff = { path = "../..", default-features = false }
30+
diesel = { version = "2.0.0", default-features = false }
31+
32+
[dev-dependencies]
33+
jiff = { path = "../..", default-features = true }

crates/jiff-diesel/LICENSE-MIT

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Andrew Gallant
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

crates/jiff-diesel/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
jiff-icu
2+
========
3+
WIP
4+
5+
### Documentation
6+
7+
https://docs.rs/jiff-icu

crates/jiff-diesel/UNLICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org/>

crates/jiff-diesel/src/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*!
2+
This crate provides integration points for [Jiff](jiff) and [Diesel](diesel).
3+
4+
Examples can be found in the
5+
[examples directory of the Jiff repository][examples].
6+
7+
# Organization
8+
9+
This crates defines several types that wrap corresponding types in
10+
Jiff. Each wrapper type provides implementations of traits found in
11+
Diesel. For most types, these are the [`diesel::deserialize::Queryable`],
12+
[`diesel::deserialize::FromSql`] and [`diesel::serialize::ToSql`] traits.
13+
14+
The intended workflow is to use these wrapper types within your wire types for
15+
encoding and decoding data from databases such as PostgreSQL. The wrapper types
16+
own the logic for encoding and decoding the data in database specific formats.
17+
18+
In order to the minimize the annoyance of wrapper types, the following
19+
conveniences are afforded:
20+
21+
* A [`ToDiesel`] trait is provided. Several Jiff types implement this trait.
22+
The trait provides easy conversion to the corresponding wrapper type in this
23+
crate.
24+
* A concrete `to_jiff` method is provided on each wrapper type. For example,
25+
[`Timestamp::to_jiff`]. This method is the reverse of `ToDiesel`. This converts
26+
from the wrapper type to the corresponding Jiff type.
27+
* There are `From` trait implementations from the wrapper type to the
28+
corresponding Jiff type, and vice versa.
29+
30+
# Database support
31+
32+
At present, MySQL, PostgreSQL and SQLite are supported.
33+
34+
# Future
35+
36+
This crate exists because there are generally only three ways to implement
37+
the necessary traits in Diesel:
38+
39+
1. Make Jiff depend on Diesel, and implement the corresponding traits where
40+
Jiff's types are defined.
41+
2. Make Diesel depend on Jiff, and implement the corresponding traits where the
42+
traits are defined.
43+
3. Make a crate like this one with types that wrap Jiff's types, and implements
44+
the corresponding traits for the wrapper types.
45+
46+
This was done because it seems inappropriate for a "lower level" crate like
47+
Jiff to depend on Diesel. And while it might be appropriate for Diesel to
48+
optionally depend on Jiff (like it does for [`chrono`] or [`time`]), at time of
49+
writing, Jiff is still early in its life. It's totally reasonable to wait for
50+
it to mature. Plus, the thought of three different datetime integrations is,
51+
admittedly, tough to stomach.
52+
53+
In the future, it may be prudent for this crate to be upstreamed into Diesel
54+
itself.
55+
56+
[examples]: https://github.com/BurntSushi/jiff/tree/master/examples
57+
[`chrono`]: https://docs.rs/chrono
58+
[`time`]: https://docs.rs/time
59+
*/
60+
#![deny(missing_docs)]
61+
62+
pub use self::wrappers::{Date, DateTime, Span, Time, Timestamp, ToDiesel};
63+
64+
#[cfg(feature = "mysql")]
65+
mod mysql;
66+
#[cfg(feature = "postgres")]
67+
mod postgres;
68+
#[cfg(feature = "sqlite")]
69+
mod sqlite;
70+
mod wrappers;

0 commit comments

Comments
 (0)