Hi everyone !
Actually, I have a Hashset populate with object initialized by parsing XML.
The problem is that the XML that I retrieve can have the same object than an other, but with some fields différent (except of unique ones).
Unique fields are : reference and create date
I have think on 2 solutions :
- Extract all in a Vec, and next filter on unique fields (and to get the latest, check the update date)
- Order files by date and keep Hashset (If when I add to hashset it keeped the recently one)
The problem is that I am lost on possbile implementation.
What is the best solution for you ?
Could you help me please ?
(I do not necessarily ask you to develop for me, just to guide me on the right way/reflection)
Current implementation (no solution already implemented)
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Ad {
pub title: String,
pub description: String,
pub price: String,
pub reference: String,
pub photos: Vec<String>,
pub creation_date: NaiveDateTime,
pub update_date: NaiveDateTime,
}
let mut ads_list = HashSet::new();
read_dir
.filter_map(Result::ok)
.filter(|f| match f.metadata() {
Ok(t) => t.is_file(),
Err(_) => false,
})
.filter(|d| {
d.path()
.file_name()
.and_then(|f| {
f.to_str().and_then(|n| {
if let Some(id) = agency_id {
if !n.starts_with(format!("import-{}", id).as_str()) {
return None;
}
}
if n.ends_with(".xml") {
Some(d)
} else {
None
}
})
})
.is_some()
})
.for_each(|f| {
trace!("File {}", f.path().display());
match self.parse_xml_for_path(f.path(), product_type) {
Ok(r) => ads_list.extend(r),
Err(e) => {
warn!(
"An error occured for file '{}' : {}",
f.path().as_path().display(),
e
)
}
}
});