Skip to content

Commit 4b5bcbb

Browse files
committed
Split ToSql/FromSql out to a separate crate
Prep for a `derive` feature.
1 parent 42f1f96 commit 4b5bcbb

File tree

18 files changed

+74
-49
lines changed

18 files changed

+74
-49
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"postgres-native-tls",
66
"postgres-openssl",
77
"postgres-protocol",
8+
"postgres-types",
89
"tokio-postgres",
910
]
1011

codegen/src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ extern crate marksman_escape;
66
extern crate phf_codegen;
77
extern crate regex;
88

9-
use std::path::Path;
10-
119
mod sqlstate;
1210
mod type_gen;
1311

1412
fn main() {
15-
let path = Path::new("../tokio-postgres/src");
16-
sqlstate::build(path);
17-
type_gen::build(path);
13+
sqlstate::build();
14+
type_gen::build();
1815
}
1916

2017
fn snake_to_camel(s: &str) -> String {

codegen/src/sqlstate.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use linked_hash_map::LinkedHashMap;
22
use phf_codegen;
33
use std::fs::File;
44
use std::io::{BufWriter, Write};
5-
use std::path::Path;
65

76
const ERRCODES_TXT: &str = include_str!("errcodes.txt");
87

9-
pub fn build(path: &Path) {
10-
let mut file = BufWriter::new(File::create(path.join("error/sqlstate.rs")).unwrap());
8+
pub fn build() {
9+
let mut file = BufWriter::new(File::create("../tokio-postgres/src/error/sqlstate.rs").unwrap());
1110

1211
let codes = parse_codes();
1312

codegen/src/type_gen.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::fmt::Write as _;
55
use std::fs::File;
66
use std::io::{BufWriter, Write};
77
use std::iter;
8-
use std::path::Path;
98
use std::str;
109

1110
use crate::snake_to_camel;
@@ -22,8 +21,8 @@ struct Type {
2221
doc: String,
2322
}
2423

25-
pub fn build(path: &Path) {
26-
let mut file = BufWriter::new(File::create(path.join("types/type_gen.rs")).unwrap());
24+
pub fn build() {
25+
let mut file = BufWriter::new(File::create("../postgres-types/src/type_gen.rs").unwrap());
2726
let types = parse_types();
2827

2928
make_header(&mut file);
@@ -266,7 +265,7 @@ fn make_header(w: &mut BufWriter<File>) {
266265
"// Autogenerated file - DO NOT EDIT
267266
use std::sync::Arc;
268267
269-
use crate::types::{{Type, Oid, Kind}};
268+
use crate::{{Type, Oid, Kind}};
270269
271270
#[derive(PartialEq, Eq, Debug)]
272271
pub struct Other {{

postgres-types/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "postgres-types"
3+
version = "0.1.0"
4+
authors = ["Steven Fackler <[email protected]>"]
5+
edition = "2018"
6+
7+
[features]
8+
"with-bit-vec-0_6" = ["bit-vec-06"]
9+
"with-chrono-0_4" = ["chrono-04"]
10+
"with-eui48-0_4" = ["eui48-04"]
11+
"with-geo-types-0_4" = ["geo-types-04"]
12+
with-serde_json-1 = ["serde-1", "serde_json-1"]
13+
"with-uuid-0_7" = ["uuid-07"]
14+
15+
[dependencies]
16+
fallible-iterator = "0.2"
17+
postgres-protocol = { version = "0.4.1", path = "../postgres-protocol" }
18+
19+
bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
20+
chrono-04 = { version = "0.4", package = "chrono", optional = true }
21+
eui48-04 = { version = "0.4", package = "eui48", optional = true }
22+
geo-types-04 = { version = "0.4", package = "geo-types", optional = true }
23+
serde-1 = { version = "1.0", package = "serde", optional = true }
24+
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
25+
uuid-07 = { version = "0.7", package = "uuid", optional = true }

tokio-postgres/src/types/bit_vec_06.rs renamed to postgres-types/src/bit_vec_06.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bit_vec_06::BitVec;
22
use postgres_protocol::types;
33
use std::error::Error;
44

5-
use crate::types::{FromSql, IsNull, ToSql, Type};
5+
use crate::{FromSql, IsNull, ToSql, Type};
66

77
impl<'a> FromSql<'a> for BitVec {
88
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<dyn Error + Sync + Send>> {

tokio-postgres/src/types/chrono_04.rs renamed to postgres-types/src/chrono_04.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use chrono_04::{DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime
22
use postgres_protocol::types;
33
use std::error::Error;
44

5-
use crate::types::{FromSql, IsNull, ToSql, Type};
5+
use crate::{FromSql, IsNull, ToSql, Type};
66

77
fn base() -> NaiveDateTime {
88
NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0)

tokio-postgres/src/types/eui48_04.rs renamed to postgres-types/src/eui48_04.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use eui48_04::MacAddress;
22
use postgres_protocol::types;
33
use std::error::Error;
44

5-
use crate::types::{FromSql, IsNull, ToSql, Type};
5+
use crate::{FromSql, IsNull, ToSql, Type};
66

77
impl<'a> FromSql<'a> for MacAddress {
88
fn from_sql(_: &Type, raw: &[u8]) -> Result<MacAddress, Box<dyn Error + Sync + Send>> {

tokio-postgres/src/types/geo_types_04.rs renamed to postgres-types/src/geo_types_04.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use geo_types_04::{Coordinate, LineString, Point, Rect};
33
use postgres_protocol::types;
44
use std::error::Error;
55

6-
use crate::types::{FromSql, IsNull, ToSql, Type};
6+
use crate::{FromSql, IsNull, ToSql, Type};
77

88
impl<'a> FromSql<'a> for Point<f64> {
99
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {

tokio-postgres/src/types/mod.rs renamed to postgres-types/src/lib.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
//! Types.
1+
//! Conversions to and from Postgres types.
2+
//!
3+
//! This crate is used by the `tokio-postgres` and `postgres` crates. You normally don't need to depend directly on it
4+
//! unless you want to define your own `ToSql` or `FromSql` definitions.
5+
#![warn(missing_docs)]
26

37
use fallible_iterator::FallibleIterator;
48
use postgres_protocol;
59
use postgres_protocol::types::{self, ArrayDimension};
10+
use std::any::type_name;
611
use std::borrow::Cow;
712
use std::collections::HashMap;
813
use std::error::Error;
@@ -12,12 +17,12 @@ use std::net::IpAddr;
1217
use std::sync::Arc;
1318
use std::time::{Duration, SystemTime, UNIX_EPOCH};
1419

15-
use crate::types::type_gen::{Inner, Other};
20+
use crate::type_gen::{Inner, Other};
1621

1722
#[doc(inline)]
1823
pub use postgres_protocol::Oid;
1924

20-
pub use crate::types::special::{Date, Timestamp};
25+
pub use crate::special::{Date, Timestamp};
2126

2227
// Number of seconds from 1970-01-01 to 2000-01-01
2328
const TIME_SEC_CONVERSION: u64 = 946_684_800;
@@ -29,9 +34,9 @@ const NSEC_PER_USEC: u64 = 1_000;
2934
#[macro_export]
3035
macro_rules! accepts {
3136
($($expected:ident),+) => (
32-
fn accepts(ty: &$crate::types::Type) -> bool {
37+
fn accepts(ty: &$crate::Type) -> bool {
3338
match *ty {
34-
$($crate::types::Type::$expected)|+ => true,
39+
$($crate::Type::$expected)|+ => true,
3540
_ => false
3641
}
3742
}
@@ -45,13 +50,13 @@ macro_rules! accepts {
4550
macro_rules! to_sql_checked {
4651
() => {
4752
fn to_sql_checked(&self,
48-
ty: &$crate::types::Type,
53+
ty: &$crate::Type,
4954
out: &mut ::std::vec::Vec<u8>)
50-
-> ::std::result::Result<$crate::types::IsNull,
55+
-> ::std::result::Result<$crate::IsNull,
5156
Box<dyn ::std::error::Error +
5257
::std::marker::Sync +
5358
::std::marker::Send>> {
54-
$crate::types::__to_sql_checked(self, ty, out)
59+
$crate::__to_sql_checked(self, ty, out)
5560
}
5661
}
5762
}
@@ -91,7 +96,6 @@ mod type_gen;
9196

9297
#[cfg(feature = "with-serde_json-1")]
9398
pub use crate::types::serde_json_1::Json;
94-
use std::any::type_name;
9599

96100
/// A Postgres type.
97101
#[derive(PartialEq, Eq, Clone, Debug)]
@@ -108,7 +112,8 @@ impl fmt::Display for Type {
108112
}
109113

110114
impl Type {
111-
pub(crate) fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Type {
115+
/// Creates a new `Type`.
116+
pub fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Type {
112117
Type(Inner::Other(Arc::new(Other {
113118
name,
114119
oid,
@@ -176,7 +181,8 @@ pub struct Field {
176181
}
177182

178183
impl Field {
179-
pub(crate) fn new(name: String, type_: Type) -> Field {
184+
/// Creates a new `Field`.
185+
pub fn new(name: String, type_: Type) -> Field {
180186
Field { name, type_ }
181187
}
182188

@@ -225,7 +231,8 @@ impl fmt::Display for WrongType {
225231
impl Error for WrongType {}
226232

227233
impl WrongType {
228-
pub(crate) fn new<T>(ty: Type) -> WrongType {
234+
/// Creates a new `WrongType` error.
235+
pub fn new<T>(ty: Type) -> WrongType {
229236
WrongType {
230237
postgres: ty,
231238
rust: type_name::<T>(),

tokio-postgres/src/types/serde_json_1.rs renamed to postgres-types/src/serde_json_1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::error::Error;
44
use std::fmt::Debug;
55
use std::io::Read;
66

7-
use crate::types::{FromSql, IsNull, ToSql, Type};
7+
use crate::{FromSql, IsNull, ToSql, Type};
88

99
/// A wrapper type to allow arbitrary `Serialize`/`Deserialize` types to convert to Postgres JSON values.
1010
#[derive(Debug)]

tokio-postgres/src/types/special.rs renamed to postgres-types/src/special.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use postgres_protocol::types;
22
use std::error::Error;
33
use std::{i32, i64};
44

5-
use crate::types::{FromSql, IsNull, ToSql, Type};
5+
use crate::{FromSql, IsNull, ToSql, Type};
66

77
/// A wrapper that can be used to represent infinity with `Type::Date` types.
88
#[derive(Debug, Clone, Copy, PartialEq)]

tokio-postgres/src/types/type_gen.rs renamed to postgres-types/src/type_gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Autogenerated file - DO NOT EDIT
22
use std::sync::Arc;
33

4-
use crate::types::{Kind, Oid, Type};
4+
use crate::{Kind, Oid, Type};
55

66
#[derive(PartialEq, Eq, Debug)]
77
pub struct Other {
File renamed without changes.

postgres/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ use tokio::runtime::{self, Runtime};
6262

6363
#[cfg(feature = "runtime")]
6464
pub use tokio_postgres::Socket;
65-
pub use tokio_postgres::{
66-
accepts, error, row, tls, to_sql_checked, types, Column, Portal, SimpleQueryMessage, Statement,
67-
};
65+
pub use tokio_postgres::{error, row, tls, types, Column, Portal, SimpleQueryMessage, Statement};
6866

6967
pub use crate::client::*;
7068
#[cfg(feature = "runtime")]

tokio-postgres/Cargo.toml

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ circle-ci = { repository = "sfackler/rust-postgres" }
2323
default = ["runtime"]
2424
runtime = ["tokio/rt-full", "tokio/tcp", "tokio/uds", "tokio-executor", "lazy_static"]
2525

26-
"with-bit-vec-0_6" = ["bit-vec-06"]
27-
"with-chrono-0_4" = ["chrono-04"]
28-
"with-eui48-0_4" = ["eui48-04"]
29-
"with-geo-types-0_4" = ["geo-types-04"]
30-
with-serde_json-1 = ["serde-1", "serde_json-1"]
31-
"with-uuid-0_7" = ["uuid-07"]
26+
"with-bit-vec-0_6" = ["postgres-types/with-bit-vec-0_6"]
27+
"with-chrono-0_4" = ["postgres-types/with-chrono-0_4"]
28+
"with-eui48-0_4" = ["postgres-types/with-eui48-0_4"]
29+
"with-geo-types-0_4" = ["postgres-types/with-geo-types-0_4"]
30+
with-serde_json-1 = ["postgres-types/with-serde_json-1"]
31+
"with-uuid-0_7" = ["postgres-types/with-uuid-0_7"]
3232

3333
[dependencies]
3434
bytes = "0.4"
@@ -40,19 +40,12 @@ percent-encoding = "1.0"
4040
pin-utils = "=0.1.0-alpha.4"
4141
phf = "0.7.23"
4242
postgres-protocol = { version = "0.4.1", path = "../postgres-protocol" }
43+
postgres-types = { version = "0.1.0", path = "../postgres-types" }
4344
tokio = { version = "=0.2.0-alpha.6", default-features = false, features = ["io", "codec"] }
4445

4546
tokio-executor = { version = "=0.2.0-alpha.6", optional = true }
4647
lazy_static = { version = "1.0", optional = true }
4748

48-
bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
49-
chrono-04 = { version = "0.4", package = "chrono", optional = true }
50-
eui48-04 = { version = "0.4", package = "eui48", optional = true }
51-
geo-types-04 = { version = "0.4", package = "geo-types", optional = true }
52-
serde-1 = { version = "1.0", package = "serde", optional = true }
53-
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
54-
uuid-07 = { version = "0.7", package = "uuid", optional = true }
55-
5649
[dev-dependencies]
5750
tokio = "=0.2.0-alpha.6"
5851
env_logger = "0.5"

tokio-postgres/src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Types.
2+
//!
3+
//! This module is a reexport of the `postgres_types` crate.
4+
5+
#[doc(inline)]
6+
pub use postgres_types::*;

tokio-postgres/tests/test/types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt;
77
use std::net::IpAddr;
88
use std::result;
99
use std::time::{Duration, UNIX_EPOCH};
10-
use tokio_postgres::to_sql_checked;
10+
use postgres_types::to_sql_checked;
1111
use tokio_postgres::types::{FromSql, FromSqlOwned, IsNull, Kind, ToSql, Type, WrongType};
1212

1313
use crate::connect;

0 commit comments

Comments
 (0)