Enum bencodex::BNode

source ·
pub enum BNode {
    Integer(i64),
    Bytes(Vec<u8>),
    List(BList),
    Dict(BDict),
}

Variants§

§

Integer(i64)

§

Bytes(Vec<u8>)

§

List(BList)

§

Dict(BDict)

Implementations§

source§

impl BNode

source

pub fn serialize<W>(&self, buf: &mut W) -> Result<usize>where W: Write,

Examples found in repository?
examples/ser/main.rs (line 11)
3
4
5
6
7
8
9
10
11
12
13
fn main() {
    let mut dict = BDict::new();
    dict.insert("bar".to_string(), "spam".into());
    dict.insert("foo".to_string(), 42.into());

    let bnode: BNode = dict.into();

    let mut buf = vec![];
    bnode.serialize(&mut buf).unwrap();
    assert_eq!("d3:bar4:spam3:fooi42ee".as_bytes(), buf)
}
More examples
Hide additional examples
examples/bufwriter/main.rs (line 19)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    let mut dict = BDict::new();
    dict.insert("bar".to_string(), "spam".into());
    dict.insert("foo".to_string(), 42.into());

    let bnode: BNode = dict.into();

    let mut file = File::create(
        env::current_dir()
            .unwrap()
            .join("examples")
            .join("bufwriter")
            .join("bufwriter-test.torrent"),
    )
    .unwrap();
    bnode.serialize(&mut file).unwrap();
}
source

pub fn as_integer(&self) -> Result<&i64, String>

Examples found in repository?
examples/de/main.rs (line 6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    let b = "d3:inti233e3:lstl7:bencodeee";
    let node = bencodex::parse(&mut b.bytes()).unwrap();

    let dict = node.as_dict().unwrap();
    let int = dict.get("int").unwrap().as_integer().unwrap();

    assert_eq!(int, &233);
    let list = dict.get("lst").unwrap().as_list().unwrap();
    assert_eq!(list.len(), 1);
    assert_eq!(
        list.get(0).unwrap().as_bytes().unwrap(),
        "bencode".as_bytes()
    );
}
More examples
Hide additional examples
examples/bufreader/main.rs (line 41)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let f = File::open(
        env::current_dir()
            .unwrap()
            .join("examples")
            .join("bufreader")
            .join("bufreader-test.torrent"),
    )
    .unwrap();
    let reader = BufReader::new(f);

    let mut adapter = Adapter {
        bytes: reader.bytes(),
    };
    let bnode = bencodex::parse(&mut adapter).unwrap();
    let dict = bnode.as_dict().unwrap();
    assert_eq!(
        dict.get("bar").unwrap().as_bytes().unwrap(),
        "spam".as_bytes()
    );
    assert_eq!(dict.get("foo").unwrap().as_integer().unwrap(), &42);
}
source

pub fn as_bytes(&self) -> Result<&[u8], String>

Examples found in repository?
examples/de/main.rs (line 12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    let b = "d3:inti233e3:lstl7:bencodeee";
    let node = bencodex::parse(&mut b.bytes()).unwrap();

    let dict = node.as_dict().unwrap();
    let int = dict.get("int").unwrap().as_integer().unwrap();

    assert_eq!(int, &233);
    let list = dict.get("lst").unwrap().as_list().unwrap();
    assert_eq!(list.len(), 1);
    assert_eq!(
        list.get(0).unwrap().as_bytes().unwrap(),
        "bencode".as_bytes()
    );
}
More examples
Hide additional examples
examples/bufreader/main.rs (line 38)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let f = File::open(
        env::current_dir()
            .unwrap()
            .join("examples")
            .join("bufreader")
            .join("bufreader-test.torrent"),
    )
    .unwrap();
    let reader = BufReader::new(f);

    let mut adapter = Adapter {
        bytes: reader.bytes(),
    };
    let bnode = bencodex::parse(&mut adapter).unwrap();
    let dict = bnode.as_dict().unwrap();
    assert_eq!(
        dict.get("bar").unwrap().as_bytes().unwrap(),
        "spam".as_bytes()
    );
    assert_eq!(dict.get("foo").unwrap().as_integer().unwrap(), &42);
}
source

pub fn as_list(&self) -> Result<&[BNode], String>

Examples found in repository?
examples/de/main.rs (line 9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    let b = "d3:inti233e3:lstl7:bencodeee";
    let node = bencodex::parse(&mut b.bytes()).unwrap();

    let dict = node.as_dict().unwrap();
    let int = dict.get("int").unwrap().as_integer().unwrap();

    assert_eq!(int, &233);
    let list = dict.get("lst").unwrap().as_list().unwrap();
    assert_eq!(list.len(), 1);
    assert_eq!(
        list.get(0).unwrap().as_bytes().unwrap(),
        "bencode".as_bytes()
    );
}
source

pub fn as_dict(&self) -> Result<&BDict, String>

Examples found in repository?
examples/de/main.rs (line 5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    let b = "d3:inti233e3:lstl7:bencodeee";
    let node = bencodex::parse(&mut b.bytes()).unwrap();

    let dict = node.as_dict().unwrap();
    let int = dict.get("int").unwrap().as_integer().unwrap();

    assert_eq!(int, &233);
    let list = dict.get("lst").unwrap().as_list().unwrap();
    assert_eq!(list.len(), 1);
    assert_eq!(
        list.get(0).unwrap().as_bytes().unwrap(),
        "bencode".as_bytes()
    );
}
More examples
Hide additional examples
examples/bufreader/main.rs (line 36)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    let f = File::open(
        env::current_dir()
            .unwrap()
            .join("examples")
            .join("bufreader")
            .join("bufreader-test.torrent"),
    )
    .unwrap();
    let reader = BufReader::new(f);

    let mut adapter = Adapter {
        bytes: reader.bytes(),
    };
    let bnode = bencodex::parse(&mut adapter).unwrap();
    let dict = bnode.as_dict().unwrap();
    assert_eq!(
        dict.get("bar").unwrap().as_bytes().unwrap(),
        "spam".as_bytes()
    );
    assert_eq!(dict.get("foo").unwrap().as_integer().unwrap(), &42);
}

Trait Implementations§

source§

impl Clone for BNode

source§

fn clone(&self) -> BNode

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for BNode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for BNode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl From<&[u8]> for BNode

source§

fn from(value: &[u8]) -> Self

Converts to this type from the input type.
source§

impl From<&str> for BNode

source§

fn from(value: &str) -> Self

Converts to this type from the input type.
source§

impl From<BTreeMap<String, BNode, Global>> for BNode

source§

fn from(value: BTreeMap<String, BNode>) -> Self

Converts to this type from the input type.
source§

impl From<String> for BNode

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

impl From<Vec<BNode, Global>> for BNode

source§

fn from(value: Vec<BNode>) -> Self

Converts to this type from the input type.
source§

impl From<Vec<u8, Global>> for BNode

source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
source§

impl From<i64> for BNode

source§

fn from(value: i64) -> Self

Converts to this type from the input type.
source§

impl PartialEq<BNode> for BNode

source§

fn eq(&self, other: &BNode) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for BNode

source§

impl StructuralEq for BNode

source§

impl StructuralPartialEq for BNode

Auto Trait Implementations§

§

impl RefUnwindSafe for BNode

§

impl Send for BNode

§

impl Sync for BNode

§

impl Unpin for BNode

§

impl UnwindSafe for BNode

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.