Trait UpdateFields

Source
pub trait UpdateFields {
    // Required method
    fn update_field(
        &mut self,
        tag: &Tag,
        doc: &Document,
    ) -> Result<(), Box<dyn Error>>;

    // Provided methods
    fn update_fields(&mut self, doc: &Document) -> Result<(), Box<dyn Error>>
       where Self: Debug { ... }
    fn update_attribute_fields(
        &mut self,
        _tag: &Tag,
    ) -> Result<(), Box<dyn Error>> { ... }
}

Required Methods§

Source

fn update_field( &mut self, tag: &Tag, doc: &Document, ) -> Result<(), Box<dyn Error>>

Provided Methods§

Source

fn update_fields(&mut self, doc: &Document) -> Result<(), Box<dyn Error>>
where Self: Debug,

Examples found in repository?
examples/extract_information_derived.rs (line 52)
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}
More examples
Hide additional examples
examples/extract_information_manual.rs (line 67)
21    fn update_field(
22        &mut self,
23        tag: &Tag,
24        doc: &Document,
25    ) -> Result<(), Box<dyn std::error::Error>> {
26        let field_name = &tag.name.local_part;
27
28        if let Some(attributes_vec) = &tag.attributes {
29            for attr in attributes_vec.iter() {
30                if let Attribute::Instance {
31                    name,
32                    value: AttributeValue::Value(attr_val),
33                } = attr
34                {
35                    if name.local_part == "isbn" {
36                        self.isbn = attr_val.to_string();
37                    }
38                }
39            }
40        }
41
42        match &doc {
43            Document::Content(Some(value)) => match field_name.as_str() {
44                "title" => {
45                    self.title = value.to_string();
46                }
47                "genre" => {
48                    self.genre = value.to_string();
49                }
50                "type" => {
51                    self.ty = value.to_string();
52                }
53                "series_number" => {
54                    self.series_number = value.parse().unwrap_or_default();
55                }
56                "description" => {
57                    self.description = value.to_string();
58                }
59                e => {
60                    return Err(format!("Unknown field2: {}", e).into());
61                }
62            },
63            Document::Nested(_) => {
64                for element in doc.iter_with_depth(1) {
65                    if let Document::Element(tag, inner_doc, _) = element {
66                        if "authored_by" == tag.name.local_part {
67                            self.authored_by.update_fields(inner_doc)?;
68                        } else {
69                            self.update_field(tag, inner_doc)?;
70                        }
71                    } else {
72                        return Err(format!("Unknown field: {element:#?}").into());
73                    }
74                }
75            }
76
77            _ => {
78                return Err("Content is missing".into());
79            }
80        }
81
82        Ok(())
83    }
examples/extract_information_manual_catalog.rs (line 104)
56    fn update_field(
57        &mut self,
58        tag: &Tag,
59        doc: &Document,
60    ) -> Result<(), Box<dyn std::error::Error>> {
61        let field_name = &tag.name.local_part;
62
63        if let Some(attributes) = &tag.attributes {
64            for attr in attributes.iter() {
65                if let Attribute::Instance {
66                    name,
67                    value: AttributeValue::Value(attr_val),
68                } = attr
69                {
70                    if name.local_part == "isbn" {
71                        self.isbn = Some(attr_val.into());
72                    }
73                }
74            }
75        }
76
77        match &doc {
78            Document::Content(Some(value)) => match field_name.as_str() {
79                "title" => {
80                    self.title = value.into();
81                }
82                "genre" => {
83                    self.genre = value.into();
84                }
85                "type" => {
86                    self.ty = value.to_string();
87                }
88                "series_number" => {
89                    self.series_number = value.parse().unwrap_or_default();
90                }
91                "description" => {
92                    self.description = value.into();
93                }
94                e => {
95                    return Err(format!("Unknown field: {}", e).into());
96                }
97            },
98            Document::Nested(_) => {
99                doc.iter_with_depth(1).try_for_each(
100                    |element| -> Result<(), Box<dyn std::error::Error>> {
101                        if let Document::Element(tag, inner_doc, _) = element {
102                            match tag.name.local_part.as_str() {
103                                "authored_by" => {
104                                    self.authored_by.update_fields(inner_doc)?;
105                                }
106                                _ => {
107                                    self.update_field(tag, inner_doc)?;
108                                }
109                            }
110                            Ok(())
111                        } else {
112                            Err(format!("Unknown field: {element:#?}").into())
113                        }
114                    },
115                )?;
116            }
117            _ => {
118                return Err("Content is missing".into());
119            }
120        }
121        Ok(())
122    }
Source

fn update_attribute_fields(&mut self, _tag: &Tag) -> Result<(), Box<dyn Error>>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> UpdateFields for Option<T>
where T: UpdateFields + Default + Debug,

Source§

fn update_fields(&mut self, doc: &Document) -> Result<(), Box<dyn Error>>

Source§

fn update_field( &mut self, tag: &Tag, doc: &Document, ) -> Result<(), Box<dyn Error>>

Source§

fn update_attribute_fields(&mut self, tag: &Tag) -> Result<(), Box<dyn Error>>

Implementors§