about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2022-11-02 21:39:32 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2022-11-02 21:39:32 +0100
commitab126ea4df01af6ca7da2118740fca8ffd22ac37 (patch)
tree7ce8fbfa8a1ee8bf7de7a4a7e50cb2c8a132f076
parentIndexer: Abstract indexer (diff)
downloadOSSE-ab126ea4df01af6ca7da2118740fca8ffd22ac37.tar.gz
OSSE-ab126ea4df01af6ca7da2118740fca8ffd22ac37.tar.bz2
OSSE-ab126ea4df01af6ca7da2118740fca8ffd22ac37.zip
Lib+Indexer: Make IndexedResource title and description Optional
-rw-r--r--frontend/src/app.rs7
-rw-r--r--indexer/src/indexer_implementation.rs8
-rw-r--r--indexer/src/main.rs24
-rw-r--r--lib/src/lib.rs5
4 files changed, 27 insertions, 17 deletions
diff --git a/frontend/src/app.rs b/frontend/src/app.rs
index 064d49a..f65e29c 100644
--- a/frontend/src/app.rs
+++ b/frontend/src/app.rs
@@ -39,6 +39,7 @@ fn result_component(props: &ResultComponentProps) -> Html {
     )
     .unwrap();
     let style = style.get_class_name().to_owned();
+
     html! {
         <div class={format!("mb-4 {}", style)}>
             <a href={props.result.url.clone()}>
@@ -46,9 +47,9 @@ fn result_component(props: &ResultComponentProps) -> Html {
                 <p class="title mb-1">{props.result.title.clone()}</p>
             </a>
             <p class="description">
-                {match props.result.description.clone().as_str() {
-                    "" => "No Description.",
-                    otherwise => otherwise,
+                {match props.result.description.clone() {
+                    None => "No Description.".to_string(),
+                    Some(description) => description.to_owned(),
                 }}{format!("PRIO: {}", props.result.priority)}
             </p>
         </div>
diff --git a/indexer/src/indexer_implementation.rs b/indexer/src/indexer_implementation.rs
index 4bb3857..d5cfead 100644
--- a/indexer/src/indexer_implementation.rs
+++ b/indexer/src/indexer_implementation.rs
@@ -30,8 +30,8 @@ impl crate::Indexer for IndexerImplementation {
         &mut self,
         word: &str,
         url: &str,
-        title: &str,
-        description: &str,
+        title: Option<String>,
+        description: Option<String>,
         content: &str,
         fixed_words: &[String],
     ) -> Result<(), String> {
@@ -39,8 +39,8 @@ impl crate::Indexer for IndexerImplementation {
             url: url.to_string(),
             priority: Self::calculate_word_priority(word, content, fixed_words),
             word: Arc::new(word.to_string()),
-            title: title.to_string(),
-            description: description.to_string(),
+            title: title.map(String::from),
+            description: description.map(String::from),
         };
 
         match self.database.get_mut(word) {
diff --git a/indexer/src/main.rs b/indexer/src/main.rs
index 289789c..9467cff 100644
--- a/indexer/src/main.rs
+++ b/indexer/src/main.rs
@@ -14,8 +14,8 @@ pub trait Indexer {
         &mut self,
         word: &str,
         url: &str,
-        title: &str,
-        description: &str,
+        title: Option<String>,
+        description: Option<String>,
         content: &str,
         fixed_words: &[String],
     ) -> Result<(), String>;
@@ -89,18 +89,26 @@ async fn add_resource(
     let title_selector = scraper::Selector::parse("title").unwrap();
     let description_selector = scraper::Selector::parse("meta").unwrap();
 
-    let page_title: String = document
+    let page_title: Option<String> = match document
         .select(&title_selector)
         .map(|e| e.inner_html())
         .take(1)
-        .collect();
+        .collect::<String>()
+    {
+        s if s.is_empty() => None,
+        string => Some(string),
+    };
 
-    let page_description: String = document
+    let page_description: Option<String> = match document
         .select(&description_selector)
         .filter(|e| e.value().attr("name") == Some("description"))
         .filter_map(|e| e.value().attr("content"))
         .take(1)
-        .collect();
+        .collect::<String>()
+    {
+        s if s.is_empty() => None,
+        string => Some(string),
+    };
 
     //and for each changed content word we add it to the db (word -> list.append(url))
     let mut indexer = data.indexer.lock().unwrap();
@@ -108,8 +116,8 @@ async fn add_resource(
         let _ = indexer.insert(
             word,
             &resource.url,
-            &page_title,
-            &page_description,
+            page_title.clone(),
+            page_description.clone(),
             &resource.content,
             &fixed_words,
         );
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index 8a006f6..48bf92b 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -14,10 +14,11 @@ pub mod lib {
     #[derive(Debug, Clone, Serialize, Deserialize)]
     pub struct IndexedResource {
         pub url: String,
-        pub title: String,
-        pub description: String,
+        pub title: Option<String>,
+        pub description: Option<String>,
         pub priority: u32,
         pub word: Arc<String>,
+        //maybe in the future we need filetypes?
     }
 
     //We implement PartialEq, Eq and Hash to ignore the priority field.