Skip to content

Commit ea0796b

Browse files
committed
Parse chunk type from binary
1 parent b307266 commit ea0796b

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

Cargo.lock

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ futures = "0.3"
1818
lazy_static = "1"
1919
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
2020
uuid = "1"
21-
rcx = "0.1.0"
21+
rcx = "0.1.1"
2222
nom = "7.1.3"
2323

2424
[dev-dependencies]
@@ -28,5 +28,5 @@ hex-literal = "0.4.1"
2828
# version = "0.3"
2929
# path = "../lego-powered-up/lego-powered-up"
3030

31-
[patch.crates-io]
32-
rcx = { path = "../rcx/rcx" }
31+
# [patch.crates-io]
32+
# rcx = { path = "../rcx/rcx" }

src/rcx/binfmt.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl RcxBin {
7171

7272
#[derive(Clone, Debug, PartialEq, Eq)]
7373
pub struct Chunk {
74-
pub ty: u8,
74+
pub ty: ChunkType,
7575
pub number: u8,
7676
pub length: u16,
7777
pub data: Vec<u8>,
@@ -81,7 +81,7 @@ fn parse_chunk(i: &[u8]) -> IResult<&[u8], Chunk> {
8181
let read_u16 = nom::number::complete::u16(Endianness::Little);
8282
let read_u8 = nom::number::complete::u8;
8383

84-
let (i, ty) = read_u8(i)?;
84+
let (i, ty) = ChunkType::parse(i)?;
8585
let (i, number) = read_u8(i)?;
8686
let (i, length) = read_u16(i)?;
8787
let (i, data) = nom::bytes::complete::take(length)(i)?;
@@ -97,6 +97,36 @@ fn parse_chunk(i: &[u8]) -> IResult<&[u8], Chunk> {
9797
))
9898
}
9999

100+
#[derive(Clone, Debug, PartialEq, Eq)]
101+
#[repr(u8)]
102+
pub enum ChunkType {
103+
Task = 0,
104+
SubChunk,
105+
Sound,
106+
Animation,
107+
Count,
108+
}
109+
110+
impl ChunkType {
111+
pub fn parse(i: &[u8]) -> IResult<&[u8], Self> {
112+
let (i, ty) = nom::number::complete::u8(i)?;
113+
let ty = match ty {
114+
0 => Self::Task,
115+
1 => Self::SubChunk,
116+
2 => Self::Sound,
117+
3 => Self::Animation,
118+
4 => Self::Count,
119+
_ => {
120+
return Err(nom::Err::Failure(nom::error::Error {
121+
input: i,
122+
code: nom::error::ErrorKind::Verify,
123+
}));
124+
}
125+
};
126+
Ok((i, ty))
127+
}
128+
}
129+
100130
#[derive(Clone, Debug, PartialEq, Eq)]
101131
pub struct Symbol {
102132
pub ty: u8,
@@ -183,7 +213,7 @@ mod test {
183213
target_type: 0,
184214
reserved: 0,
185215
chunks: vec![Chunk {
186-
ty: 0,
216+
ty: ChunkType::Task,
187217
number: 0,
188218
length: 20,
189219
data: vec![

0 commit comments

Comments
 (0)