-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from dusk-network/auto-trait
Add auto trait and bump to v0.1.1
- Loading branch information
Showing
7 changed files
with
101 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters