-
-
Notifications
You must be signed in to change notification settings - Fork 55
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 #335 from IniterWorker/feature/reader-catch-all
Add `default` attribute for DekuRead enums
- Loading branch information
Showing
7 changed files
with
170 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use deku::prelude::*; | ||
use hexlit::hex; | ||
use std::convert::TryFrom; | ||
use std::convert::TryInto; | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, Debug, DekuWrite, DekuRead)] | ||
#[deku(type = "u8")] | ||
#[non_exhaustive] | ||
#[repr(u8)] | ||
pub enum DekuTest { | ||
/// A | ||
#[deku(id = "1")] | ||
A = 0, | ||
/// B | ||
#[deku(id = "2")] | ||
B = 1, | ||
/// C | ||
#[deku(id = "3", default)] | ||
C = 2, | ||
} | ||
|
||
fn main() { | ||
let input = hex!("0A").to_vec(); | ||
let output = hex!("03").to_vec(); | ||
|
||
let ret_read = DekuTest::try_from(input.as_slice()).unwrap(); | ||
assert_eq!(DekuTest::C, ret_read); | ||
let ret_write: Vec<u8> = ret_read.try_into().unwrap(); | ||
assert_eq!(output.to_vec(), ret_write); | ||
} |
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,86 @@ | ||
#[cfg(test)] | ||
mod test { | ||
use deku::prelude::*; | ||
use std::convert::TryFrom; | ||
use std::convert::TryInto; | ||
|
||
/// Basic test struct | ||
#[derive(Clone, Copy, PartialEq, Eq, Debug, DekuWrite, DekuRead)] | ||
#[deku(type = "u8")] | ||
#[non_exhaustive] | ||
#[repr(u8)] | ||
pub enum BasicMapping { | ||
/// A | ||
A = 0, | ||
/// B | ||
B = 1, | ||
/// C | ||
#[deku(default)] | ||
C = 2, | ||
} | ||
|
||
/// Advanced test struct | ||
#[derive(Clone, Copy, PartialEq, Eq, Debug, DekuWrite, DekuRead)] | ||
#[deku(type = "u8")] | ||
#[non_exhaustive] | ||
#[repr(u8)] | ||
pub enum AdvancedRemapping { | ||
/// A | ||
#[deku(id = "1")] | ||
A = 0, | ||
/// B | ||
#[deku(id = "2")] | ||
B = 1, | ||
/// C | ||
#[deku(id = "3", default)] | ||
C = 2, | ||
} | ||
|
||
#[test] | ||
fn test_basic_a() { | ||
let input = [0u8]; | ||
let ret_read = BasicMapping::try_from(input.as_slice()).unwrap(); | ||
assert_eq!(BasicMapping::A, ret_read); | ||
let ret_write: Vec<u8> = ret_read.try_into().unwrap(); | ||
assert_eq!(input.to_vec(), ret_write); | ||
} | ||
|
||
#[test] | ||
fn test_basic_c() { | ||
let input = [2u8]; | ||
let ret_read = BasicMapping::try_from(input.as_slice()).unwrap(); | ||
assert_eq!(BasicMapping::C, ret_read); | ||
let ret_write: Vec<u8> = ret_read.try_into().unwrap(); | ||
assert_eq!(input.to_vec(), ret_write); | ||
} | ||
|
||
#[test] | ||
fn test_basic_pattern() { | ||
let input = [10u8]; | ||
let output = [BasicMapping::C as u8]; | ||
let ret_read = BasicMapping::try_from(input.as_slice()).unwrap(); | ||
assert_eq!(BasicMapping::C, ret_read); | ||
let ret_write: Vec<u8> = ret_read.try_into().unwrap(); | ||
assert_eq!(output.to_vec(), ret_write); | ||
} | ||
|
||
#[test] | ||
fn test_advanced_remapping() { | ||
let input = [1u8]; | ||
let output = [1u8]; | ||
let ret_read = AdvancedRemapping::try_from(input.as_slice()).unwrap(); | ||
assert_eq!(AdvancedRemapping::A, ret_read); | ||
let ret_write: Vec<u8> = ret_read.try_into().unwrap(); | ||
assert_eq!(output.to_vec(), ret_write); | ||
} | ||
|
||
#[test] | ||
fn test_advanced_remapping_default_field() { | ||
let input = [10u8]; | ||
let output = [3u8]; | ||
let ret_read = AdvancedRemapping::try_from(input.as_slice()).unwrap(); | ||
assert_eq!(AdvancedRemapping::C, ret_read); | ||
let ret_write: Vec<u8> = ret_read.try_into().unwrap(); | ||
assert_eq!(output.to_vec(), ret_write); | ||
} | ||
} |
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,12 @@ | ||
use deku::prelude::*; | ||
|
||
#[derive(DekuRead)] | ||
#[deku(type = "u8")] | ||
enum Test1 { | ||
#[deku(default)] | ||
A = 1, | ||
#[deku(default)] | ||
B = 2, | ||
} | ||
|
||
fn main() {} |
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,5 @@ | ||
error: DekuRead: `default` must be specified only once | ||
--> tests/test_compile/cases/catch_all_multiple.rs:9:5 | ||
| | ||
9 | B = 2, | ||
| ^ |