Skip to content

Commit 40e8571

Browse files
committed
Merge pull request #158 from abaumhauer/macaddr
Macaddr
2 parents a37f103 + b6478d2 commit 40e8571

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ before_script:
1010
- "./.travis/setup.sh"
1111
script:
1212
- cargo test
13-
- cargo test --features "uuid rustc-serialize time unix_socket serde_json chrono openssl bit-vec"
13+
- cargo test --features "uuid rustc-serialize time unix_socket serde_json chrono openssl bit-vec eui48"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ unix_socket = { version = "0.5", optional = true }
4040
uuid = { version = "0.1", optional = true }
4141
security-framework = { version = "0.1.2", optional = true }
4242
bit-vec = { version = "0.4", optional = true }
43+
eui48 = { version = "0.1", optional = true }
4344

4445
[dev-dependencies]
4546
url = "0.5"

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ types. The driver currently supports the following conversions:
252252
<td>HashMap&lt;String, Option&lt;String&gt;&gt;</td>
253253
<td>HSTORE</td>
254254
</tr>
255+
<tr>
256+
<td>
257+
<a href="https://github.com/abaumhauer/eui48">eui48::MacAddress</a>
258+
(<a href="#optional-features">optional</a>)
259+
</td>
260+
<td>MACADDR</td>
261+
</tr>
255262
</tbody>
256263
</table>
257264

@@ -302,3 +309,9 @@ feature, which adds `ToSql` and `FromSql` implementations for `chrono`'s
302309
[BIT and VARBIT](http://www.postgresql.org/docs/9.4/static/datatype-bit.html)
303310
support is provided optionally by the `bit-vec` feature, which adds `ToSql` and
304311
`FromSql` implementations for `bit-vec`'s `BitVec` type.
312+
313+
### MACADDR type
314+
315+
[MACADDR](http://www.postgresql.org/docs/9.4/static/datatype-net-types.html#DATATYPE-MACADDR)
316+
support is provided optionally by the `eui48` feature, which adds `ToSql` and
317+
`FromSql` implementations for `eui48`'s `MacAddress` type.

src/types/eui48.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extern crate eui48;
2+
3+
use std::io::prelude::*;
4+
5+
use self::eui48::MacAddress;
6+
7+
use types::{FromSql, ToSql, Type, IsNull, SessionInfo};
8+
use Result;
9+
use util;
10+
11+
impl FromSql for MacAddress {
12+
fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<MacAddress> {
13+
let mut bytes = [0; 6];
14+
try!(util::read_all(raw, &mut bytes));
15+
Ok(MacAddress::new(bytes))
16+
}
17+
18+
accepts!(Type::Macaddr);
19+
}
20+
21+
impl ToSql for MacAddress {
22+
fn to_sql<W: Write + ?Sized>(&self, _: &Type, w: &mut W, _: &SessionInfo) -> Result<IsNull> {
23+
try!(w.write_all(self.as_bytes()));
24+
Ok(IsNull::No)
25+
}
26+
27+
accepts!(Type::Macaddr);
28+
to_sql_checked!();
29+
}

src/types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ mod rustc_serialize;
6767
mod serde_json;
6868
#[cfg(feature = "chrono")]
6969
mod chrono;
70+
#[cfg(feature = "eui48")]
71+
mod eui48;
7072

7173
/// A structure providing information for conversion methods.
7274
pub struct SessionInfo<'a> {
@@ -658,6 +660,7 @@ impl WrongTypeNew for WrongType {
658660
/// | chrono::DateTime&lt;FixedOffset&gt; | TIMESTAMP WITH TIME ZONE |
659661
/// | chrono::NaiveDate | DATE |
660662
/// | chrono::NaiveTime | TIME |
663+
/// | eui48::MacAddress | MACADDR |
661664
/// | uuid::Uuid | UUID |
662665
/// | bit_vec::BitVec | BIT, VARBIT |
663666
///

tests/types/eui48.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern crate eui48;
2+
3+
use types::test_type;
4+
5+
#[test]
6+
fn test_eui48_params() {
7+
test_type("MACADDR", &[(Some(eui48::MacAddress::parse_str("12-34-56-AB-CD-EF").unwrap()),
8+
"'12-34-56-ab-cd-ef'"), (None, "NULL")])
9+
}

0 commit comments

Comments
 (0)