pub enum BNode {
Integer(i64),
Bytes(Vec<u8>),
List(BList),
Dict(BDict),
}
Variants§
Implementations§
source§impl BNode
impl BNode
sourcepub fn serialize<W>(&self, buf: &mut W) -> Result<usize>where
W: Write,
pub fn serialize<W>(&self, buf: &mut W) -> Result<usize>where W: Write,
Examples found in repository?
More 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();
}
sourcepub fn as_integer(&self) -> Result<&i64, String>
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
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);
}
sourcepub fn as_bytes(&self) -> Result<&[u8], String>
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
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);
}
sourcepub fn as_list(&self) -> Result<&[BNode], String>
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()
);
}
sourcepub fn as_dict(&self) -> Result<&BDict, String>
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
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 PartialEq<BNode> for BNode
impl PartialEq<BNode> for BNode
impl Eq for BNode
impl StructuralEq for BNode
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more