Bencode

Enum Bencode 

Source
pub enum Bencode {
    Value(Vec<u8>),
    List(Vec<Bencode>),
    Dict(HashMap<Vec<u8>, Bencode>),
}
Expand description

A nested enum containing the parsed Bencode file structure.

Variants§

§

Value(Vec<u8>)

A value. (or a ‘leaf’ in tree terminology) which can be converted into a value with any of: as_string(), as_bytes(), or as_usize().

Possible options:

  • An Integer. eg: i123e. indicated by i and terminated by e.
  • A ByteString. eg: 3:cat. stars with an integer length, followed by : as a separator, then the bytes.
§

List(Vec<Bencode>)

A list of Bencode structs which can be destructured into a Vec with as_vec().

§

Dict(HashMap<Vec<u8>, Bencode>)

A map of bencoded values. All keys are Vec, and values are nested Bencode structs. This can be destructured with as_map().

Implementations§

Source§

impl Bencode

Source

pub fn as_usize(&self) -> Result<usize, BencodeParserError>

Attempt to parse a Bencode::Value variant into a usize.

Can return:

  • InvalidVariantMethod, if called on any of Bencode::{List, Dict}.
  • InvalidUTF8String, if the bytes cannot be parsed as a UTF8, ASCII string.
  • InvalidASCIIInteger, if the ASCII bytes aren’t a valid base 10 number.
Source

pub fn as_string(&self) -> Result<String, BencodeParserError>

Attempt to parse a Bencode::Value variant into a String.

Can return:

  • InvalidVariantMethod, if called on any of Bencode::{List, Dict}.
  • InvalidUTF8String, if the bytes cannot be parsed as a UTF8, ASCII string.
Source

pub fn as_bytes(&self) -> Result<&Vec<u8>, BencodeParserError>

Destructures a Bencode::Value into a Vec<u8>. This method will always succeed if applied to a Bencode::Value.

Can return:

  • InvalidVariantMethod, if called on any of Bencode::{List, Dict}.
Source

pub fn as_vec(&self) -> Result<&Vec<Bencode>, BencodeParserError>

Attempt to parse a Bencode::List into a Vec<Bencode>. This method will always succeed if applied to a Bencode::List.

Can return:

  • InvalidVariantMethod, if called on any of Bencode::{Value, Dict}.
Source

pub fn as_map(&self) -> Result<&HashMap<Vec<u8>, Bencode>, BencodeParserError>

Attempt to parse a Bencode::Dict into a HashMap<Vec<u8>, Bencode>. This method will always succeed if applied to a Bencode::Dict.

Can return:

  • InvalidVariantMethod, if called on any of Bencode::{Value, List}.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.