From 6b3c3179e0fda27cf78f70d93a69a8b4a7049545 Mon Sep 17 00:00:00 2001 From: Baitinq Date: Thu, 27 Oct 2022 17:32:33 +0200 Subject: 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. --- frontend/Cargo.toml | 3 ++- frontend/src/main.rs | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'frontend') 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 { + 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(&self, state: &mut H) { self.url.hash(state); @@ -44,7 +59,8 @@ fn osse() -> Html { let display_results = |results: &Vec| -> Html { results - .into_iter() + .iter() + .sorted() .map(|r| { html! {
-- cgit 1.4.1