Split ToSql/FromSql out to a separate crate

Prep for a `derive` feature.
This commit is contained in:
Steven Fackler 2019-10-07 17:10:34 -07:00
parent 42f1f9675c
commit 4b5bcbb602
18 changed files with 74 additions and 49 deletions

View File

@ -5,6 +5,7 @@ members = [
"postgres-native-tls", "postgres-native-tls",
"postgres-openssl", "postgres-openssl",
"postgres-protocol", "postgres-protocol",
"postgres-types",
"tokio-postgres", "tokio-postgres",
] ]

View File

@ -6,15 +6,12 @@ extern crate marksman_escape;
extern crate phf_codegen; extern crate phf_codegen;
extern crate regex; extern crate regex;
use std::path::Path;
mod sqlstate; mod sqlstate;
mod type_gen; mod type_gen;
fn main() { fn main() {
let path = Path::new("../tokio-postgres/src"); sqlstate::build();
sqlstate::build(path); type_gen::build();
type_gen::build(path);
} }
fn snake_to_camel(s: &str) -> String { fn snake_to_camel(s: &str) -> String {

View File

@ -2,12 +2,11 @@ use linked_hash_map::LinkedHashMap;
use phf_codegen; use phf_codegen;
use std::fs::File; use std::fs::File;
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::path::Path;
const ERRCODES_TXT: &str = include_str!("errcodes.txt"); const ERRCODES_TXT: &str = include_str!("errcodes.txt");
pub fn build(path: &Path) { pub fn build() {
let mut file = BufWriter::new(File::create(path.join("error/sqlstate.rs")).unwrap()); let mut file = BufWriter::new(File::create("../tokio-postgres/src/error/sqlstate.rs").unwrap());
let codes = parse_codes(); let codes = parse_codes();

View File

@ -5,7 +5,6 @@ use std::fmt::Write as _;
use std::fs::File; use std::fs::File;
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::iter; use std::iter;
use std::path::Path;
use std::str; use std::str;
use crate::snake_to_camel; use crate::snake_to_camel;
@ -22,8 +21,8 @@ struct Type {
doc: String, doc: String,
} }
pub fn build(path: &Path) { pub fn build() {
let mut file = BufWriter::new(File::create(path.join("types/type_gen.rs")).unwrap()); let mut file = BufWriter::new(File::create("../postgres-types/src/type_gen.rs").unwrap());
let types = parse_types(); let types = parse_types();
make_header(&mut file); make_header(&mut file);
@ -266,7 +265,7 @@ fn make_header(w: &mut BufWriter<File>) {
"// Autogenerated file - DO NOT EDIT "// Autogenerated file - DO NOT EDIT
use std::sync::Arc; use std::sync::Arc;
use crate::types::{{Type, Oid, Kind}}; use crate::{{Type, Oid, Kind}};
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub struct Other {{ pub struct Other {{

25
postgres-types/Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "postgres-types"
version = "0.1.0"
authors = ["Steven Fackler <sfackler@gmail.com>"]
edition = "2018"
[features]
"with-bit-vec-0_6" = ["bit-vec-06"]
"with-chrono-0_4" = ["chrono-04"]
"with-eui48-0_4" = ["eui48-04"]
"with-geo-types-0_4" = ["geo-types-04"]
with-serde_json-1 = ["serde-1", "serde_json-1"]
"with-uuid-0_7" = ["uuid-07"]
[dependencies]
fallible-iterator = "0.2"
postgres-protocol = { version = "0.4.1", path = "../postgres-protocol" }
bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
chrono-04 = { version = "0.4", package = "chrono", optional = true }
eui48-04 = { version = "0.4", package = "eui48", optional = true }
geo-types-04 = { version = "0.4", package = "geo-types", optional = true }
serde-1 = { version = "1.0", package = "serde", optional = true }
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
uuid-07 = { version = "0.7", package = "uuid", optional = true }

View File

@ -2,7 +2,7 @@ use bit_vec_06::BitVec;
use postgres_protocol::types; use postgres_protocol::types;
use std::error::Error; use std::error::Error;
use crate::types::{FromSql, IsNull, ToSql, Type}; use crate::{FromSql, IsNull, ToSql, Type};
impl<'a> FromSql<'a> for BitVec { impl<'a> FromSql<'a> for BitVec {
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<dyn Error + Sync + Send>> { fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<dyn Error + Sync + Send>> {

View File

@ -2,7 +2,7 @@ use chrono_04::{DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime
use postgres_protocol::types; use postgres_protocol::types;
use std::error::Error; use std::error::Error;
use crate::types::{FromSql, IsNull, ToSql, Type}; use crate::{FromSql, IsNull, ToSql, Type};
fn base() -> NaiveDateTime { fn base() -> NaiveDateTime {
NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0) NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0)

View File

@ -2,7 +2,7 @@ use eui48_04::MacAddress;
use postgres_protocol::types; use postgres_protocol::types;
use std::error::Error; use std::error::Error;
use crate::types::{FromSql, IsNull, ToSql, Type}; use crate::{FromSql, IsNull, ToSql, Type};
impl<'a> FromSql<'a> for MacAddress { impl<'a> FromSql<'a> for MacAddress {
fn from_sql(_: &Type, raw: &[u8]) -> Result<MacAddress, Box<dyn Error + Sync + Send>> { fn from_sql(_: &Type, raw: &[u8]) -> Result<MacAddress, Box<dyn Error + Sync + Send>> {

View File

@ -3,7 +3,7 @@ use geo_types_04::{Coordinate, LineString, Point, Rect};
use postgres_protocol::types; use postgres_protocol::types;
use std::error::Error; use std::error::Error;
use crate::types::{FromSql, IsNull, ToSql, Type}; use crate::{FromSql, IsNull, ToSql, Type};
impl<'a> FromSql<'a> for Point<f64> { impl<'a> FromSql<'a> for Point<f64> {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> { fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {

View File

@ -1,8 +1,13 @@
//! Types. //! Conversions to and from Postgres types.
//!
//! This crate is used by the `tokio-postgres` and `postgres` crates. You normally don't need to depend directly on it
//! unless you want to define your own `ToSql` or `FromSql` definitions.
#![warn(missing_docs)]
use fallible_iterator::FallibleIterator; use fallible_iterator::FallibleIterator;
use postgres_protocol; use postgres_protocol;
use postgres_protocol::types::{self, ArrayDimension}; use postgres_protocol::types::{self, ArrayDimension};
use std::any::type_name;
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
@ -12,12 +17,12 @@ use std::net::IpAddr;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::types::type_gen::{Inner, Other}; use crate::type_gen::{Inner, Other};
#[doc(inline)] #[doc(inline)]
pub use postgres_protocol::Oid; pub use postgres_protocol::Oid;
pub use crate::types::special::{Date, Timestamp}; pub use crate::special::{Date, Timestamp};
// Number of seconds from 1970-01-01 to 2000-01-01 // Number of seconds from 1970-01-01 to 2000-01-01
const TIME_SEC_CONVERSION: u64 = 946_684_800; const TIME_SEC_CONVERSION: u64 = 946_684_800;
@ -29,9 +34,9 @@ const NSEC_PER_USEC: u64 = 1_000;
#[macro_export] #[macro_export]
macro_rules! accepts { macro_rules! accepts {
($($expected:ident),+) => ( ($($expected:ident),+) => (
fn accepts(ty: &$crate::types::Type) -> bool { fn accepts(ty: &$crate::Type) -> bool {
match *ty { match *ty {
$($crate::types::Type::$expected)|+ => true, $($crate::Type::$expected)|+ => true,
_ => false _ => false
} }
} }
@ -45,13 +50,13 @@ macro_rules! accepts {
macro_rules! to_sql_checked { macro_rules! to_sql_checked {
() => { () => {
fn to_sql_checked(&self, fn to_sql_checked(&self,
ty: &$crate::types::Type, ty: &$crate::Type,
out: &mut ::std::vec::Vec<u8>) out: &mut ::std::vec::Vec<u8>)
-> ::std::result::Result<$crate::types::IsNull, -> ::std::result::Result<$crate::IsNull,
Box<dyn ::std::error::Error + Box<dyn ::std::error::Error +
::std::marker::Sync + ::std::marker::Sync +
::std::marker::Send>> { ::std::marker::Send>> {
$crate::types::__to_sql_checked(self, ty, out) $crate::__to_sql_checked(self, ty, out)
} }
} }
} }
@ -91,7 +96,6 @@ mod type_gen;
#[cfg(feature = "with-serde_json-1")] #[cfg(feature = "with-serde_json-1")]
pub use crate::types::serde_json_1::Json; pub use crate::types::serde_json_1::Json;
use std::any::type_name;
/// A Postgres type. /// A Postgres type.
#[derive(PartialEq, Eq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
@ -108,7 +112,8 @@ impl fmt::Display for Type {
} }
impl Type { impl Type {
pub(crate) fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Type { /// Creates a new `Type`.
pub fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Type {
Type(Inner::Other(Arc::new(Other { Type(Inner::Other(Arc::new(Other {
name, name,
oid, oid,
@ -176,7 +181,8 @@ pub struct Field {
} }
impl Field { impl Field {
pub(crate) fn new(name: String, type_: Type) -> Field { /// Creates a new `Field`.
pub fn new(name: String, type_: Type) -> Field {
Field { name, type_ } Field { name, type_ }
} }
@ -225,7 +231,8 @@ impl fmt::Display for WrongType {
impl Error for WrongType {} impl Error for WrongType {}
impl WrongType { impl WrongType {
pub(crate) fn new<T>(ty: Type) -> WrongType { /// Creates a new `WrongType` error.
pub fn new<T>(ty: Type) -> WrongType {
WrongType { WrongType {
postgres: ty, postgres: ty,
rust: type_name::<T>(), rust: type_name::<T>(),

View File

@ -4,7 +4,7 @@ use std::error::Error;
use std::fmt::Debug; use std::fmt::Debug;
use std::io::Read; use std::io::Read;
use crate::types::{FromSql, IsNull, ToSql, Type}; use crate::{FromSql, IsNull, ToSql, Type};
/// A wrapper type to allow arbitrary `Serialize`/`Deserialize` types to convert to Postgres JSON values. /// A wrapper type to allow arbitrary `Serialize`/`Deserialize` types to convert to Postgres JSON values.
#[derive(Debug)] #[derive(Debug)]

View File

@ -2,7 +2,7 @@ use postgres_protocol::types;
use std::error::Error; use std::error::Error;
use std::{i32, i64}; use std::{i32, i64};
use crate::types::{FromSql, IsNull, ToSql, Type}; use crate::{FromSql, IsNull, ToSql, Type};
/// A wrapper that can be used to represent infinity with `Type::Date` types. /// A wrapper that can be used to represent infinity with `Type::Date` types.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]

View File

@ -1,7 +1,7 @@
// Autogenerated file - DO NOT EDIT // Autogenerated file - DO NOT EDIT
use std::sync::Arc; use std::sync::Arc;
use crate::types::{Kind, Oid, Type}; use crate::{Kind, Oid, Type};
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub struct Other { pub struct Other {

View File

@ -62,9 +62,7 @@ use tokio::runtime::{self, Runtime};
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]
pub use tokio_postgres::Socket; pub use tokio_postgres::Socket;
pub use tokio_postgres::{ pub use tokio_postgres::{error, row, tls, types, Column, Portal, SimpleQueryMessage, Statement};
accepts, error, row, tls, to_sql_checked, types, Column, Portal, SimpleQueryMessage, Statement,
};
pub use crate::client::*; pub use crate::client::*;
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]

View File

@ -23,12 +23,12 @@ circle-ci = { repository = "sfackler/rust-postgres" }
default = ["runtime"] default = ["runtime"]
runtime = ["tokio/rt-full", "tokio/tcp", "tokio/uds", "tokio-executor", "lazy_static"] runtime = ["tokio/rt-full", "tokio/tcp", "tokio/uds", "tokio-executor", "lazy_static"]
"with-bit-vec-0_6" = ["bit-vec-06"] "with-bit-vec-0_6" = ["postgres-types/with-bit-vec-0_6"]
"with-chrono-0_4" = ["chrono-04"] "with-chrono-0_4" = ["postgres-types/with-chrono-0_4"]
"with-eui48-0_4" = ["eui48-04"] "with-eui48-0_4" = ["postgres-types/with-eui48-0_4"]
"with-geo-types-0_4" = ["geo-types-04"] "with-geo-types-0_4" = ["postgres-types/with-geo-types-0_4"]
with-serde_json-1 = ["serde-1", "serde_json-1"] with-serde_json-1 = ["postgres-types/with-serde_json-1"]
"with-uuid-0_7" = ["uuid-07"] "with-uuid-0_7" = ["postgres-types/with-uuid-0_7"]
[dependencies] [dependencies]
bytes = "0.4" bytes = "0.4"
@ -40,19 +40,12 @@ percent-encoding = "1.0"
pin-utils = "=0.1.0-alpha.4" pin-utils = "=0.1.0-alpha.4"
phf = "0.7.23" phf = "0.7.23"
postgres-protocol = { version = "0.4.1", path = "../postgres-protocol" } postgres-protocol = { version = "0.4.1", path = "../postgres-protocol" }
postgres-types = { version = "0.1.0", path = "../postgres-types" }
tokio = { version = "=0.2.0-alpha.6", default-features = false, features = ["io", "codec"] } tokio = { version = "=0.2.0-alpha.6", default-features = false, features = ["io", "codec"] }
tokio-executor = { version = "=0.2.0-alpha.6", optional = true } tokio-executor = { version = "=0.2.0-alpha.6", optional = true }
lazy_static = { version = "1.0", optional = true } lazy_static = { version = "1.0", optional = true }
bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
chrono-04 = { version = "0.4", package = "chrono", optional = true }
eui48-04 = { version = "0.4", package = "eui48", optional = true }
geo-types-04 = { version = "0.4", package = "geo-types", optional = true }
serde-1 = { version = "1.0", package = "serde", optional = true }
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
uuid-07 = { version = "0.7", package = "uuid", optional = true }
[dev-dependencies] [dev-dependencies]
tokio = "=0.2.0-alpha.6" tokio = "=0.2.0-alpha.6"
env_logger = "0.5" env_logger = "0.5"

View File

@ -0,0 +1,6 @@
//! Types.
//!
//! This module is a reexport of the `postgres_types` crate.
#[doc(inline)]
pub use postgres_types::*;

View File

@ -7,7 +7,7 @@ use std::fmt;
use std::net::IpAddr; use std::net::IpAddr;
use std::result; use std::result;
use std::time::{Duration, UNIX_EPOCH}; use std::time::{Duration, UNIX_EPOCH};
use tokio_postgres::to_sql_checked; use postgres_types::to_sql_checked;
use tokio_postgres::types::{FromSql, FromSqlOwned, IsNull, Kind, ToSql, Type, WrongType}; use tokio_postgres::types::{FromSql, FromSqlOwned, IsNull, Kind, ToSql, Type, WrongType};
use crate::connect; use crate::connect;