BdecodeNode

Enum BdecodeNode 

Source
pub enum BdecodeNode {
    Dict(Dict),
    List(List),
    Str(Str),
    Int(Int),
    End(End),
}
Expand description

用于存放解析后的数据

Variants§

§

Dict(Dict)

§

List(List)

§

Str(Str)

§

Int(Int)

§

End(End)

Implementations§

Source§

impl BdecodeNode

Source

pub fn new( token_idx: u32, tokens: Arc<Vec<BdecodeToken>>, buffer: Arc<Vec<u8>>, ) -> BdecodeNode

Source

pub fn as_int(&self) -> Result<i64, BdecodeError>

Source

pub fn as_str(&self) -> Cow<'_, [u8]>

Source

pub fn len(&self) -> usize

Examples found in repository?
examples/decode.rs (line 12)
3fn main() {
4    let buf = "d 2:k1 2:v1 2:k2 l i1e i2e e 3:k03 i3e 2:k4 d 2:k5 i5e 2:k6 i6e e e".replace(" ", "");
5
6    let root_node = BdecodeNode::parse_buffer(buf.into()).unwrap();
7    println!("{}", root_node.to_json());
8
9    let k2_node = root_node.dict_find(b"k2").unwrap();
10    println!("{}", k2_node.to_json());
11   
12    for i in 0..k2_node.len() {
13        let val = k2_node.list_item_as_int(i).unwrap();
14        println!("item_{} = {}", i, val)
15    }
16}
Source

pub fn list_item(&self, index: usize) -> BdecodeNode

Source

pub fn list_item_as_int(&self, index: usize) -> Result<i64, BdecodeError>

Examples found in repository?
examples/decode.rs (line 13)
3fn main() {
4    let buf = "d 2:k1 2:v1 2:k2 l i1e i2e e 3:k03 i3e 2:k4 d 2:k5 i5e 2:k6 i6e e e".replace(" ", "");
5
6    let root_node = BdecodeNode::parse_buffer(buf.into()).unwrap();
7    println!("{}", root_node.to_json());
8
9    let k2_node = root_node.dict_find(b"k2").unwrap();
10    println!("{}", k2_node.to_json());
11   
12    for i in 0..k2_node.len() {
13        let val = k2_node.list_item_as_int(i).unwrap();
14        println!("item_{} = {}", i, val)
15    }
16}
Source

pub fn list_item_as_str(&self, index: usize) -> Cow<'_, [u8]>

Source

pub fn dict_item(&self, index: usize) -> (BdecodeNode, BdecodeNode)

Source

pub fn dict_find(&self, key: &[u8]) -> Option<BdecodeNode>

Examples found in repository?
examples/decode.rs (line 9)
3fn main() {
4    let buf = "d 2:k1 2:v1 2:k2 l i1e i2e e 3:k03 i3e 2:k4 d 2:k5 i5e 2:k6 i6e e e".replace(" ", "");
5
6    let root_node = BdecodeNode::parse_buffer(buf.into()).unwrap();
7    println!("{}", root_node.to_json());
8
9    let k2_node = root_node.dict_find(b"k2").unwrap();
10    println!("{}", k2_node.to_json());
11   
12    for i in 0..k2_node.len() {
13        let val = k2_node.list_item_as_int(i).unwrap();
14        println!("item_{} = {}", i, val)
15    }
16}
Source

pub fn dict_find_as_str(&self, key: &[u8]) -> Option<Cow<'_, [u8]>>

Source

pub fn dict_find_as_int(&self, key: &[u8]) -> Option<i64>

Source

pub fn dict_find_as_list(&self, key: &[u8]) -> Option<Vec<BdecodeNode>>

Source

pub fn dict_find_as_dict( &self, key: &[u8], ) -> Option<HashMap<Cow<'_, [u8]>, BdecodeNode>>

Source

pub fn parse( buffer: Vec<u8>, depth_limit: Option<usize>, token_limit: Option<i32>, ) -> Result<Self, BdecodeError>

Source

pub fn parse_buffer(buffer: Vec<u8>) -> Result<Self, BdecodeError>

Examples found in repository?
examples/decode.rs (line 6)
3fn main() {
4    let buf = "d 2:k1 2:v1 2:k2 l i1e i2e e 3:k03 i3e 2:k4 d 2:k5 i5e 2:k6 i6e e e".replace(" ", "");
5
6    let root_node = BdecodeNode::parse_buffer(buf.into()).unwrap();
7    println!("{}", root_node.to_json());
8
9    let k2_node = root_node.dict_find(b"k2").unwrap();
10    println!("{}", k2_node.to_json());
11   
12    for i in 0..k2_node.len() {
13        let val = k2_node.list_item_as_int(i).unwrap();
14        println!("item_{} = {}", i, val)
15    }
16}
Source

pub fn to_json(&self) -> String

Examples found in repository?
examples/decode.rs (line 7)
3fn main() {
4    let buf = "d 2:k1 2:v1 2:k2 l i1e i2e e 3:k03 i3e 2:k4 d 2:k5 i5e 2:k6 i6e e e".replace(" ", "");
5
6    let root_node = BdecodeNode::parse_buffer(buf.into()).unwrap();
7    println!("{}", root_node.to_json());
8
9    let k2_node = root_node.dict_find(b"k2").unwrap();
10    println!("{}", k2_node.to_json());
11   
12    for i in 0..k2_node.len() {
13        let val = k2_node.list_item_as_int(i).unwrap();
14        println!("item_{} = {}", i, val)
15    }
16}

Trait Implementations§

Source§

impl Clone for BdecodeNode

Source§

fn clone(&self) -> BdecodeNode

Returns a duplicate 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 BdecodeNode

Source§

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

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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, 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.