Function serde_bencode::de::from_str

source ·
pub fn from_str<'de, T>(s: &'de str) -> Result<T>
where T: Deserialize<'de>,
Expand description

Deserialize an instance of type T from a string of bencode.

Examples

use serde_derive::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct Address {
    street: String,
    city: String,
}

let encoded = "d4:city18:Duckburg, Calisota6:street17:1313 Webfoot Walke".to_string();
let decoded: Address = serde_bencode::from_str(&encoded)?;

assert_eq!(
    decoded,
    Address {
        street: "1313 Webfoot Walk".to_string(),
        city: "Duckburg, Calisota".to_string(),
    }
);

Errors

This conversion can fail if the input bencode is improperly formatted or if the structure of the input does not match the structure expected by T. It can also fail if T’s implementation of Deserialize decides to fail.