Skip to content

Commit

Permalink
Merge pull request #9 from dusk-network/auto-trait
Browse files Browse the repository at this point in the history
Add auto trait and bump to v0.1.1
  • Loading branch information
ZER0 committed Jan 22, 2021
2 parents 64805d3 + c607b50 commit 8775cd9
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 38 deletions.
2 changes: 1 addition & 1 deletion derive-hex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "derive-hex"
version = "0.1.0"
version = "0.1.1"
authors = ["zer0 <matteo@dusk.network>"]
edition = "2018"

Expand Down
2 changes: 1 addition & 1 deletion dusk-bytes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dusk-bytes"
version = "0.1.0"
version = "0.1.1"
authors = ["zer0 <matteo@dusk.network>"]
edition = "2018"

Expand Down
3 changes: 3 additions & 0 deletions dusk-bytes/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ fn val(c: u8) -> Option<u8> {
_ => None,
}
}

// Auto trait [`ParseHexStr`] for any type that implements [`Serializable`]
impl<T, const N: usize> ParseHexStr<N> for T where T: Serializable<N> {}
4 changes: 4 additions & 0 deletions dusk-bytes/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ pub trait DeserializableSlice<const N: usize>: Serializable<N> {
}
}
}

// Auto trait [`DeserializableSlice`] for any type that implements
// [`Serializable`]
impl<T, const N: usize> DeserializableSlice<N> for T where T: Serializable<N> {}
48 changes: 48 additions & 0 deletions dusk-bytes/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_bytes::{BadLength, InvalidChar, Serializable};

use dusk_bytes::HexDebug;
#[derive(HexDebug)]
pub struct Beef {}

#[derive(Debug)]
pub enum BeefError {
InvalidBytes,
UnexpectedEof,
CharNotValid(char, usize),
}

impl Serializable<2> for Beef {
type Error = BeefError;
fn from_bytes(buf: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
if buf[0] == 0xbe && buf[1] == 0xef {
Ok(Self {})
} else {
Err(BeefError::InvalidBytes)
}
}

fn to_bytes(&self) -> [u8; Self::SIZE] {
[0xbe, 0xef]
}
}

// Implementing DeserializableSlice requires `Error` to implements `BadLength`
// too
impl BadLength for BeefError {
fn bad_length(_found: usize, _expected: usize) -> Self {
Self::UnexpectedEof
}
}

// Implementing ParseHexStr requires `Error` to implements `InvalidChar` too
impl InvalidChar for BeefError {
fn invalid_char(ch: char, index: usize) -> Self {
Self::CharNotValid(ch, index)
}
}
41 changes: 41 additions & 0 deletions dusk-bytes/tests/parse_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

mod common;
use common::{Beef, BeefError};

use dusk_bytes::ParseHexStr;

#[test]
fn parse_correct_chars() -> Result<(), BeefError> {
let beef = Beef::from_hex_str("beef")?;

assert_eq!(format!("{:x}", beef), "beef");

Ok(())
}

#[test]
fn parse_invalid_chars() {
let beef = Beef::from_hex_str("beqf");

let result = matches!(beef, Err(BeefError::CharNotValid('q', 2)));
assert!(
result,
"Expected parse failing at index 2 for character 'q'"
)
}

#[test]
fn parse_wrong_chars() {
let beef = Beef::from_hex_str("abcd");

let result = matches!(beef, Err(BeefError::InvalidBytes));
assert!(
result,
"Expected parse failing because invalid bytes for Beef"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,10 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_bytes::{BadLength, DeserializableSlice, Serializable};
mod common;
use common::{Beef, BeefError};

use dusk_bytes::HexDebug;
#[derive(HexDebug)]
struct Beef {}

#[derive(Debug)]
enum BeefError {
InvalidBytes,
UnexpectedEof,
}

impl Serializable<2> for Beef {
type Error = BeefError;
fn from_bytes(buf: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
if buf[0] == 0xbe && buf[1] == 0xef {
Ok(Self {})
} else {
Err(BeefError::InvalidBytes)
}
}

fn to_bytes(&self) -> [u8; Self::SIZE] {
[0xbe, 0xef]
}
}

// This implements automatically `from_bytes_slice` and length checks
impl DeserializableSlice<2> for Beef {}

// Implementing DeserializableSlice requires `Error` to implements `BadLength`
// too
impl BadLength for BeefError {
fn bad_length(_found: usize, _expected: usize) -> Self {
Self::UnexpectedEof
}
}
use dusk_bytes::{DeserializableSlice, Serializable};

#[test]
fn expected_size() {
Expand Down

0 comments on commit 8775cd9

Please sign in to comment.