[−][src]Struct ben::Node
Methods
impl<'a> Node<'a>
[src]
pub fn parse(buf: &'a [u8]) -> Result<Self>
[src]
Parse given bencoded bytes.
Example:
use ben::Node; let bytes = b"11:Hello World"; let node = Node::parse(bytes).unwrap(); assert_eq!("Hello World", node.as_str().unwrap());
pub fn parse_in(buf: &'a [u8], tokens: &'a mut Vec<Token>) -> Result<Self>
[src]
Parse given bencoded bytes using a given token buffer. It helps you reuse the buffer next time.
Example:
use ben::Node; let mut v = vec![]; let values: &[&[u8]] = &[b"5:Hello", b"5:World"]; for bytes in values { let node = Node::parse_in(bytes, &mut v).unwrap(); assert!(node.is_str()); }
pub fn parse_prefix(buf: &'a [u8]) -> Result<(Self, usize)>
[src]
Parse given bencoded bytes from the beginning and returns index where one root object is parsed successfully.
Example:
use ben::Node; let bytes = b"5:Hello World"; let (node, i) = Node::parse_prefix(bytes).unwrap(); assert_eq!("Hello", node.as_str().unwrap()); assert_eq!(b" World", &bytes[i..]);
pub fn parse_prefix_in(
buf: &'a [u8],
tokens: &'a mut Vec<Token>
) -> Result<(Self, usize)>
[src]
buf: &'a [u8],
tokens: &'a mut Vec<Token>
) -> Result<(Self, usize)>
Parse given bencoded bytes from the beginning and returns index where one root object is parsed successfully. It accepts a token buffer argument which helps reuse the buffer next time.
Example:
use ben::Node; let mut v = vec![]; let values: &[&[u8]] = &[b"5:Hello World", b"1:ade"]; for bytes in values { let (node, i) = Node::parse_prefix_in(bytes, &mut v).unwrap(); assert!(node.is_str()); }
pub fn parse_max_tokens(buf: &'a [u8], max_tokens: usize) -> Result<Self>
[src]
Parse given bencoded bytes with limit on maximum number of tokens that can be parsed.
Example:
use ben::{Node, Error}; let bytes = b"l1:a2:bce"; let err = Node::parse_max_tokens(bytes, 2).unwrap_err(); assert_eq!(Error::NoMemory, err);
pub fn data(&self) -> &'a [u8]
[src]
Returns raw bytes inside this node.
Example:
use ben::Node; let bytes = b"l1:a2:bce"; let node = Node::parse(bytes).unwrap(); assert_eq!(b"1:a2:bc", node.data());
pub fn is_list(&self) -> bool
[src]
Returns true if this node is a list.
pub fn is_dict(&self) -> bool
[src]
Returns true if this node is a dictionary.
pub fn is_str(&self) -> bool
[src]
Returns true if this node is a string.
pub fn is_int(&self) -> bool
[src]
Returns true if this node is a integer.
pub fn as_list(&self) -> Option<List>
[src]
Return this node as a List
which provides further
list operations such as get
, iter
etc.
Example:
use ben::Node; let bytes = b"l1:a2:bce"; let node = Node::parse(bytes).unwrap(); let list = node.as_list().unwrap(); assert_eq!("a", list.get_str(0).unwrap()); assert_eq!("bc", list.get_str(1).unwrap());
pub fn as_dict(&self) -> Option<Dict>
[src]
Return this node as a Dict
which provides further
dictionary operations such as get
, iter
etc.
Example:
use ben::Node; let bytes = b"d1:a2:bce"; let node = Node::parse(bytes).unwrap(); let dict = node.as_dict().unwrap(); assert_eq!("bc", dict.get_str(b"a").unwrap());
pub fn as_int(&self) -> Option<i64>
[src]
Return this node as a i64
.
Example:
use ben::Node; let bytes = b"i123e"; let node = Node::parse(bytes).unwrap(); assert_eq!(123, node.as_int().unwrap());
pub fn as_str(&self) -> Option<&'a str>
[src]
Return this node as a i64
.
Example:
use ben::Node; let bytes = b"3:abc"; let node = Node::parse(bytes).unwrap(); assert_eq!("abc", node.as_str().unwrap());
Trait Implementations
impl<'_> Debug for Node<'_>
[src]
impl<'a> PartialEq<Node<'a>> for Node<'a>
[src]
impl<'a> StructuralPartialEq for Node<'a>
[src]
Auto Trait Implementations
impl<'a> RefUnwindSafe for Node<'a>
impl<'a> Send for Node<'a>
impl<'a> Sync for Node<'a>
impl<'a> Unpin for Node<'a>
impl<'a> UnwindSafe for Node<'a>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,