diff options
author | Thang Pham <phamducthang1234@gmail.com> | 2021-06-06 15:23:09 +0900 |
---|---|---|
committer | Thang Pham <phamducthang1234@gmail.com> | 2021-06-06 15:23:09 +0900 |
commit | 285add4640b4a13444d6f8b88352993e5825ca82 (patch) | |
tree | fc3d9f08f3c39ba87450dd60a65d50e60b1274c7 | |
parent | 0e46cb452e6506a5a36ce8ae40f9ffb885ecf757 (diff) |
use clippy for linter
-rw-r--r-- | src/hn_client.rs | 31 | ||||
-rw-r--r-- | src/utils.rs | 2 | ||||
-rw-r--r-- | src/view/article_view.rs | 28 | ||||
-rw-r--r-- | src/view/comment_view.rs | 12 | ||||
-rw-r--r-- | src/view/help_view.rs | 14 | ||||
-rw-r--r-- | src/view/list_view.rs | 2 | ||||
-rw-r--r-- | src/view/search_view.rs | 2 | ||||
-rw-r--r-- | src/view/story_view.rs | 12 |
8 files changed, 55 insertions, 48 deletions
diff --git a/src/hn_client.rs b/src/hn_client.rs index 82f8d3d..cad2b9e 100644 --- a/src/hn_client.rs +++ b/src/hn_client.rs @@ -8,11 +8,11 @@ use std::{sync::Arc, sync::RwLock}; use crate::prelude::*; -const HN_ALGOLIA_PREFIX: &'static str = "https://hn.algolia.com/api/v1"; -const HN_OFFICIAL_PREFIX: &'static str = "https://hacker-news.firebaseio.com/v0"; -const HN_SEARCH_QUERY_STRING: &'static str = +const HN_ALGOLIA_PREFIX: &str = "https://hn.algolia.com/api/v1"; +const HN_OFFICIAL_PREFIX: &str = "https://hacker-news.firebaseio.com/v0"; +const HN_SEARCH_QUERY_STRING: &str = "tags=story&restrictSearchableAttributes=title,url&typoTolerance=false"; -pub const HN_HOST_URL: &'static str = "https://news.ycombinator.com"; +pub const HN_HOST_URL: &str = "https://news.ycombinator.com"; // serde helper functions @@ -199,7 +199,7 @@ impl From<StoryResponse> for Story { Story { title: s.title.unwrap(), url: s.url.unwrap_or_default(), - author: s.author.unwrap_or(String::from("[deleted]")), + author: s.author.unwrap_or_else(|| String::from("[deleted]")), id: s.id, points: s.points, num_comments: s.num_comments, @@ -265,7 +265,7 @@ impl LazyLoadingComments { /// then request comments with the corresponding ids. /// parameter `block` determines whether the retrieving process should happen in background pub fn drain(&mut self, size: usize, block: bool) { - if self.ids.len() == 0 { + if self.ids.is_empty() { return; } @@ -386,7 +386,7 @@ impl StoryNumericFilters { self.points_interval.query("points"), self.num_comments_interval.query("num_comments") ); - if query.len() > 0 { + if !query.is_empty() { query.remove(0); // remove trailing , format!("&numericFilters={}", query) } else { @@ -442,16 +442,13 @@ impl HNClient { .call()? .into_json::<HNStoryResponse>()? .kids; - match ids.iter().position(|id| *id == focus_top_comment_id) { - Some(pos) => { - // move `pos` to the beginning of the list. - // using `reverse` twice with `swap_remove` is quite - // an inefficient way to achieve the above but probably the easiest - ids.reverse(); - ids.swap_remove(pos); - ids.reverse(); - } - None => {} + if let Some(pos) = ids.iter().position(|id| *id == focus_top_comment_id) { + // move `pos` to the beginning of the list. + // using `reverse` twice with `swap_remove` is quite + // an inefficient way to achieve the above but probably the easiest + ids.reverse(); + ids.swap_remove(pos); + ids.reverse(); }; let mut comments = LazyLoadingComments::new(self.clone(), ids); diff --git a/src/utils.rs b/src/utils.rs index 91cde6e..87956a7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -106,7 +106,7 @@ pub fn get_story_view_desc_by_tag(tag: &str) -> String { /// open a given url using a specific command pub fn open_url_in_browser(url: &str) { - if url.len() == 0 { + if url.is_empty() { return; } diff --git a/src/view/article_view.rs b/src/view/article_view.rs index d925bad..4f48b98 100644 --- a/src/view/article_view.rs +++ b/src/view/article_view.rs @@ -80,7 +80,7 @@ impl Article { } else { link.to_string() }; - let desc = if desc.len() == 0 { + let desc = if desc.is_empty() { format!("\"{}\"", shorten_url(&link)) } else { desc @@ -96,7 +96,7 @@ impl Article { prefix.drain(range); prefix += prefix_char; - if prefix.len() > 0 { + if !prefix.is_empty() { styled_s.append_plain( md_escape_char_re .replace_all(&&prefix, "${char}") @@ -109,7 +109,7 @@ impl Article { Style::from(get_config_theme().link_text.color), ); - let valid_url = link.len() > 0; + let valid_url = !link.is_empty(); styled_s.append_styled( if valid_url { format!("[{}]", links.len()) @@ -122,7 +122,7 @@ impl Article { ), ); - if link.len() > 0 { + if !link.is_empty() { // valid url links.push(link.to_string()); } @@ -130,7 +130,7 @@ impl Article { } } } - if s.len() > 0 { + if !s.is_empty() { styled_s.append_plain(md_escape_char_re.replace_all(&s, "${char}").to_string()); } (styled_s, links) @@ -152,8 +152,10 @@ impl ArticleView { let desc = format!( "by: {}, date_published: {}, word_count: {}\n\n", - article.author.unwrap_or("[unknown]".to_string()), - article.date_published.unwrap_or("[unknown]".to_string()), + article.author.unwrap_or_else(|| "[unknown]".to_string()), + article + .date_published + .unwrap_or_else(|| "[unknown]".to_string()), if article.word_count > 1 { article.word_count.to_string() } else { @@ -192,7 +194,7 @@ impl ArticleView { } /// Construct a help dialog from a list of URLs -pub fn get_link_dialog(links: &Vec<String>) -> impl View { +pub fn get_link_dialog(links: &[String]) -> impl View { let article_view_keymap = get_article_view_keymap().clone(); let links_view = OnEventView::new(LinearLayout::vertical().with(|v| { @@ -218,7 +220,7 @@ pub fn get_link_dialog(links: &Vec<String>) -> impl View { Some(EventResult::Consumed(None)) }) .on_pre_event_inner(article_view_keymap.open_link_in_browser, { - let links = links.clone(); + let links = links.to_owned(); move |s, _| { let focus_id = s.get_focus_index(); open_url_in_browser(&links[focus_id]); @@ -226,7 +228,7 @@ pub fn get_link_dialog(links: &Vec<String>) -> impl View { } }) .on_pre_event_inner(article_view_keymap.open_link_in_article_view, { - let links = links.clone(); + let links = links.to_owned(); move |s, _| { let focus_id = s.get_focus_index(); let url = links[focus_id].clone(); @@ -262,7 +264,7 @@ pub fn get_article_main_view(article: Article, raw_md: bool) -> OnEventView<Arti OnEventView::new(ArticleView::new(article, raw_md)) .on_pre_event_inner(EventTrigger::from_fn(|_| true), move |s, e| { match *e { - Event::Char(c) if '0' <= c && c <= '9' => { + Event::Char(c) if ('0'..='9').contains(&c) => { s.raw_command.push(c); } _ => { @@ -306,7 +308,9 @@ pub fn get_article_main_view(article: Article, raw_md: bool) -> OnEventView<Arti .on_pre_event_inner(article_view_keymap.open_link_dialog, |s, _| { Some(EventResult::with_cb({ let links = s.links.clone(); - move |s| s.add_layer(get_link_dialog(&links)) + move |s| { + s.add_layer(get_link_dialog(&links)); + } })) }) .on_pre_event_inner(article_view_keymap.open_link_in_browser, |s, _| { diff --git a/src/view/comment_view.rs b/src/view/comment_view.rs index 683d888..7be0754 100644 --- a/src/view/comment_view.rs +++ b/src/view/comment_view.rs @@ -112,7 +112,7 @@ impl CommentView { } fn decode_html(s: &str) -> String { - htmlescape::decode_html(s).unwrap_or(s.to_string()) + htmlescape::decode_html(s).unwrap_or_else(|_| s.to_string()) } /// Parse a comment in HTML text style to markdown text style (with colors) @@ -152,7 +152,7 @@ impl CommentView { .collect(); prefix.drain(range); - if prefix.len() > 0 { + if !prefix.is_empty() { styled_s.append_plain(Self::decode_html(&prefix)); } @@ -172,7 +172,7 @@ impl CommentView { } } } - if s.len() > 0 { + if !s.is_empty() { styled_s.append_plain(Self::decode_html(&s)); } (styled_s, links) @@ -180,7 +180,7 @@ impl CommentView { /// Parse comments recursively into readable texts with styles and colors fn parse_comments( - comments: &Vec<hn_client::Comment>, + comments: &[hn_client::Comment], height: usize, top_comment_id: u32, ) -> Vec<Comment> { @@ -254,7 +254,7 @@ fn get_comment_main_view( OnEventView::new(CommentView::new(story.clone(), comments, focus_id)) .on_pre_event_inner(EventTrigger::from_fn(|_| true), move |s, e| { match *e { - Event::Char(c) if '0' <= c && c <= '9' => { + Event::Char(c) if ('0'..='9').contains(&c) => { s.raw_command.push(c); } _ => { @@ -432,7 +432,7 @@ pub fn get_comment_view( { let url = story.url.clone(); move |s| { - if url.len() > 0 { + if !url.is_empty() { article_view::add_article_view_layer(s, url.clone()) } } diff --git a/src/view/help_view.rs b/src/view/help_view.rs index 146ad6a..59ee707 100644 --- a/src/view/help_view.rs +++ b/src/view/help_view.rs @@ -38,7 +38,7 @@ impl HelpView { }) } - fn construct_keys_view(keys: &Vec<(String, String)>) -> impl View { + fn construct_keys_view(keys: &[(String, String)]) -> impl View { let max_key_len = match keys.iter().max_by_key(|key| key.0.len()) { None => 0, Some(key) => key.0.len(), @@ -78,7 +78,7 @@ impl HelpView { .max_height(32) } - pub fn to_keys<X: Display, Y: Display>(keys: Vec<(X, Y)>) -> Vec<(String, String)> { + pub fn process_keys<X: Display, Y: Display>(keys: Vec<(X, Y)>) -> Vec<(String, String)> { keys.into_iter() .map(|(key, desc)| (key.to_string(), desc.to_string())) .collect() @@ -91,7 +91,7 @@ impl HelpView { ) -> Self { let mut key_groups: Vec<(&'static str, Vec<(String, String)>)> = key_groups .into_iter() - .map(|(group_desc, keys)| (group_desc, Self::to_keys(keys))) + .map(|(group_desc, keys)| (group_desc, Self::process_keys(keys))) .collect(); self.key_groups.append(&mut key_groups); let view = self.construct_key_groups_view(); @@ -100,6 +100,12 @@ impl HelpView { } } +impl Default for HelpView { + fn default() -> Self { + Self::new() + } +} + impl ViewWrapper for HelpView { wrap_impl!(self.view: OnEventView<Dialog>); } @@ -236,7 +242,7 @@ impl HasHelpView for StoryView { ], ), ]); - if custom_keymaps.len() > 0 { + if !custom_keymaps.is_empty() { help_view = help_view.key_groups(vec![("Custom keymaps", custom_keymaps)]); } help_view.key_groups(vec![ diff --git a/src/view/list_view.rs b/src/view/list_view.rs index 396b47e..a55b173 100644 --- a/src/view/list_view.rs +++ b/src/view/list_view.rs @@ -73,7 +73,7 @@ macro_rules! impl_scrollable_list { fn set_focus_index(&mut self, id: usize) -> Option<EventResult> { let current_id = self.get_focus_index(); - let direction = if (current_id <= id) { true } else { false }; + let direction = (current_id <= id); let linear_layout = self.get_inner_mut().get_inner_mut(); match linear_layout.set_focus_index(id) { diff --git a/src/view/search_view.rs b/src/view/search_view.rs index a106c9a..a803b8e 100644 --- a/src/view/search_view.rs +++ b/src/view/search_view.rs @@ -83,7 +83,7 @@ impl SearchView { fn update_view(&mut self) { if self.query.read().unwrap().1 { let stories = self.stories.read().unwrap().clone(); - if stories.len() == 0 { + if stories.is_empty() { self.mode = SearchViewMode::Search; }; diff --git a/src/view/story_view.rs b/src/view/story_view.rs index 7128dc7..921f8a0 100644 --- a/src/view/story_view.rs +++ b/src/view/story_view.rs @@ -59,7 +59,7 @@ impl StoryView { .collect(); prefix.drain(range); - if prefix.len() > 0 { + if !prefix.is_empty() { styled_s.append_styled(&prefix, default_style); } @@ -74,7 +74,7 @@ impl StoryView { } }; } - if s.len() > 0 { + if !s.is_empty() { styled_s.append_styled(s, default_style); } styled_s @@ -84,7 +84,7 @@ impl StoryView { fn get_story_text(story: &hn_client::Story) -> StyledString { let mut story_text = Self::get_matched_text(story.highlight_result.title.clone(), ColorStyle::default()); - if story.url.len() > 0 { + if !story.url.is_empty() { let url = format!("\n{}", story.highlight_result.url); story_text.append(Self::get_matched_text( url, @@ -123,7 +123,7 @@ pub fn get_story_main_view( // number parsing .on_pre_event_inner(EventTrigger::from_fn(|_| true), move |s, e| { match *e { - Event::Char(c) if '0' <= c && c <= '9' => { + Event::Char(c) if ('0'..='9').contains(&c) => { s.raw_command.push(c); } _ => { @@ -171,7 +171,7 @@ pub fn get_story_main_view( move |s, _| { let id = s.get_focus_index(); let url = s.stories[id].url.clone(); - if url.len() > 0 { + if !url.is_empty() { Some(EventResult::with_cb({ move |s| article_view::add_article_view_layer(s, url.clone()) })) @@ -217,7 +217,7 @@ pub fn get_story_view( numeric_filters: hn_client::StoryNumericFilters, ) -> impl View { let starting_id = get_config().client.story_limit.get_story_limit_by_tag(tag) * page; - let main_view = get_story_main_view(stories.clone(), client, starting_id).full_height(); + let main_view = get_story_main_view(stories, client, starting_id).full_height(); let mut view = LinearLayout::vertical() .child(get_status_bar_with_desc(desc)) |