keichi / binary-parser

A blazing-fast declarative parser builder for binary data
MIT License
864 stars 134 forks source link

Error handler #190

Closed TheMychenik closed 2 years ago

TheMychenik commented 2 years ago

How to explain, I would like to be able to get the maximum pointer of the parser to compare with buffer that i try to parse example:

let incomingArray = new Parser()
    .uint8('someData1')
    .skip(7)
    .uint8('someData2')
    .skip(7)
    .uint8('someData3')
// incomingArray.getMaxSize() >> 17
// msg.length >> 16
if (msg.length <= incomingArray.getMaxSize())
// incomingArray.parse(msg)
else
// skip, log or what you want

or not to throw range error and parse available size

let incomingArray = new Parser()
    .parseAsManyAsYouCan()
    .uint8('someData1')
    .skip(7)
    .uint8('someData2')
    .skip(7)
    .uint8('someData3')
// incomingArray.parse(msg) >> msg { someData: '1', someData2: '2' } 
// no someData3 but no err
keichi commented 2 years ago

Is incomingArray.sizeOf() what you need?

TheMychenik commented 2 years ago

Is incomingArray.sizeOf() what you need?

Exactly! Thanks, I didn't find it in the readme

keichi commented 2 years ago

Yes, I will document the method in the readme. Thanks for pointing it out!