Skip to content

Commit

Permalink
clippy stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed May 4, 2023
1 parent 839f5e3 commit b60d3d7
Show file tree
Hide file tree
Showing 23 changed files with 173 additions and 172 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub trait Tab {
if let MyKey::KeyPress(pos) = key {
self.get_view().cursor = pos;
}
let cursor = self.get_cursor().clone();
let cursor = *self.get_cursor();
match key {
MyKey::Nav(dir) => self.navigate(dir),
key => self.keyhandler(appdata, key, &cursor),
Expand Down Expand Up @@ -417,7 +417,7 @@ pub trait Tab {
area = chunks[1];
self.transform_area(&mut area);
}
let cursor = self.get_cursor().clone();
let cursor = *self.get_cursor();
self.set_selection(area);
self.get_tabdata().view.validate_pos(); // ensures cursor is on a widget;
self.render(f, appdata, &cursor);
Expand Down
51 changes: 26 additions & 25 deletions src/popups/ankimporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ impl fmt::Display for Deck {
}
}
use crate::app::{AppData, Tab, TabData, Widget};
impl<'a> Ankimporter<'a> {
pub fn new() -> Self {

impl Default for Ankimporter<'_> {
fn default() -> Self {
let mut list = StatefulList::<Deck>::new("".to_string());
list.persistent_highlight = true;
let searchterm = Field::default();
let description = Field::default();

Ankimporter {
Self {
prompt: Button::new("Choose anki deck".to_string()),
searchterm,
description,
Expand All @@ -89,16 +90,18 @@ impl<'a> Ankimporter<'a> {
tabdata: TabData::new("Anki Importing".to_string()),
}
}
}

impl<'a> Ankimporter<'a> {
fn update_desc(&mut self) {
if let Some(idx) = self.list.state.selected() {
let id = self.list.items[idx].id;
if !self.descmap.contains_key(&id) {
self.descmap.entry(id).or_insert_with(|| {
let url = format!("https://ankiweb.net/shared/info/{}", id);
let body = reqwest::blocking::get(url).unwrap().text().unwrap();
let desc = get_description(&body, &id);
self.descmap.insert(id, desc);
}

get_description(&body, &id)
});
}
}

Expand All @@ -117,13 +120,13 @@ impl<'a> Ankimporter<'a> {
let body = reqwest::blocking::get(url).unwrap().text().unwrap();

let splitter: Vec<&str> = body.split("const shared = new anki.SharedList(").collect();
let foo = splitter[1].to_string();
let xxx = splitter[1].to_string();

let mut myvec = Vec::<Deck>::new();
let mut stringstatus = Stringstatus::Beforeint;
let mut title = String::new();
let mut intrep = String::new();
for c in foo.chars() {
for c in xxx.chars() {
if c == ';' {
break;
}
Expand Down Expand Up @@ -170,11 +173,11 @@ impl<'a> Ankimporter<'a> {
}

for deck in &myvec {
if !self.descmap.contains_key(&deck.id) {
if let std::collections::hash_map::Entry::Vacant(e) = self.descmap.entry(deck.id) {
let url = format!("https://ankiweb.net/shared/info/{}", deck.id);
let body = reqwest::blocking::get(url).unwrap().text().unwrap();
let desc = get_description(&body, &deck.id);
self.descmap.insert(deck.id, desc);
e.insert(desc);
break;
}
}
Expand Down Expand Up @@ -204,7 +207,7 @@ impl<'a> Tab for Ankimporter<'a> {
});
let msg = MsgPopup::new(rx, "Unzipping".to_string());
self.set_popup(Box::new(msg));
self.state = State::Unzipping(name.clone());
self.state = State::Unzipping(name);
}
State::Unzipping(name) => {
let (tx, rx): (mpsc::SyncSender<ImportProgress>, Receiver<ImportProgress>) =
Expand All @@ -219,7 +222,7 @@ impl<'a> Tab for Ankimporter<'a> {
self.state = State::Renaming(name);
}
State::Renaming(name) => {
let ldc = LoadCards::new(appdata, name.clone());
let ldc = LoadCards::new(appdata, name);
self.set_popup(Box::new(ldc));
self.state = State::Main;
}
Expand Down Expand Up @@ -356,10 +359,10 @@ pub async fn download_deck(

while let Some(item) = stream.next().await {
let chunk = item
.or(Err(format!("Error while downloading file")))
.or(Err("Error while downloading file".to_string()))
.unwrap();
file.write_all(&chunk)
.or(Err(format!("Error while writing to file")))
.or(Err("Error while writing to file".to_string()))
.unwrap();
let new = std::cmp::min(downloaded + (chunk.len() as u64), total_size);
downloaded = new;
Expand All @@ -373,18 +376,17 @@ pub async fn download_deck(
fn extract_download_link(trd: &String) -> String {
let pattern = r"(?P<link>(https:.*));".to_string();
let re = Regex::new(&pattern).unwrap();
let foo = re.captures(&trd).expect(&format!(
"Couldnt find pattern on following string: {}@@",
trd
));
foo.get(1).unwrap().as_str().to_string()
let xxx = re
.captures(trd)
.unwrap_or_else(|| panic!("Couldnt find pattern on following string: {}@@", trd));
xxx.get(1).unwrap().as_str().to_string()
}

fn get_k_value(pagesource: &String) -> String {
fn get_k_value(pagesource: &str) -> String {
let pattern = "k\" value=\"(.*)\"".to_string();
let re = Regex::new(&pattern).unwrap();
let foo = re.captures(&pagesource).unwrap();
foo.get(1).unwrap().as_str().to_string()
let xxx = re.captures(pagesource).unwrap();
xxx.get(1).unwrap().as_str().to_string()
}

pub fn get_download_link(deckid: u32) -> String {
Expand All @@ -410,6 +412,5 @@ pub fn get_download_link(deckid: u32) -> String {
.unwrap()
.text()
.unwrap();
let link = extract_download_link(&result);
return link;
extract_download_link(&result)
}
2 changes: 1 addition & 1 deletion src/popups/edit_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
utils::{
area::{split_leftright_by_percent, split_updown_by_percent},
card::{CardTypeData, CardView},
sql::fetch::cards::{get_cardtype, get_cardtypedata},
sql::fetch::cards::get_cardtypedata,
},
widgets::infobox::InfoBox,
MyKey,
Expand Down
2 changes: 1 addition & 1 deletion src/popups/filepicker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<'a> FilePicker<'a> {
if let Some('/') = wtf {
break;
}
if let None = wtf {
if wtf.is_none() {
panic!("oh none");
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/popups/find_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,11 @@ impl StatefulList<CardMatch> {
});
}
self.items = matching_cards;
if self.items.len() == 0 {
if self.items.is_empty() {
self.state.select(None)
} else {
if let Some(idx) = self.state.selected() {
let new_index = std::cmp::min(idx, self.items.len() - 1);
self.state.select(Some(new_index));
}
} else if let Some(idx) = self.state.selected() {
let new_index = std::cmp::min(idx, self.items.len() - 1);
self.state.select(Some(new_index));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/popups/newchild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'a> Tab for AddChildWidget<'a> {
self.cardview.answer.set_area(answer);
}
fn render(&mut self, f: &mut Frame<MyType>, appdata: &AppData, cursor: &Pos) {
self.prompt.render(f, appdata, &cursor);
self.prompt.render(f, appdata, cursor);
self.cardview.render(f, appdata, cursor)
}

Expand Down
10 changes: 4 additions & 6 deletions src/popups/progress_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ impl Tab for Progress {
self.tabdata.state = PopUpState::Exit;
}
}
} else if let Some(tab) = std::mem::take(&mut self.next_tab) {
self.tabdata.state = PopUpState::Switch(tab);
} else {
if let Some(tab) = std::mem::take(&mut self.next_tab) {
self.tabdata.state = PopUpState::Switch(tab);
} else {
self.popupvalue = PopupValue::Ok;
self.tabdata.state = PopUpState::Exit;
}
self.popupvalue = PopupValue::Ok;
self.tabdata.state = PopUpState::Exit;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/tabs/import.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::{
app::{AppData, Tab},
popups::{
ankimporter::Ankimporter,
self,
filepicker::{FilePicker, FilePickerPurpose},
menu::{Menu, TraitButton},
},
};

impl<'a> Menu<'a> {
pub fn new_import_tab() -> Menu<'a> {
let a = |_appdata: &AppData| -> Box<dyn Tab> { Box::new(Ankimporter::new()) };
let a = |_appdata: &AppData| -> Box<dyn Tab> {
Box::<popups::ankimporter::Ankimporter<'_>>::default()
};
let b = |appdata: &AppData| -> Box<dyn Tab> { Box::new(Menu::new_anki_users(appdata)) };
let c = |_appdata: &AppData| -> Box<dyn Tab> {
Box::new(FilePicker::new(
Expand Down
2 changes: 1 addition & 1 deletion src/tabs/review/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl<'a> MainReview<'a> {
};

let color = modecolor(&self.mode);
let cursor = self.get_cursor().clone();
let cursor = *self.get_cursor();
self.progress_bar.current = current;
self.progress_bar.max = target;
self.progress_bar.color = color;
Expand Down
9 changes: 4 additions & 5 deletions src/tests/doc/databasetest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use rusqlite::Connection;
#[cfg(test)]
use crate::utils::misc::SpekiPaths;
use crate::utils::{
aliases::Conn,
card::{self, Card, CardType, CardTypeData, FinishedInfo, RecallGrade, Review, UnfinishedInfo},
card::{Card, CardTypeData, FinishedInfo, RecallGrade, Review, UnfinishedInfo},
interval::{calc_stability, strength_algo},
sql::{fetch::cards::fetch_card, init_db},
};
Expand All @@ -25,7 +24,7 @@ fn get_paths() -> SpekiPaths {
fn get_conn() -> Arc<Mutex<Connection>> {
let paths = get_paths();
Arc::new(Mutex::new(
Connection::open(&paths.database).expect("Failed to connect to database."),
Connection::open(paths.database).expect("Failed to connect to database."),
))
}

Expand Down Expand Up @@ -103,8 +102,8 @@ fn dependency_logic() {
.answer(answer.clone())
.save_card(&conn);
let card = fetch_card(&conn, id1);
assert_eq!(question.clone(), card.question.clone());
assert_eq!(answer.clone(), card.answer.clone());
assert_eq!(question, card.question);
assert_eq!(answer, card.answer);
assert!(Card::is_resolved(&conn, id1));
let id2 = Card::new(CardTypeData::Unfinished(UnfinishedInfo::default()))
.question("what is a capital".to_string())
Expand Down
Loading

0 comments on commit b60d3d7

Please sign in to comment.