Skip to content

Commit

Permalink
fix: fix remote dictonary server address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ikey4u committed Nov 28, 2021
1 parent 3d06b62 commit d647366
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
3 changes: 3 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum WikitError {

#[error("FSTLevenshteinError error")]
FSTLevenshteinError(#[from] fst::automaton::LevenshteinError),

#[error("Reqwest Error")]
ReqwestError(#[from] reqwest::Error),
}

pub type WikitResult<T> = std::result::Result<T, WikitError>;
Expand Down
27 changes: 16 additions & 11 deletions core/src/wikit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,34 @@ impl RemoteDictionary {
}

pub fn get_dict_list(&self) -> WikitResult<Vec<DictList>> {
let r = reqwest::blocking::get(format!("{}/wikit/list", self.url)).unwrap().json::<Vec<DictList>>().unwrap();
let url = format!("{}/wikit/list", self.url);
let r = reqwest::blocking::get(url)?.json::<Vec<DictList>>()?;
Ok(r)
}

pub fn lookup<P>(&self, word: P, dict: P) -> WikitResult<Vec<(String, String)>> where P: AsRef<str> {
let r = reqwest::blocking::get(
format!("{}/wikit/query?word={}&dictname={}", self.url, word.as_ref(), dict.as_ref())
).unwrap().json::<Vec<(String, String)>>().unwrap();
)?.json::<Vec<(String, String)>>()?;
Ok(r)
}

pub fn get_script<S>(&self, dict: S) -> String where S: AsRef<str> {
let r = reqwest::blocking::get(
format!("{}/wikit/script?dictname={}", self.url, dict.as_ref())
).unwrap().text().unwrap();
r
if let Ok(r) = reqwest::blocking::get(format!("{}/wikit/script?dictname={}", self.url, dict.as_ref())) {
if let Ok(r) = r.text() {
return r;
}
}
"".to_string()
}

pub fn get_style<S>(&self, dict: S) -> String where S: AsRef<str> {
let r = reqwest::blocking::get(
format!("{}/wikit/style?dictname={}", self.url, dict.as_ref())
).unwrap().text().unwrap();
r
if let Ok(r) = reqwest::blocking::get(format!("{}/wikit/style?dictname={}", self.url, dict.as_ref())) {
if let Ok(r) = r.text() {
return r;
}
}
"".to_string()
}
}

Expand Down Expand Up @@ -496,7 +501,7 @@ pub fn load_dictionary_from_uri<S>(uri: S) -> Option<WikitDictionary> where S: A
""
};
let dict = RemoteDictionary::new(
format!("{}://{}{}", url.scheme(), host, port),
format!("{}://{}{}{}", url.scheme(), host, port, url.path()),
user.to_string(),
token.to_string(),
);
Expand Down
2 changes: 2 additions & 0 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ fn get_dict_list() -> Vec<String> {
for d in ds {
dictdb.insert(d.id.clone(), dict.clone());
}
} else {
println!("failed to get remote dictionary list");
}
},
}
Expand Down

0 comments on commit d647366

Please sign in to comment.