pub trait Parser {
// Required methods
fn peek(&self) -> Option<[u8; 1]>;
fn skip_bytes(&mut self, n: usize) -> XRPLCoreResult<&Self>;
fn read(&mut self, n: usize) -> XRPLCoreResult<Vec<u8>>;
fn read_uint8(&mut self) -> XRPLCoreResult<u8>;
fn read_uint16(&mut self) -> XRPLCoreResult<u16>;
fn read_uint32(&mut self) -> XRPLCoreResult<u32>;
fn is_end(&self, custom_end: Option<usize>) -> bool;
fn read_length_prefix(&mut self) -> XRPLCoreResult<usize>;
fn read_field_header(&mut self) -> XRPLCoreResult<FieldHeader>;
fn read_field(&mut self) -> XRPLCoreResult<FieldInstance>;
fn read_type<T: TryFromParser>(&mut self) -> XRPLCoreResult<T, T::Error>;
fn read_field_value<T: TryFromParser>(
&mut self,
field: &FieldInstance,
) -> XRPLCoreResult<T, T::Error>
where T::Error: From<XRPLCoreException>;
}
Required Methods§
Sourcefn peek(&self) -> Option<[u8; 1]>
fn peek(&self) -> Option<[u8; 1]>
Peek the first byte of the BinaryParser.
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
let test_bytes: &[u8] = &[0, 17, 34, 51, 68, 85, 102];
let binary_parser: BinaryParser = BinaryParser::from(test_bytes);
let first_byte: Option<[u8; 1]> = binary_parser.peek();
assert_eq!(Some([test_bytes[0]; 1]), first_byte);
Sourcefn skip_bytes(&mut self, n: usize) -> XRPLCoreResult<&Self>
fn skip_bytes(&mut self, n: usize) -> XRPLCoreResult<&Self>
Consume the first n bytes of the BinaryParser.
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
use xrpl::core::exceptions::XRPLCoreException;
let test_bytes: &[u8] = &[0, 17, 34, 51, 68, 85, 102];
let mut binary_parser: BinaryParser = BinaryParser::from(test_bytes);
match binary_parser.skip_bytes(4) {
Ok(parser) => assert_eq!(*parser, test_bytes[4..]),
Err(e) => match e {
XRPLCoreException::XRPLBinaryCodecError(XRPLBinaryCodecException::UnexpectedParserSkipOverflow {
max: _,
found: _,
}) => assert!(false),
_ => assert!(false)
}
}
Sourcefn read(&mut self, n: usize) -> XRPLCoreResult<Vec<u8>>
fn read(&mut self, n: usize) -> XRPLCoreResult<Vec<u8>>
Consume and return the first n bytes of the BinaryParser.
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
use xrpl::core::exceptions::XRPLCoreException;
let test_bytes: &[u8] = &[0, 17, 34, 51, 68, 85, 102];
let mut binary_parser: BinaryParser = BinaryParser::from(test_bytes);
match binary_parser.read(5) {
Ok(data) => assert_eq!(test_bytes[..5], data),
Err(e) => match e {
XRPLCoreException::XRPLBinaryCodecError(XRPLBinaryCodecException::UnexpectedParserSkipOverflow {
max: _,
found: _,
}) => assert!(false),
_ => assert!(false)
}
}
Sourcefn read_uint8(&mut self) -> XRPLCoreResult<u8>
fn read_uint8(&mut self) -> XRPLCoreResult<u8>
Read 1 byte from parser and return as unsigned int.
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
use xrpl::core::exceptions::XRPLCoreException;
let test_bytes: &[u8] = &[0, 17, 34, 51, 68, 85, 102];
let mut binary_parser: BinaryParser = BinaryParser::from(test_bytes);
match binary_parser.read_uint8() {
Ok(data) => assert_eq!(0, data),
Err(e) => match e {
XRPLCoreException::XRPLBinaryCodecError(XRPLBinaryCodecException::UnexpectedParserSkipOverflow {
max: _,
found: _,
}) => assert!(false),
_ => assert!(false)
}
}
Sourcefn read_uint16(&mut self) -> XRPLCoreResult<u16>
fn read_uint16(&mut self) -> XRPLCoreResult<u16>
Read 2 bytes from parser and return as unsigned int.
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
use xrpl::core::exceptions::XRPLCoreException;
let test_bytes: &[u8] = &[0, 17, 34, 51, 68, 85, 102];
let mut binary_parser: BinaryParser = BinaryParser::from(test_bytes);
match binary_parser.read_uint16() {
Ok(data) => assert_eq!(17, data),
Err(e) => match e {
XRPLCoreException::XRPLBinaryCodecError(XRPLBinaryCodecException::UnexpectedParserSkipOverflow {
max: _,
found: _,
}) => assert!(false),
_ => assert!(false)
}
}
Sourcefn read_uint32(&mut self) -> XRPLCoreResult<u32>
fn read_uint32(&mut self) -> XRPLCoreResult<u32>
Read 4 bytes from parser and return as unsigned int.
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
use xrpl::core::exceptions::XRPLCoreException;
let test_bytes: &[u8] = &[0, 17, 34, 51, 68, 85, 102];
let mut binary_parser: BinaryParser = BinaryParser::from(test_bytes);
match binary_parser.read_uint32() {
Ok(data) => assert_eq!(1122867, data),
Err(e) => match e {
XRPLCoreException::XRPLBinaryCodecError(XRPLBinaryCodecException::UnexpectedParserSkipOverflow {
max: _,
found: _,
}) => assert!(false),
_ => assert!(false)
}
}
Sourcefn is_end(&self, custom_end: Option<usize>) -> bool
fn is_end(&self, custom_end: Option<usize>) -> bool
Returns whether the binary parser has finished parsing (e.g. there is nothing left in the buffer that needs to be processed).
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
use xrpl::core::exceptions::XRPLCoreException;
extern crate alloc;
use alloc::vec;
let empty: &[u8] = &[];
let mut buffer: Vec<u8> = vec![];
let test_bytes: &[u8] = &[0, 17, 34, 51, 68, 85, 102];
let mut binary_parser: BinaryParser = BinaryParser::from(test_bytes);
while !binary_parser.is_end(None) {
match binary_parser.read(1) {
Ok(data) => buffer.extend_from_slice(&data),
Err(e) => match e {
XRPLCoreException::XRPLBinaryCodecError(XRPLBinaryCodecException::UnexpectedParserSkipOverflow {
max: _,
found: _,
}) => assert!(false),
_ => assert!(false)
}
}
}
assert_eq!(test_bytes, &buffer[..]);
// The BinaryParser is emptied as it is read.
assert_eq!(binary_parser, empty[..]);
Sourcefn read_length_prefix(&mut self) -> XRPLCoreResult<usize>
fn read_length_prefix(&mut self) -> XRPLCoreResult<usize>
Reads a variable length encoding prefix and returns the encoded length. The formula for decoding a length prefix is described in Length Prefixing.
See Length Prefixing:
<https://xrpl.org/serialization.html#length-prefixing>
§Examples
§Basic usage
use xrpl::core::binarycodec::BinaryParser;
use xrpl::core::Parser;
use xrpl::core::binarycodec::exceptions::XRPLBinaryCodecException;
use xrpl::core::exceptions::XRPLCoreException;
let test_bytes: &[u8] = &[6, 17, 34, 51, 68, 85, 102];
let mut binary_parser: BinaryParser = BinaryParser::from(test_bytes);
match binary_parser.read_length_prefix() {
Ok(data) => assert_eq!(6, data),
Err(e) => match e {
XRPLCoreException::XRPLBinaryCodecError(XRPLBinaryCodecException::UnexpectedLengthPrefixRange {
min: _, max: _
}) => assert!(false),
_ => assert!(false)
}
}
Sourcefn read_field_header(&mut self) -> XRPLCoreResult<FieldHeader>
fn read_field_header(&mut self) -> XRPLCoreResult<FieldHeader>
Reads field ID from BinaryParser and returns as a FieldHeader object.
Sourcefn read_field(&mut self) -> XRPLCoreResult<FieldInstance>
fn read_field(&mut self) -> XRPLCoreResult<FieldInstance>
Read the field ordinal at the head of the BinaryParser and return a FieldInstance object representing information about the field containedin the following bytes.
Sourcefn read_type<T: TryFromParser>(&mut self) -> XRPLCoreResult<T, T::Error>
fn read_type<T: TryFromParser>(&mut self) -> XRPLCoreResult<T, T::Error>
Read next bytes from BinaryParser as the given type.
Sourcefn read_field_value<T: TryFromParser>(
&mut self,
field: &FieldInstance,
) -> XRPLCoreResult<T, T::Error>
fn read_field_value<T: TryFromParser>( &mut self, field: &FieldInstance, ) -> XRPLCoreResult<T, T::Error>
Read value of the type specified by field from the BinaryParser.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl Parser for BinaryParser
Peek the first byte of the BinaryParser.