Function read_file

Source
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?
examples/parse_first_matching_element.rs (line 9)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let mut file = File::open("examples/TheExpanseSeries.xml")?;
9    let data = read_file(&mut file)?;
10    let (_, doc) = Document::parse_element_by_tag_name(&data, "book", &None)?;
11    println!("{doc:?}");
12    Ok(())
13}
More examples
Hide additional examples
examples/parse_all_of_specific_tag.rs (line 10)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let mut file = File::open("examples/TheExpanseSeries.xml")?;
10    let data = read_file(&mut file)?;
11
12    let (_, doc) = Document::parse_elements_by_tag_name(&data, "book", &None)?;
13    println!("{doc:?}");
14    Ok(())
15}
examples/parse_element_with_specific_attribute_value.rs (line 10)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let mut file = File::open("examples/TheExpanseSeries.xml")?;
10    let data = read_file(&mut file)?;
11    let (_, doc) = Document::parse_element_by_tag_name(
12        &data,
13        "book",
14        &Some(vec![Attribute::new("isbn", "978-0316332910")]),
15    )?;
16    println!("{doc:?}");
17    Ok(())
18}
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}