| 1 | use bytes::BytesMut; |
| 2 | use cidr_03::{IpCidr, IpInet}; |
| 3 | use postgres_protocol::types; |
| 4 | use std::error::Error; |
| 5 | |
| 6 | use crate::{FromSql, IsNull, ToSql, Type}; |
| 7 | |
| 8 | impl<'a> FromSql<'a> for IpCidr { |
| 9 | fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> { |
| 10 | let inet = types::inet_from_sql(raw)?; |
| 11 | Ok(IpCidr::new(inet.addr(), inet.netmask())?) |
| 12 | } |
| 13 | |
| 14 | accepts!(CIDR); |
| 15 | } |
| 16 | |
| 17 | impl ToSql for IpCidr { |
| 18 | fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> { |
| 19 | types::inet_to_sql(self.first_address(), self.network_length(), w); |
| 20 | Ok(IsNull::No) |
| 21 | } |
| 22 | |
| 23 | accepts!(CIDR); |
| 24 | to_sql_checked!(); |
| 25 | } |
| 26 | |
| 27 | impl<'a> FromSql<'a> for IpInet { |
| 28 | fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> { |
| 29 | let inet = types::inet_from_sql(raw)?; |
| 30 | Ok(IpInet::new(inet.addr(), inet.netmask())?) |
| 31 | } |
| 32 | |
| 33 | accepts!(INET); |
| 34 | } |
| 35 | |
| 36 | impl ToSql for IpInet { |
| 37 | fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> { |
| 38 | types::inet_to_sql(self.address(), self.network_length(), w); |
| 39 | Ok(IsNull::No) |
| 40 | } |
| 41 | |
| 42 | accepts!(INET); |
| 43 | to_sql_checked!(); |
| 44 | } |