Enum lava_torrent::bencode::BencodeElem
source · pub enum BencodeElem {
String(String),
Bytes(Vec<u8>),
Integer(i64),
List(Vec<BencodeElem>),
Dictionary(HashMap<String, BencodeElem>),
RawDictionary(HashMap<Vec<u8>, BencodeElem>),
}
Expand description
Represent a single bencode element.
There are 4 variants in the spec, but this enum has 6 variants. The extra variants are
Bytes
(a sequence of bytes that does not represent a valid utf8
string, e.g. a SHA1 block hash), which is considered to be the
same as String
in the spec, and RawDictionary
, which has keys that are not
valid utf8 strings. They are best treated differently
in actual implementations to make things easier.
Note that the Integer
variant here uses i64
explicitly instead of using a type alias like
Integer
. The reasoning behind this is that if you have to handle
bencode directly then what you are doing is relatively low-level. In this case, exposing the
underlying type might actually be better.
Variants§
String(String)
Bytes(Vec<u8>)
Integer(i64)
List(Vec<BencodeElem>)
Dictionary(HashMap<String, BencodeElem>)
RawDictionary(HashMap<Vec<u8>, BencodeElem>)
Implementations§
source§impl BencodeElem
impl BencodeElem
sourcepub fn from_bytes<B>(bytes: B) -> Result<Vec<BencodeElem>, LavaTorrentError>where
B: AsRef<[u8]>,
pub fn from_bytes<B>(bytes: B) -> Result<Vec<BencodeElem>, LavaTorrentError>where B: AsRef<[u8]>,
Parse bytes
and return all BencodeElem
found.
If bytes
is empty, then Ok(vec)
will be returned, but
vec
would be empty as well.
If bytes
contains any malformed bencode, or if any other
error is encountered (e.g. IOError
), then Err(error)
will be returned.
sourcepub fn from_file<P>(path: P) -> Result<Vec<BencodeElem>, LavaTorrentError>where
P: AsRef<Path>,
pub fn from_file<P>(path: P) -> Result<Vec<BencodeElem>, LavaTorrentError>where P: AsRef<Path>,
Parse the content of the file at path
and return all BencodeElem
found.
If the file at path
is empty, then Ok(vec)
will be returned, but
vec
would be empty as well.
If the file at path
contains any malformed bencode, or if any other
error is encountered (e.g. IOError
), then Err(error)
will be returned.
source§impl BencodeElem
impl BencodeElem
sourcepub fn write_into<W>(&self, dst: &mut W) -> Result<(), LavaTorrentError>where
W: Write,
pub fn write_into<W>(&self, dst: &mut W) -> Result<(), LavaTorrentError>where W: Write,
Encode self
and write the result to dst
.
sourcepub fn write_into_file<P>(&self, path: P) -> Result<(), LavaTorrentError>where
P: AsRef<Path>,
pub fn write_into_file<P>(&self, path: P) -> Result<(), LavaTorrentError>where P: AsRef<Path>,
Encode self
and write the result to path
.
path
must be the path to a file.
“This function will create a file if it does not exist, and will truncate it if it does.”
Note: it is the client’s responsibility to ensure
that all directories in path
actually exist (e.g.
by calling create_dir_all
).
Trait Implementations§
source§impl Clone for BencodeElem
impl Clone for BencodeElem
source§fn clone(&self) -> BencodeElem
fn clone(&self) -> BencodeElem
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for BencodeElem
impl Debug for BencodeElem
source§impl Display for BencodeElem
impl Display for BencodeElem
source§impl<'a> From<&'a [u8]> for BencodeElem
impl<'a> From<&'a [u8]> for BencodeElem
source§fn from(val: &'a [u8]) -> BencodeElem
fn from(val: &'a [u8]) -> BencodeElem
source§impl<'a> From<&'a str> for BencodeElem
impl<'a> From<&'a str> for BencodeElem
source§fn from(val: &'a str) -> BencodeElem
fn from(val: &'a str) -> BencodeElem
source§impl From<String> for BencodeElem
impl From<String> for BencodeElem
source§fn from(val: String) -> BencodeElem
fn from(val: String) -> BencodeElem
source§impl From<i16> for BencodeElem
impl From<i16> for BencodeElem
source§fn from(val: i16) -> BencodeElem
fn from(val: i16) -> BencodeElem
source§impl From<i32> for BencodeElem
impl From<i32> for BencodeElem
source§fn from(val: i32) -> BencodeElem
fn from(val: i32) -> BencodeElem
source§impl From<i64> for BencodeElem
impl From<i64> for BencodeElem
source§fn from(val: i64) -> BencodeElem
fn from(val: i64) -> BencodeElem
source§impl From<i8> for BencodeElem
impl From<i8> for BencodeElem
source§fn from(val: i8) -> BencodeElem
fn from(val: i8) -> BencodeElem
source§impl From<u16> for BencodeElem
impl From<u16> for BencodeElem
source§fn from(val: u16) -> BencodeElem
fn from(val: u16) -> BencodeElem
source§impl From<u32> for BencodeElem
impl From<u32> for BencodeElem
source§fn from(val: u32) -> BencodeElem
fn from(val: u32) -> BencodeElem
source§impl From<u8> for BencodeElem
impl From<u8> for BencodeElem
source§fn from(val: u8) -> BencodeElem
fn from(val: u8) -> BencodeElem
source§impl PartialEq<BencodeElem> for BencodeElem
impl PartialEq<BencodeElem> for BencodeElem
source§fn eq(&self, other: &BencodeElem) -> bool
fn eq(&self, other: &BencodeElem) -> bool
self
and other
values to be equal, and is used
by ==
.