diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2022-10-27 17:32:33 +0200 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2022-10-27 17:32:47 +0200 |
commit | 6b3c3179e0fda27cf78f70d93a69a8b4a7049545 (patch) | |
tree | 16a16f2f58d65a68b702ea8de92387a03884ffb8 | |
parent | Frontend: Use display_results() function for rendering CrawledResources (diff) | |
download | OSSE-6b3c3179e0fda27cf78f70d93a69a8b4a7049545.tar.gz OSSE-6b3c3179e0fda27cf78f70d93a69a8b4a7049545.tar.bz2 OSSE-6b3c3179e0fda27cf78f70d93a69a8b4a7049545.zip |
Fronted: Order search results by priority
We do this by implementing the PartialOrd and Ord traits into the CrawledResource struct and then using Itertools::sorted() on the display iterator.
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | frontend/Cargo.toml | 3 | ||||
-rw-r--r-- | frontend/src/main.rs | 18 |
3 files changed, 20 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock index 6f48c0b..0bd1e55 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -618,6 +618,7 @@ version = "0.1.0" dependencies = [ "gloo 0.8.0", "gloo-net", + "itertools", "serde", "wasm-bindgen", "wasm-bindgen-futures", diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index c1be331..106b818 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -12,4 +12,5 @@ web-sys = "0.3.60" wasm-bindgen = "0.2.83" gloo-net = "0.2" serde = { version = "1.0", features = ["derive", "rc"] } -wasm-bindgen-futures = "0.4" \ No newline at end of file +wasm-bindgen-futures = "0.4" +itertools = "0.10.5" diff --git a/frontend/src/main.rs b/frontend/src/main.rs index fd28d89..0195a88 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,6 +1,8 @@ use gloo::console::log; use gloo_net::http::Request; +use itertools::Itertools; use serde::Deserialize; +use std::cmp::Ordering; use std::hash::{Hash, Hasher}; use std::{ops::Deref, sync::Arc}; use wasm_bindgen::*; @@ -22,6 +24,19 @@ impl PartialEq for CrawledResource { } } impl Eq for CrawledResource {} + +impl PartialOrd for CrawledResource { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +impl Ord for CrawledResource { + fn cmp(&self, other: &Self) -> Ordering { + self.priority.cmp(&other.priority) + } +} + impl Hash for CrawledResource { fn hash<H: Hasher>(&self, state: &mut H) { self.url.hash(state); @@ -44,7 +59,8 @@ fn osse() -> Html { let display_results = |results: &Vec<CrawledResource>| -> Html { results - .into_iter() + .iter() + .sorted() .map(|r| { html! { <div key={r.url.to_owned()}> |