Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9685f9c

Browse files
committed
Add ToSql / FromSql for IpInet and IpCidr from cidr crate
1 parent25cabd5 commit9685f9c

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

‎postgres-types/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ categories = ["database"]
1414
derive = ["postgres-derive"]
1515
array-impls = ["array-init"]
1616
with-bit-vec-0_6 = ["bit-vec-06"]
17+
with-cidr-0_2 = ["cidr-02"]
1718
with-chrono-0_4 = ["chrono-04"]
1819
with-eui48-0_4 = ["eui48-04"]
1920
with-eui48-1 = ["eui48-1"]
@@ -32,6 +33,7 @@ postgres-derive = { version = "0.4.0", optional = true, path = "../postgres-deri
3233

3334
array-init = {version ="2",optional =true }
3435
bit-vec-06 = {version ="0.6",package ="bit-vec",optional =true }
36+
cidr-02 = {version ="0.2",package ="cidr",optional =true }
3537
chrono-04 = {version ="0.4.16",package ="chrono",default-features =false,features = ["clock"],optional =true }
3638
eui48-04 = {version ="0.4",package ="eui48",optional =true }
3739
eui48-1 = {version ="1.0",package ="eui48",optional =true }

‎postgres-types/src/cidr_02.rs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use bytes::BytesMut;
2+
use cidr_02::{IpCidr,IpInet};
3+
use postgres_protocol::types;
4+
use std::error::Error;
5+
6+
usecrate::{FromSql,IsNull,ToSql,Type};
7+
8+
impl<'a>FromSql<'a>forIpCidr{
9+
fnfrom_sql(_:&Type,raw:&[u8]) ->Result<Self,Box<dynError +Sync +Send>>{
10+
let inet = types::inet_from_sql(raw)?;
11+
Ok(IpCidr::new(inet.addr(), inet.netmask()).expect("postgres cidr type has zeroed host portion"))
12+
}
13+
14+
accepts!(CIDR);
15+
}
16+
17+
implToSqlforIpCidr{
18+
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +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>forIpInet{
28+
fnfrom_sql(_:&Type,raw:&[u8]) ->Result<Self,Box<dynError +Sync +Send>>{
29+
let inet = types::inet_from_sql(raw)?;
30+
Ok(IpInet::new(inet.addr(), inet.netmask()).expect("postgres enforces maximum length of netmask"))
31+
}
32+
33+
accepts!(INET);
34+
}
35+
36+
implToSqlforIpInet{
37+
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +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+
}

‎postgres-types/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ where
210210

211211
#[cfg(feature ="with-bit-vec-0_6")]
212212
mod bit_vec_06;
213+
#[cfg(feature ="with-cidr-0_2")]
214+
mod cidr_02;
213215
#[cfg(feature ="with-chrono-0_4")]
214216
mod chrono_04;
215217
#[cfg(feature ="with-eui48-0_4")]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp