diff --git a/Cargo.toml b/Cargo.toml index cf7f99ca..fe3568fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "postgres-native-tls", "postgres-openssl", "postgres-protocol", + "postgres-types", "tokio-postgres", ] diff --git a/codegen/src/main.rs b/codegen/src/main.rs index 6d99ded6..d0a74f2d 100644 --- a/codegen/src/main.rs +++ b/codegen/src/main.rs @@ -6,15 +6,12 @@ extern crate marksman_escape; extern crate phf_codegen; extern crate regex; -use std::path::Path; - mod sqlstate; mod type_gen; fn main() { - let path = Path::new("../tokio-postgres/src"); - sqlstate::build(path); - type_gen::build(path); + sqlstate::build(); + type_gen::build(); } fn snake_to_camel(s: &str) -> String { diff --git a/codegen/src/sqlstate.rs b/codegen/src/sqlstate.rs index 2da7e8dc..3d7a6143 100644 --- a/codegen/src/sqlstate.rs +++ b/codegen/src/sqlstate.rs @@ -2,12 +2,11 @@ use linked_hash_map::LinkedHashMap; use phf_codegen; use std::fs::File; use std::io::{BufWriter, Write}; -use std::path::Path; const ERRCODES_TXT: &str = include_str!("errcodes.txt"); -pub fn build(path: &Path) { - let mut file = BufWriter::new(File::create(path.join("error/sqlstate.rs")).unwrap()); +pub fn build() { + let mut file = BufWriter::new(File::create("../tokio-postgres/src/error/sqlstate.rs").unwrap()); let codes = parse_codes(); diff --git a/codegen/src/type_gen.rs b/codegen/src/type_gen.rs index 65c0852a..1e882ab1 100644 --- a/codegen/src/type_gen.rs +++ b/codegen/src/type_gen.rs @@ -5,7 +5,6 @@ use std::fmt::Write as _; use std::fs::File; use std::io::{BufWriter, Write}; use std::iter; -use std::path::Path; use std::str; use crate::snake_to_camel; @@ -22,8 +21,8 @@ struct Type { doc: String, } -pub fn build(path: &Path) { - let mut file = BufWriter::new(File::create(path.join("types/type_gen.rs")).unwrap()); +pub fn build() { + let mut file = BufWriter::new(File::create("../postgres-types/src/type_gen.rs").unwrap()); let types = parse_types(); make_header(&mut file); @@ -266,7 +265,7 @@ fn make_header(w: &mut BufWriter) { "// Autogenerated file - DO NOT EDIT use std::sync::Arc; -use crate::types::{{Type, Oid, Kind}}; +use crate::{{Type, Oid, Kind}}; #[derive(PartialEq, Eq, Debug)] pub struct Other {{ diff --git a/postgres-types/Cargo.toml b/postgres-types/Cargo.toml new file mode 100644 index 00000000..a3c762a5 --- /dev/null +++ b/postgres-types/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "postgres-types" +version = "0.1.0" +authors = ["Steven Fackler "] +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 } diff --git a/tokio-postgres/src/types/bit_vec_06.rs b/postgres-types/src/bit_vec_06.rs similarity index 93% rename from tokio-postgres/src/types/bit_vec_06.rs rename to postgres-types/src/bit_vec_06.rs index a68ed35a..52fb6d1c 100644 --- a/tokio-postgres/src/types/bit_vec_06.rs +++ b/postgres-types/src/bit_vec_06.rs @@ -2,7 +2,7 @@ use bit_vec_06::BitVec; use postgres_protocol::types; use std::error::Error; -use crate::types::{FromSql, IsNull, ToSql, Type}; +use crate::{FromSql, IsNull, ToSql, Type}; impl<'a> FromSql<'a> for BitVec { fn from_sql(_: &Type, raw: &[u8]) -> Result> { diff --git a/tokio-postgres/src/types/chrono_04.rs b/postgres-types/src/chrono_04.rs similarity index 98% rename from tokio-postgres/src/types/chrono_04.rs rename to postgres-types/src/chrono_04.rs index e414a93f..8a3c8a22 100644 --- a/tokio-postgres/src/types/chrono_04.rs +++ b/postgres-types/src/chrono_04.rs @@ -2,7 +2,7 @@ use chrono_04::{DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateTime use postgres_protocol::types; use std::error::Error; -use crate::types::{FromSql, IsNull, ToSql, Type}; +use crate::{FromSql, IsNull, ToSql, Type}; fn base() -> NaiveDateTime { NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0) diff --git a/tokio-postgres/src/types/eui48_04.rs b/postgres-types/src/eui48_04.rs similarity index 92% rename from tokio-postgres/src/types/eui48_04.rs rename to postgres-types/src/eui48_04.rs index d0a67f8a..3e7422ee 100644 --- a/tokio-postgres/src/types/eui48_04.rs +++ b/postgres-types/src/eui48_04.rs @@ -2,7 +2,7 @@ use eui48_04::MacAddress; use postgres_protocol::types; use std::error::Error; -use crate::types::{FromSql, IsNull, ToSql, Type}; +use crate::{FromSql, IsNull, ToSql, Type}; impl<'a> FromSql<'a> for MacAddress { fn from_sql(_: &Type, raw: &[u8]) -> Result> { diff --git a/tokio-postgres/src/types/geo_types_04.rs b/postgres-types/src/geo_types_04.rs similarity index 97% rename from tokio-postgres/src/types/geo_types_04.rs rename to postgres-types/src/geo_types_04.rs index b3e7245c..f51273f2 100644 --- a/tokio-postgres/src/types/geo_types_04.rs +++ b/postgres-types/src/geo_types_04.rs @@ -3,7 +3,7 @@ use geo_types_04::{Coordinate, LineString, Point, Rect}; use postgres_protocol::types; use std::error::Error; -use crate::types::{FromSql, IsNull, ToSql, Type}; +use crate::{FromSql, IsNull, ToSql, Type}; impl<'a> FromSql<'a> for Point { fn from_sql(_: &Type, raw: &[u8]) -> Result> { diff --git a/tokio-postgres/src/types/mod.rs b/postgres-types/src/lib.rs similarity index 96% rename from tokio-postgres/src/types/mod.rs rename to postgres-types/src/lib.rs index f938bd7c..0224abfa 100644 --- a/tokio-postgres/src/types/mod.rs +++ b/postgres-types/src/lib.rs @@ -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 postgres_protocol; use postgres_protocol::types::{self, ArrayDimension}; +use std::any::type_name; use std::borrow::Cow; use std::collections::HashMap; use std::error::Error; @@ -12,12 +17,12 @@ use std::net::IpAddr; use std::sync::Arc; use std::time::{Duration, SystemTime, UNIX_EPOCH}; -use crate::types::type_gen::{Inner, Other}; +use crate::type_gen::{Inner, Other}; #[doc(inline)] 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 const TIME_SEC_CONVERSION: u64 = 946_684_800; @@ -29,9 +34,9 @@ const NSEC_PER_USEC: u64 = 1_000; #[macro_export] macro_rules! accepts { ($($expected:ident),+) => ( - fn accepts(ty: &$crate::types::Type) -> bool { + fn accepts(ty: &$crate::Type) -> bool { match *ty { - $($crate::types::Type::$expected)|+ => true, + $($crate::Type::$expected)|+ => true, _ => false } } @@ -45,13 +50,13 @@ macro_rules! accepts { macro_rules! to_sql_checked { () => { fn to_sql_checked(&self, - ty: &$crate::types::Type, + ty: &$crate::Type, out: &mut ::std::vec::Vec) - -> ::std::result::Result<$crate::types::IsNull, + -> ::std::result::Result<$crate::IsNull, Box> { - $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")] pub use crate::types::serde_json_1::Json; -use std::any::type_name; /// A Postgres type. #[derive(PartialEq, Eq, Clone, Debug)] @@ -108,7 +112,8 @@ impl fmt::Display for 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 { name, oid, @@ -176,7 +181,8 @@ pub struct 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_ } } @@ -225,7 +231,8 @@ impl fmt::Display for WrongType { impl Error for WrongType {} impl WrongType { - pub(crate) fn new(ty: Type) -> WrongType { + /// Creates a new `WrongType` error. + pub fn new(ty: Type) -> WrongType { WrongType { postgres: ty, rust: type_name::(), diff --git a/tokio-postgres/src/types/serde_json_1.rs b/postgres-types/src/serde_json_1.rs similarity index 97% rename from tokio-postgres/src/types/serde_json_1.rs rename to postgres-types/src/serde_json_1.rs index b3b2e3c9..01616d32 100644 --- a/tokio-postgres/src/types/serde_json_1.rs +++ b/postgres-types/src/serde_json_1.rs @@ -4,7 +4,7 @@ use std::error::Error; use std::fmt::Debug; 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. #[derive(Debug)] diff --git a/tokio-postgres/src/types/special.rs b/postgres-types/src/special.rs similarity index 98% rename from tokio-postgres/src/types/special.rs rename to postgres-types/src/special.rs index 15538f7a..b1da02a7 100644 --- a/tokio-postgres/src/types/special.rs +++ b/postgres-types/src/special.rs @@ -2,7 +2,7 @@ use postgres_protocol::types; use std::error::Error; 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. #[derive(Debug, Clone, Copy, PartialEq)] diff --git a/tokio-postgres/src/types/type_gen.rs b/postgres-types/src/type_gen.rs similarity index 99% rename from tokio-postgres/src/types/type_gen.rs rename to postgres-types/src/type_gen.rs index 626cd1a3..0ff5a219 100644 --- a/tokio-postgres/src/types/type_gen.rs +++ b/postgres-types/src/type_gen.rs @@ -1,7 +1,7 @@ // Autogenerated file - DO NOT EDIT use std::sync::Arc; -use crate::types::{Kind, Oid, Type}; +use crate::{Kind, Oid, Type}; #[derive(PartialEq, Eq, Debug)] pub struct Other { diff --git a/tokio-postgres/src/types/uuid_07.rs b/postgres-types/src/uuid_07.rs similarity index 100% rename from tokio-postgres/src/types/uuid_07.rs rename to postgres-types/src/uuid_07.rs diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index 901d3c4e..cf30fd26 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -62,9 +62,7 @@ use tokio::runtime::{self, Runtime}; #[cfg(feature = "runtime")] pub use tokio_postgres::Socket; -pub use tokio_postgres::{ - accepts, error, row, tls, to_sql_checked, types, Column, Portal, SimpleQueryMessage, Statement, -}; +pub use tokio_postgres::{error, row, tls, types, Column, Portal, SimpleQueryMessage, Statement}; pub use crate::client::*; #[cfg(feature = "runtime")] diff --git a/tokio-postgres/Cargo.toml b/tokio-postgres/Cargo.toml index 63e174d5..ff83c50e 100644 --- a/tokio-postgres/Cargo.toml +++ b/tokio-postgres/Cargo.toml @@ -23,12 +23,12 @@ circle-ci = { repository = "sfackler/rust-postgres" } default = ["runtime"] runtime = ["tokio/rt-full", "tokio/tcp", "tokio/uds", "tokio-executor", "lazy_static"] -"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"] +"with-bit-vec-0_6" = ["postgres-types/with-bit-vec-0_6"] +"with-chrono-0_4" = ["postgres-types/with-chrono-0_4"] +"with-eui48-0_4" = ["postgres-types/with-eui48-0_4"] +"with-geo-types-0_4" = ["postgres-types/with-geo-types-0_4"] +with-serde_json-1 = ["postgres-types/with-serde_json-1"] +"with-uuid-0_7" = ["postgres-types/with-uuid-0_7"] [dependencies] bytes = "0.4" @@ -40,19 +40,12 @@ percent-encoding = "1.0" pin-utils = "=0.1.0-alpha.4" phf = "0.7.23" 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-executor = { version = "=0.2.0-alpha.6", 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] tokio = "=0.2.0-alpha.6" env_logger = "0.5" diff --git a/tokio-postgres/src/types.rs b/tokio-postgres/src/types.rs new file mode 100644 index 00000000..b2e15d05 --- /dev/null +++ b/tokio-postgres/src/types.rs @@ -0,0 +1,6 @@ +//! Types. +//! +//! This module is a reexport of the `postgres_types` crate. + +#[doc(inline)] +pub use postgres_types::*; diff --git a/tokio-postgres/tests/test/types/mod.rs b/tokio-postgres/tests/test/types/mod.rs index b89434fa..7311ea28 100644 --- a/tokio-postgres/tests/test/types/mod.rs +++ b/tokio-postgres/tests/test/types/mod.rs @@ -7,7 +7,7 @@ use std::fmt; use std::net::IpAddr; use std::result; 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 crate::connect;