Skip to content

Instantly share code, notes, and snippets.

@ian-p-cooke
Last active November 23, 2018 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-p-cooke/b4d74a8459814d881c6f6510bf1c6f7b to your computer and use it in GitHub Desktop.
Save ian-p-cooke/b4d74a8459814d881c6f6510bf1c6f7b to your computer and use it in GitHub Desktop.
extern crate sxd_document;
extern crate sxd_xpath;
use std::error::Error;
use sxd_document::parser;
use sxd_xpath::evaluate_xpath;
use sxd_xpath::Value;
pub struct LengthTags<'a> {
pub tags: Vec<&'a [u8]>,
}
pub fn parse1(xml: &str) -> Result<LengthTags, Box<Error>> {
let package = parser::parse(xml)?;
let document = package.as_document();
let mut length_tags = LengthTags { tags: vec![] };
let fields = evaluate_xpath(&document, "/fix/fields/field")?;
if let Value::Nodeset(nodes) = fields {
for node in nodes {
let el = node.element().unwrap();
let field_type = el.attribute_value("type").unwrap();
if field_type.to_lowercase() == "length" {
let field_name = el.attribute_value("name").unwrap();
let field_number = el.attribute_value("number").unwrap();
println!("{} : {} = {}", field_name, field_type, field_number);
length_tags.tags.push(field_number.as_bytes());
}
}
}
Ok(length_tags)
}
fn main() -> Result<(), Box<Error>> {
let xml = r#"<fix><fields><field name="blah" number="1" type="LENGTH"></field><field name="blahblah" number="12" type="STRING"></field></fields></fix>"#;
let length_tags = parse1(xml).unwrap();
assert_eq!(length_tags.tags.len(), 1);
Ok(())
}
@ian-p-cooke
Copy link
Author

this doesn't compile

    error[E0597]: `package` does not live long enough
    --> examples\xml_example.rs:15:20
    |
    15 |     let document = package.as_document();
    |                    ^^^^^^^ borrowed value does not live long enough
    ...
    33 | }
    | - borrowed value only lives until here
    |
    note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 13:1...
    --> examples\xml_example.rs:13:1
    |
    13 | / pub fn parse1(xml: &str) -> Result<LengthTags, Box<Error>> {
    14 | |     let package = parser::parse(xml)?;
    15 | |     let document = package.as_document();
    16 | | 
    ...  |
    32 | |     Ok(length_tags)
    33 | | }
    | |_^

    error[E0597]: `document` does not live long enough
    --> examples\xml_example.rs:19:34
    |
    19 |     let fields = evaluate_xpath(&document, "/fix/fields/field")?;
    |                                  ^^^^^^^^ borrowed value does not live long enough
    ...
    33 | }
    | - borrowed value only lives until here
    |
    note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 13:1...
    --> examples\xml_example.rs:13:1
    |
    13 | / pub fn parse1(xml: &str) -> Result<LengthTags, Box<Error>> {
    14 | |     let package = parser::parse(xml)?;
    15 | |     let document = package.as_document();
    16 | | 
    ...  |
    32 | |     Ok(length_tags)
    33 | | }
    | |_^

    error: aborting due to 2 previous errors

    For more information about this error, try `rustc --explain E0597`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment