Skip to content

Commit

Permalink
Use more lints
Browse files Browse the repository at this point in the history
  • Loading branch information
theodore-s-beers committed Feb 24, 2024
1 parent fabb572 commit bcb6498
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]

use std::io::Write;
use std::process::Command;
use std::str;
use std::str; // For str::from_utf8

use anyhow::Context;
use regex::Regex;
use scraper::{ElementRef, Html, Selector};
use tempfile::NamedTempFile;

#[must_use]
// Take list of elements and compile them into a string (as appropriate)
pub fn compile_results(etym_mode: bool, section_vec: Vec<ElementRef>) -> String {
let mut results = String::new();

if etym_mode {
// If etymology, just push everything from any sections
for section in section_vec.iter() {
for section in section_vec {
results.push_str(&section.html());
}
} else {
Expand All @@ -39,12 +43,14 @@ pub fn get_response_text(lookup_url: &str) -> Result<String, anyhow::Error> {
Ok(response_text)
}

#[must_use]
// Cull certain elements from the HTML fragment, based on CSS selectors
pub fn get_section_vec(etym_mode: bool, parsed_chunk: &Html) -> Vec<ElementRef> {
// Set up a selector for the relevant section
let section_selector = match etym_mode {
true => Selector::parse(r#"div[class^="word--"]:not([class*="word_4pc"]) h1, div[class^="word--"]:not([class*="word_4pc"]) p"#).unwrap(),
_ => Selector::parse(r#"div#Definition section[data-src="hm"]"#).unwrap(),
let section_selector = if etym_mode {
Selector::parse(r#"div[class^="word--"]:not([class*="word_4pc"]) h1, div[class^="word--"]:not([class*="word_4pc"]) p"#).unwrap()
} else {
Selector::parse(r#"div#Definition section[data-src="hm"]"#).unwrap()
};

// Run the select iterator and collect the result(s) in a vec
Expand Down Expand Up @@ -125,6 +131,7 @@ pub fn pandoc_primary(etym_mode: bool, results: &str) -> Result<String, anyhow::
}
}

#[must_use]
// Take only part of the response text, for faster parsing
pub fn take_chunk(response_text: &str) -> Html {
// In definition mode, we split the document
Expand Down
23 changes: 13 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![warn(clippy::pedantic, clippy::cargo)]
#![warn(clippy::pedantic, clippy::nursery, clippy::cargo)]

use core::time::Duration;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
Expand Down Expand Up @@ -81,11 +82,13 @@ fn main() -> Result<(), anyhow::Error> {
let cache_dir = proj_dirs.cache_dir();

// If we have clear-cache flag, handle it and return
if clear_cache && cache_dir.exists() {
trash::delete(cache_dir)?;
eprintln!("Cache directory deleted");
return Ok(());
} else if clear_cache {
if clear_cache {
if cache_dir.exists() {
trash::delete(cache_dir)?;
eprintln!("Cache directory deleted");
return Ok(());
}

return Err(anyhow!("Cache directory not found"));
}

Expand Down Expand Up @@ -145,7 +148,7 @@ fn main() -> Result<(), anyhow::Error> {

// Start a progress spinner; this could take a second
let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::from_millis(80));
pb.enable_steady_tick(Duration::from_millis(80));
pb.set_style(
ProgressStyle::default_spinner()
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])
Expand All @@ -158,10 +161,10 @@ fn main() -> Result<(), anyhow::Error> {
let mut lookup_url: String;

if etym_mode {
lookup_url = "https://www.etymonline.com/word/".to_string();
lookup_url = "https://www.etymonline.com/word/".to_owned();
lookup_url += &desired_word.replace(' ', "%20");
} else {
lookup_url = "https://www.thefreedictionary.com/".to_string();
lookup_url = "https://www.thefreedictionary.com/".to_owned();
lookup_url += &desired_word.replace(' ', "+");
}

Expand Down Expand Up @@ -255,7 +258,7 @@ fn pandoc_fallback(results: &str) -> Result<String, anyhow::Error> {

let pandoc_output = str::from_utf8(&pandoc.stdout)
.context("Failed to convert Pandoc output to string")?
.to_string();
.to_owned();

Ok(pandoc_output)
}
Expand Down

0 comments on commit bcb6498

Please sign in to comment.