Skip to main content

from_str

Function from_str 

Source
pub fn from_str<'a, T>(string: &'a str) -> Result<T, DeError>
where T: Deserialize<'a>,
Available on crate feature xml only.
Expand description

Deserialize an instance of type T from a string of XML text.

Elements or non-whitespace text that follow the value in string are an error.

Always use Xml to deserialize XML request data.

§Example

use rocket::serde::{Deserialize, xml};

#[derive(Debug, PartialEq, Deserialize)]
#[serde(crate = "rocket::serde")]
struct Data<'r> {
    framework: &'r str,
    stars: usize,
}

let string = r#"
    <Data>
        <framework>Rocket</framework>
        <stars>5</stars>
    </Data>
"#;

let data: Data = xml::from_str(string).unwrap();
assert_eq!(data, Data { framework: "Rocket", stars: 5, });

§Errors

This conversion can fail if the structure of the input does not match the structure expected by T, for example if T is a struct type but the input contains something other than a XML element. It can also fail if the structure is correct but T’s implementation of Deserialize decides that something is wrong with the data, for example required struct fields are missing from the XML element or some number is too big to fit in the expected primitive type.