pub fn read_file(file: &mut File) -> Result<String>
Expand description
Read the file and decode the contents into a String
Examples found in repository?
More examples
examples/extract_information_manual.rs (line 164)
162fn main() -> Result<(), Box<dyn std::error::Error>> {
163 let mut file = File::open("examples/TheExpanseSeries.xml")?;
164 let data = read_file(&mut file)?;
165 let (_, doc) = Document::parse_element_by_tag_name(&data, "book", &None)?;
166 let mut book = Book::default();
167
168 doc.iter_with_depth(0)
169 .filter_map(|element| {
170 if let Document::Element(tag, inner_doc, _) = element {
171 Some((tag, inner_doc))
172 } else {
173 None
174 }
175 })
176 .try_for_each(|(tag, inner_doc)| book.update_field(tag, inner_doc))?;
177
178 println!("{book:#?}");
179 Ok(())
180}
examples/extract_information_derived.rs (line 39)
37fn main() -> Result<(), Box<dyn std::error::Error>> {
38 let mut file = File::open("examples/TheExpanseSeries.xml")?;
39 let data = read_file(&mut file)?;
40 let (_, doc) = Document::parse_element_by_tag_name(&data, "book", &None)?;
41 let mut book = Book::default();
42 // doc.iter_with_depth(0)
43 // .filter_map(|record| {
44 // if let Document::Element(tag, inner_doc, _) = record {
45 // Some((tag, inner_doc))
46 // } else {
47 // None
48 // }
49 // })
50 // .try_for_each(|(tag, inner_doc)| book.update_field(tag, inner_doc))?;
51 // book.update_attribute_fields(&doc);
52 book.update_fields(&doc)?;
53 println!("{book:#?}");
54 Ok(())
55}
examples/extract_information_derived_catalog.rs (line 45)
43fn main() -> Result<(), Box<dyn std::error::Error>> {
44 let mut file = File::open("examples/TheExpanseSeries.xml")?;
45 let data = read_file(&mut file)?;
46 let (_, doc) = Document::parse_element_by_tag_name(&data, "catalog", &None)?;
47 let mut books = Books::default();
48
49 doc.iter_with_depth(0)
50 .filter_map(|element| {
51 if let Document::Element(tag, inner_doc, _) = element {
52 Some((tag, inner_doc))
53 } else {
54 None
55 }
56 })
57 .try_for_each(|(tag, inner_doc)| books.update_field(tag, inner_doc))
58 .map_err(|e| {
59 println!("Error updating field: {}", e);
60 e
61 })?;
62
63 println!("{books:#?}");
64 Ok(())
65}
Additional examples can be found in: