Skip to content

Commit 9e01e3e

Browse files
committed
refactor websearch to use super:: instead of seperate file for shared structs
1 parent 9c687fb commit 9e01e3e

3 files changed

Lines changed: 38 additions & 37 deletions

File tree

src/websearch/bits.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/websearch/mod.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,40 @@ use std::collections::HashMap;
33

44
use crate::{
55
module::{Module, ModuleMessage},
6-
util,
7-
websearch::bits::SearchError,
8-
widglets,
6+
util, widglets,
97
};
108

11-
mod bits;
12-
use bits::SearchResult;
13-
pub use bits::WebMsg;
9+
// mod dictionary;
1410
mod wikipedia;
1511

12+
#[derive(Debug, Clone)]
13+
pub enum WebMsg {
14+
GotResult(String, Result<Vec<SearchResult>, SearchError>),
15+
FetchedImage((String, Result<iced::widget::image::Handle, ()>)),
16+
ResultActivated(String), // URL
17+
}
18+
19+
#[derive(Debug, Clone)]
20+
pub struct SearchResult {
21+
pub destination_url: String,
22+
pub title: String,
23+
pub description: String,
24+
pub image_url: Option<String>,
25+
}
26+
27+
#[derive(Debug, Clone, thiserror::Error)]
28+
pub enum SearchError {
29+
BadResponse(String),
30+
}
31+
32+
impl std::fmt::Display for SearchError {
33+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34+
match self {
35+
Self::BadResponse(inner) => f.write_str(&format!("Bad Response: {inner}")),
36+
}
37+
}
38+
}
39+
1640
#[derive(Debug)]
1741
pub struct Web {
1842
input_for_results: String,
@@ -57,9 +81,9 @@ impl Web {
5781
let first = input_chars.clone().next();
5882
let search_text = input.trim().to_string();
5983

60-
match (first, search_text) {
84+
match first {
6185
// get first char
62-
(Some('w'), search_text) => {
86+
Some('w') => {
6387
log::debug!("wikipedia time!");
6488
let client = self.client.clone();
6589

@@ -75,7 +99,11 @@ impl Web {
7599
|r| ModuleMessage::WebMessage(WebMsg::GotResult(r.0, r.1)),
76100
)
77101
}
78-
(None, _) => {
102+
Some('d') => {
103+
log::debug!("Dictionary module");
104+
Task::none()
105+
}
106+
None => {
79107
log::info!("Did not match in web searcher");
80108
Task::none()
81109
}

src/websearch/wikipedia.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::Deserialize;
22

3-
use super::bits::{SearchError, SearchResult};
3+
use super::{SearchError, SearchResult};
44

55
#[derive(Deserialize)]
66
struct WikiResults {

0 commit comments

Comments
 (0)