Struct lazy_bencoding::BEncoded
[−]
[src]
pub struct BEncoded<'a> { pub data: &'a [u8], pub depth: i16, }
Token iterator instance
Example
extern crate lazy_bencoding; use lazy_bencoding::*; let bencoded = BEncoded::new(&data[..]); let name = bencoded.get(b"info").get(b"name").get_utf8_string();
You may now:
- iterate over
bencoded
to process allToken
in order, or - traverse the structure using
get()
andget_utf8_string()
.
Fields
data | Next data to parse, only a cheap reference and updated with
each |
depth | Keeping track of the depth, for:
|
Methods
impl<'a> BEncoded<'a>
fn new(data: &'a [u8]) -> Self
Construct from a pointer to data
fn empty() -> BEncoded<'static>
Construct with empty data, used for when traversing fails
impl<'a> BEncoded<'a>
fn is_dict(&self) -> bool
Is the remaining data
a dict?
fn is_list(&self) -> bool
Is the remaining data
a list?
fn get_byte_string(&self) -> Option<&'a [u8]>
Is the remaining data
a string? Then get it as a cheap slice.
fn get_utf8_string(&self) -> Option<&'a str>
Is the remaining data
a string? Then get it as checked UTF-8.
fn get_integer(&self) -> Option<i64>
Is the remaining data
an integer? If so, parse and return it.
fn this_and_rest(self) -> Option<(BEncoded<'a>, &'a [u8])>
Restricts the BEncoded<'a>
to a single item (string,
integer, list, or dict). Also returns the trailing data.
fn list(&self) -> ListIterator
If the remaining data is a list, return value fulfills
Iterator<Item=BEncoded<'a>>
fn dict(&self) -> DictIterator
If the remaining data is a dict, return value fulfills
Iterator<Item=(BEncoded<'a>, BEncoded<'a>)>
fn get(&'a self, wanted_key: &'a [u8]) -> Self
From a dict()
, get the value of key
wanted_key
.
In case the key is not present, we return an empty instance. This allows chaining these calls conveniently:
let bencoded = BEncoded::new(b"d1:ad1:ad1:a4:spame1:bd1:a4:eggseee"); assert_eq!(bencoded.get(b"a").get(b"b").get(b"a").get_utf8_string(), Some("eggs"));
Trait Implementations
impl<'a> Iterator for BEncoded<'a>
The token stream. Advances self.data
and updates self.depth
.