From 6408ddb6a3160d44c2b76376b86e9028b6d4d72d Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 8 Sep 2022 08:37:49 +0200 Subject: [PATCH] example --- content/blog/bencode-parser-with-nom.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/content/blog/bencode-parser-with-nom.md b/content/blog/bencode-parser-with-nom.md index 03334aa..d928cef 100644 --- a/content/blog/bencode-parser-with-nom.md +++ b/content/blog/bencode-parser-with-nom.md @@ -313,4 +313,26 @@ pub fn parse(source: &[u8]) -> Result, Error<&[u8]>> { } ``` +An example using the parser: + +```rust +use nom_bencode::Value; + +let data = nom_bencode::parse(b"d3:cow3:moo4:spam4:eggse").unwrap(); +let v = data.first().unwrap(); + +if let Value::Dictionary(dict) = v { + let v = dict.get("cow".as_bytes()).unwrap(); + + if let Value::Bytes(data) = v { + assert_eq!(data, b"moo"); + } + + let v = dict.get("spam".as_bytes()).unwrap(); + if let Value::Bytes(data) = v { + assert_eq!(data, b"eggs"); + } +} +``` + You can find the full source code here: