From c9cca27e654ba0af8f59f799b371860091ce75d3 Mon Sep 17 00:00:00 2001 From: PoiScript Date: Fri, 20 Mar 2020 10:01:10 +0800 Subject: [PATCH] rename feature gate to `ipnetwork` --- Cargo.toml | 1 + sqlx-core/Cargo.toml | 3 +-- sqlx-core/src/postgres/types/{network.rs => ipnetwork.rs} | 2 +- sqlx-core/src/postgres/types/mod.rs | 6 ++++-- sqlx-core/src/types.rs | 6 ++++++ sqlx-macros/Cargo.toml | 1 + sqlx-macros/src/database/postgres.rs | 5 ++++- 7 files changed, 18 insertions(+), 6 deletions(-) rename sqlx-core/src/postgres/types/{network.rs => ipnetwork.rs} (97%) diff --git a/Cargo.toml b/Cargo.toml index 5c5b6e5a..2ff41fc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ sqlite = [ "sqlx-core/sqlite", "sqlx-macros/sqlite" ] # types bigdecimal = ["sqlx-core/bigdecimal_bigint", "sqlx-macros/bigdecimal"] chrono = [ "sqlx-core/chrono", "sqlx-macros/chrono" ] +ipnetwork = [ "sqlx-core/ipnetwork", "sqlx-macros/ipnetwork" ] uuid = [ "sqlx-core/uuid", "sqlx-macros/uuid" ] [dependencies] diff --git a/sqlx-core/Cargo.toml b/sqlx-core/Cargo.toml index 9b16aaa6..b0d07b48 100644 --- a/sqlx-core/Cargo.toml +++ b/sqlx-core/Cargo.toml @@ -18,7 +18,6 @@ unstable = [] # we need a feature which activates `num-bigint` as well because # `bigdecimal` uses types from it but does not reexport (tsk tsk) bigdecimal_bigint = ["bigdecimal", "num-bigint"] -network-address = [ "ipnetwork", "libc" ] postgres = [ "md-5", "sha2", "base64", "sha-1", "rand", "hmac", "futures-channel/sink", "futures-util/sink" ] mysql = [ "sha-1", "sha2", "generic-array", "num-bigint", "base64", "digest", "rand" ] sqlite = [ "libsqlite3-sys" ] @@ -45,7 +44,7 @@ generic-array = { version = "0.12.3", default-features = false, optional = true hex = "0.4.2" hmac = { version = "0.7.1", default-features = false, optional = true } ipnetwork = { version = "0.16.0", default-feature = false, optional = true } -libc = { version = "0.2.68", default-feature = false, optional = true } +libc = "0.2.68" log = { version = "0.4.8", default-features = false } md-5 = { version = "0.8.0", default-features = false, optional = true } memchr = { version = "2.3.3", default-features = false } diff --git a/sqlx-core/src/postgres/types/network.rs b/sqlx-core/src/postgres/types/ipnetwork.rs similarity index 97% rename from sqlx-core/src/postgres/types/network.rs rename to sqlx-core/src/postgres/types/ipnetwork.rs index 18135ef4..ea4f6d2e 100644 --- a/sqlx-core/src/postgres/types/network.rs +++ b/sqlx-core/src/postgres/types/ipnetwork.rs @@ -55,7 +55,7 @@ impl<'de> Decode<'de, Postgres> for IpNetwork { fn decode(value: Option>) -> crate::Result { match value.try_into()? { PgValue::Binary(buf) => decode(buf, INET_TYPE), - PgValue::Text(s) => decode(s.as_bytes(), INET_TYPE), + PgValue::Text(s) => s.parse().map_err(|err| crate::Error::decode(err)), } } } diff --git a/sqlx-core/src/postgres/types/mod.rs b/sqlx-core/src/postgres/types/mod.rs index 98574509..24aebb5a 100644 --- a/sqlx-core/src/postgres/types/mod.rs +++ b/sqlx-core/src/postgres/types/mod.rs @@ -35,7 +35,7 @@ //! //! ### [`ipnetwork`](https://crates.io/crates/ipnetwork) //! -//! Requires the `network-address` Cargo feature flag. +//! Requires the `ipnetwork` Cargo feature flag. //! //! | Rust type | Postgres type(s) | //! |---------------------------------------|------------------------------------------------------| @@ -78,7 +78,8 @@ mod chrono; #[cfg(feature = "uuid")] mod uuid; -mod network; +#[cfg(feature = "ipnetwork")] +mod ipnetwork; /// Type information for a Postgres SQL type. #[derive(Debug, Clone)] @@ -118,6 +119,7 @@ impl PgTypeInfo { TypeId::UUID => Some("uuid"), // we can support decoding `PgNumeric` but it's decidedly less useful to the layman TypeId::NUMERIC => Some("bigdecimal"), + TypeId::CIDR | TypeId::INET => Some("ipnetwork"), _ => None, } } diff --git a/sqlx-core/src/types.rs b/sqlx-core/src/types.rs index 134bb08f..039543fc 100644 --- a/sqlx-core/src/types.rs +++ b/sqlx-core/src/types.rs @@ -18,6 +18,12 @@ pub mod chrono { #[cfg_attr(docsrs, doc(cfg(feature = "bigdecimal")))] pub use bigdecimal::BigDecimal; +#[cfg(feature = "ipnetwork")] +#[cfg_attr(docsrs, doc(cfg(feature = "ipnetwork")))] +pub mod ipnetwork { + pub use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network}; +} + pub trait TypeInfo: Debug + Display + Clone { /// Compares type information to determine if `other` is compatible at the Rust level /// with `self`. diff --git a/sqlx-macros/Cargo.toml b/sqlx-macros/Cargo.toml index 928ff2fd..9ca2fa0e 100644 --- a/sqlx-macros/Cargo.toml +++ b/sqlx-macros/Cargo.toml @@ -29,6 +29,7 @@ sqlite = [ "sqlx/sqlite" ] # type bigdecimal = [ "sqlx/bigdecimal_bigint" ] chrono = [ "sqlx/chrono" ] +ipnetwork = [ "sqlx/ipnetwork" ] uuid = [ "sqlx/uuid" ] [dependencies] diff --git a/sqlx-macros/src/database/postgres.rs b/sqlx-macros/src/database/postgres.rs index 7d1c3c64..982bebad 100644 --- a/sqlx-macros/src/database/postgres.rs +++ b/sqlx-macros/src/database/postgres.rs @@ -27,7 +27,10 @@ impl_database_ext! { sqlx::types::chrono::DateTime | sqlx::types::chrono::DateTime<_>, #[cfg(feature = "bigdecimal")] - sqlx::types::BigDecimal + sqlx::types::BigDecimal, + + #[cfg(feature = "ipnetwork")] + sqlx::types::ipnetwork::IpNetwork }, ParamChecking::Strong, feature-types: info => info.type_feature_gate(),