Comments (3)
You might have figured it out by now, but here is a small example of how to do that:
use deku::prelude::*;
use dbg_hex::dbg_hex;
pub const ENDIAN_CONSTANT: [u8; 4] = [0x12, 0x34, 0x56, 0x78];
pub const REVERSE_ENDIAN_CONSTANT: [u8; 4] = [0x78, 0x56, 0x34, 0x12];
/// 32-bit unsigned int
#[allow(non_camel_case_types)]
pub type uint = u32;
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(type = "u32", ctx = "endian: deku::ctx::Endian", endian = "endian")]
enum Endian {
Little = 0x12345678,
Big = 0x78563412,
}
impl Endian {
fn to_ctx(&self) -> deku::ctx::Endian {
match self {
Endian::Little => deku::ctx::Endian::Little,
Endian::Big => deku::ctx::Endian::Big,
}
}
}
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(endian = "big")]
pub struct Header {
/// Endianness tag
/// A value of 0x12345678 denotes little-endian, 0x78563412 denotes byte-swapped form.
endian_tag: Endian,
/// Size of the link section, or 0 if this file isn't statically linked
#[deku(endian = "endian_tag.to_ctx()")]
link_size: uint,
}
fn main() {
// Little
let bytes = hex::decode("12345678ff112233").unwrap();
let header = Header::from_bytes((&bytes, 0));
dbg_hex!(header);
// Big
let bytes = hex::decode("78563412ff112233").unwrap();
let header = Header::from_bytes((&bytes, 0));
dbg_hex!(header);
}
Output:
[src/[main.rs:43](http://main.rs:43/)] header = Ok(
(
(
[],
0x0,
),
Header {
endian_tag: Little,
link_size: 0x332211ff,
},
),
)
[src/[main.rs:48](http://main.rs:48/)] header = Ok(
(
(
[],
0x0,
),
Header {
endian_tag: Big,
link_size: 0xff112233,
},
),
)
from deku.
Closing, lmk if you have any more questions
from deku.
You might have figured it out by now, but here is a small example of how to do that:
use deku::prelude::*; use dbg_hex::dbg_hex; pub const ENDIAN_CONSTANT: [u8; 4] = [0x12, 0x34, 0x56, 0x78]; pub const REVERSE_ENDIAN_CONSTANT: [u8; 4] = [0x78, 0x56, 0x34, 0x12]; /// 32-bit unsigned int #[allow(non_camel_case_types)] pub type uint = u32; #[derive(Debug, DekuRead, DekuWrite)] #[deku(type = "u32", ctx = "endian: deku::ctx::Endian", endian = "endian")] enum Endian { Little = 0x12345678, Big = 0x78563412, } impl Endian { fn to_ctx(&self) -> deku::ctx::Endian { match self { Endian::Little => deku::ctx::Endian::Little, Endian::Big => deku::ctx::Endian::Big, } } } #[derive(Debug, DekuRead, DekuWrite)] #[deku(endian = "big")] pub struct Header { /// Endianness tag /// A value of 0x12345678 denotes little-endian, 0x78563412 denotes byte-swapped form. endian_tag: Endian, /// Size of the link section, or 0 if this file isn't statically linked #[deku(endian = "endian_tag.to_ctx()")] link_size: uint, } fn main() { // Little let bytes = hex::decode("12345678ff112233").unwrap(); let header = Header::from_bytes((&bytes, 0)); dbg_hex!(header); // Big let bytes = hex::decode("78563412ff112233").unwrap(); let header = Header::from_bytes((&bytes, 0)); dbg_hex!(header); }Output:
[src/[main.rs:43](http://main.rs:43/)] header = Ok( ( ( [], 0x0, ), Header { endian_tag: Little, link_size: 0x332211ff, }, ), ) [src/[main.rs:48](http://main.rs:48/)] header = Ok( ( ( [], 0x0, ), Header { endian_tag: Big, link_size: 0xff112233, }, ), )
You might have figured it out by now, but here is a small example of how to do that:
use deku::prelude::*; use dbg_hex::dbg_hex; pub const ENDIAN_CONSTANT: [u8; 4] = [0x12, 0x34, 0x56, 0x78]; pub const REVERSE_ENDIAN_CONSTANT: [u8; 4] = [0x78, 0x56, 0x34, 0x12]; /// 32-bit unsigned int #[allow(non_camel_case_types)] pub type uint = u32; #[derive(Debug, DekuRead, DekuWrite)] #[deku(type = "u32", ctx = "endian: deku::ctx::Endian", endian = "endian")] enum Endian { Little = 0x12345678, Big = 0x78563412, } impl Endian { fn to_ctx(&self) -> deku::ctx::Endian { match self { Endian::Little => deku::ctx::Endian::Little, Endian::Big => deku::ctx::Endian::Big, } } } #[derive(Debug, DekuRead, DekuWrite)] #[deku(endian = "big")] pub struct Header { /// Endianness tag /// A value of 0x12345678 denotes little-endian, 0x78563412 denotes byte-swapped form. endian_tag: Endian, /// Size of the link section, or 0 if this file isn't statically linked #[deku(endian = "endian_tag.to_ctx()")] link_size: uint, } fn main() { // Little let bytes = hex::decode("12345678ff112233").unwrap(); let header = Header::from_bytes((&bytes, 0)); dbg_hex!(header); // Big let bytes = hex::decode("78563412ff112233").unwrap(); let header = Header::from_bytes((&bytes, 0)); dbg_hex!(header); }Output:
[src/[main.rs:43](http://main.rs:43/)] header = Ok( ( ( [], 0x0, ), Header { endian_tag: Little, link_size: 0x332211ff, }, ), ) [src/[main.rs:48](http://main.rs:48/)] header = Ok( ( ( [], 0x0, ), Header { endian_tag: Big, link_size: 0xff112233, }, ), )
thanks,but i think this is not the best method.
from deku.
Related Issues (20)
- Missing support for core::net::Ipv4Addr/etc. HOT 1
- Replacing indices with references during parsing.
- Under / over-sized buffer parsing HOT 5
- Enable MIRI testing
- Help passing context around HOT 1
- Revisit enum id & type
- How to properly debug Deku input data? HOT 4
- Seemingly nonsensical vector count/read_size error HOT 2
- Conditional DekuWrite HOT 3
- Error: Parse("Too much data") using custom readers HOT 1
- Cant "count" from Some(field) directly - need as_ref().unwrap() HOT 1
- Passing a struct in ctx creates opaque error HOT 7
- Doc request: update for structs
- How to access enum-struct members? HOT 4
- Assert for tuple enum HOT 1
- Working with elements of Vec<StructUsingDeku> to get sum of their byte-sizes HOT 4
- Weird parsing with `bool` and non-aligned data. HOT 4
- Is there a way to pad by a variable number of bytes? HOT 3
- Roll a release? HOT 5
- Performance of `read_all` and `count`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deku.