Skip to content

Commit 08050b4

Browse files
authored
Merge pull request #16 from hashmismatch/issue_10
Basic error_chain compatibility
2 parents 770542b + e8e9083 commit 08050b4

File tree

6 files changed

+109
-7
lines changed

6 files changed

+109
-7
lines changed

packed_struct/src/packing.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ pub enum PackingError {
4242
BufferSizeMismatch { expected: usize, actual: usize }
4343
}
4444

45+
impl Display for PackingError {
46+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47+
write!(f, "{:?}", self)
48+
}
49+
}
50+
51+
#[cfg(feature="std")]
52+
impl ::std::error::Error for PackingError {
53+
fn description(&self) -> &str {
54+
match *self {
55+
PackingError::InvalidValue => "Invalid value",
56+
PackingError::BitsError => "Bits error",
57+
PackingError::BufferTooSmall => "Buffer too small",
58+
PackingError::BufferSizeMismatch { .. } => "Buffer size mismatched",
59+
PackingError::NotImplemented => "Not implemented"
60+
}
61+
}
62+
}
63+
4564

4665
macro_rules! packing_slice {
4766
($T: path; $num_bytes: expr) => (

packed_struct_codegen/src/common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ pub fn collections_prefix() -> syn::Ty {
1010
syn::parse_type("::collections").unwrap()
1111
}
1212

13+
#[cfg(feature="std")]
14+
pub fn result_type() -> syn::Ty {
15+
syn::parse_type("::std::result::Result").expect("result type parse error")
16+
}
17+
18+
#[cfg(not(feature="std"))]
19+
pub fn result_type() -> syn::Ty {
20+
syn::parse_type("::core::result::Result").expect("result type parse error")
21+
}
1322

1423

1524

packed_struct_codegen/src/pack_codegen.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ pub fn derive_pack(parsed: &PackStruct) -> quote::Tokens {
8989

9090
}
9191

92+
let result_ty = result_type();
93+
9294
quote! {
9395
#type_documentation
9496
impl #impl_generics ::packed_struct::PackedStruct<[u8; #num_bytes]> for #name #ty_generics #where_clause {
@@ -106,7 +108,7 @@ pub fn derive_pack(parsed: &PackStruct) -> quote::Tokens {
106108

107109
#[inline]
108110
#[allow(unused_imports, unused_parens)]
109-
fn unpack(src: &[u8; #num_bytes]) -> Result<#name, ::packed_struct::PackingError> {
111+
fn unpack(src: &[u8; #num_bytes]) -> #result_ty <#name, ::packed_struct::PackingError> {
110112
use ::packed_struct::*;
111113

112114
#(#unpack_fields)*
@@ -127,7 +129,7 @@ pub fn derive_pack(parsed: &PackStruct) -> quote::Tokens {
127129
impl #impl_generics ::packed_struct::PackedStructSlice for #name #ty_generics #where_clause {
128130
#[inline]
129131
#[allow(unused_imports)]
130-
fn pack_to_slice(&self, output: &mut [u8]) -> Result<(), ::packed_struct::PackingError> {
132+
fn pack_to_slice(&self, output: &mut [u8]) -> #result_ty <(), ::packed_struct::PackingError> {
131133
use ::packed_struct::*;
132134

133135
if output.len() != #num_bytes {
@@ -140,7 +142,7 @@ pub fn derive_pack(parsed: &PackStruct) -> quote::Tokens {
140142

141143
#[inline]
142144
#[allow(unused_imports)]
143-
fn unpack_from_slice(src: &[u8]) -> Result<Self, ::packed_struct::PackingError> {
145+
fn unpack_from_slice(src: &[u8]) -> #result_ty <Self, ::packed_struct::PackingError> {
144146
use ::packed_struct::*;
145147

146148
if src.len() != #num_bytes {
@@ -325,6 +327,7 @@ fn pack_field(name: &syn::Ident, field: &FieldRegular) -> quote::Tokens {
325327
fn unpack_field(field: &FieldRegular) -> quote::Tokens {
326328
let wrappers: Vec<_> = field.serialization_wrappers.iter().rev().cloned().collect();
327329

330+
let result_ty = result_type();
328331
let mut unpack = quote! { bytes };
329332

330333
let mut i = 0;
@@ -336,7 +339,7 @@ fn unpack_field(field: &FieldRegular) -> quote::Tokens {
336339
use ::packed_struct::types::*;
337340
use ::packed_struct::types::bits::*;
338341

339-
let res: Result<#endian <_, _, #integer >, PackingError> = <#endian <_, _, _>>::unpack(& #unpack );
342+
let res: #result_ty <#endian <_, _, #integer >, PackingError> = <#endian <_, _, _>>::unpack(& #unpack );
340343
let unpacked = try!(res);
341344
**unpacked
342345
};
@@ -361,7 +364,7 @@ fn unpack_field(field: &FieldRegular) -> quote::Tokens {
361364
use ::packed_struct::types::*;
362365
use ::packed_struct::types::bits::*;
363366

364-
let res: Result<#endian <_, _, #integer_ty >, PackingError> = <#endian <_, _, #integer_ty >>::unpack(& #unpack );
367+
let res: #result_ty <#endian <_, _, #integer_ty >, PackingError> = <#endian <_, _, #integer_ty >>::unpack(& #unpack );
365368
let unpacked = try!(res);
366369
*unpacked
367370
};
@@ -383,4 +386,4 @@ fn unpack_field(field: &FieldRegular) -> quote::Tokens {
383386
}
384387

385388
unpack
386-
}
389+
}

packed_struct_codegen/src/pack_codegen_docs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub fn struct_runtime_formatter(parsed: &PackStruct) -> quote::Tokens {
5858
}
5959

6060
let num_fields = debug_fields.len();
61+
let result_ty = result_type();
6162

6263
quote! {
6364
#[doc(hidden)]
@@ -67,7 +68,7 @@ pub fn struct_runtime_formatter(parsed: &PackStruct) -> quote::Tokens {
6768

6869
#[allow(unused_imports)]
6970
impl #impl_generics ::packed_struct::debug_fmt::PackedStructDebug for #name #ty_generics #where_clause {
70-
fn fmt_fields(&self, fmt: &mut #stdlib_prefix::fmt::Formatter) -> Result<(), #stdlib_prefix::fmt::Error> {
71+
fn fmt_fields(&self, fmt: &mut #stdlib_prefix::fmt::Formatter) -> #result_ty <(), #stdlib_prefix::fmt::Error> {
7172
use ::packed_struct::PackedStruct;
7273

7374
let fields = #debug_fields_fn(self);

packed_struct_tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ publish = false
77
[dependencies]
88
packed_struct = "^0.2.0"
99
packed_struct_codegen = "^0.2.0"
10+
error-chain = "0.11.0"
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
extern crate packed_struct;
2+
#[macro_use]
3+
extern crate packed_struct_codegen;
4+
5+
#[macro_use]
6+
extern crate error_chain;
7+
8+
mod errors {
9+
error_chain! {
10+
foreign_links {
11+
PackedStruct(::packed_struct::PackingError);
12+
}
13+
}
14+
}
15+
16+
use errors::*;
17+
18+
use packed_struct::prelude::*;
19+
20+
#[derive(Debug, PackedStruct)]
21+
#[packed_struct(bit_numbering = "msb0")]
22+
pub struct LedDriverConfig {
23+
#[packed_field(bits = "46:47")]
24+
lodvth: Integer<u8, ::packed_bits::Bits2>,
25+
#[packed_field(bits = "44:45")]
26+
sel_td0: Integer<u8, ::packed_bits::Bits2>,
27+
#[packed_field(bits = "43")]
28+
sel_gdly: Integer<u8, ::packed_bits::Bits1>,
29+
#[packed_field(bits = "42")]
30+
xrefresh: Integer<u8, ::packed_bits::Bits1>,
31+
#[packed_field(bits = "41")]
32+
sel_gck_edge: Integer<u8, ::packed_bits::Bits1>,
33+
#[packed_field(bits = "40")]
34+
sel_pchg: Integer<u8, ::packed_bits::Bits1>,
35+
#[packed_field(bits = "39")]
36+
espwm: Integer<u8, ::packed_bits::Bits1>,
37+
#[packed_field(bits = "38")]
38+
lgse3: Integer<u8, ::packed_bits::Bits1>,
39+
#[packed_field(bits = "37")]
40+
sel_sck_edge: Integer<u8, ::packed_bits::Bits1>,
41+
#[packed_field(bits = "34:36")]
42+
lgse1: Integer<u8, ::packed_bits::Bits3>,
43+
#[packed_field(bits = "25:33", endian = "msb")]
44+
ccb: Integer<u16, ::packed_bits::Bits9>,
45+
#[packed_field(bits = "16:24", endian = "msb")]
46+
ccg: Integer<u16, ::packed_bits::Bits9>,
47+
#[packed_field(bits = "7:15", endian = "msb")]
48+
ccr: Integer<u16, ::packed_bits::Bits9>,
49+
#[packed_field(bits = "4:6")]
50+
bc: Integer<u8, ::packed_bits::Bits3>,
51+
#[packed_field(bits = "3")]
52+
poker_trans_mode: Integer<u8, ::packed_bits::Bits1>,
53+
#[packed_field(bits = "0:2")]
54+
lgse2: Integer<u8, ::packed_bits::Bits3>,
55+
}
56+
57+
#[test]
58+
#[cfg(test)]
59+
fn test_packed_struct_issue_10() {
60+
run().unwrap();
61+
}
62+
63+
fn run() -> Result<()> {
64+
let data = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
65+
let unpacked = LedDriverConfig::unpack(&data)
66+
.chain_err(|| "unable to unpack LED driver config")?;
67+
68+
Ok(())
69+
}

0 commit comments

Comments
 (0)