From a97ad6ac6204ac785e3ef757fe8898ca88c5fbd3 Mon Sep 17 00:00:00 2001 From: Till Schulte Date: Fri, 26 Jan 2024 00:20:32 +0100 Subject: [PATCH] linting and housekeeping --- Cargo.toml | 4 ++-- example/main.rs | 2 +- src/cloud/word.rs | 13 ++++++------- src/cloud/word_cloud.rs | 12 ++---------- src/common/font.rs | 13 +++++-------- src/filtering/stop_words.rs | 2 +- src/types/point.rs | 2 +- src/types/rect.rs | 12 ------------ src/types/rotation.rs | 4 ++-- 9 files changed, 20 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index de7a434..817f5f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ doctest = false [dependencies] rayon = "1.6.1" -svg = "0.13.0" +svg = "0.14.0" image = { version = "0.24.5", optional = true } edge-detection = { version = "0.2.6", optional = true } rand = { version = "0.8.5", features = ["small_rng"] } @@ -43,7 +43,7 @@ parking_lot = "0.12.1" quadtree_rs = "0.1.3" num-traits = "0.2.15" swash = "0.1.6" -itertools = "0.10.5" +itertools = "0.12.0" base64 = "0.21.2" unicode-script = "0.5.5" woff2 = { version = "0.3.0", optional = true } diff --git a/example/main.rs b/example/main.rs index 3a3117d..d4747ff 100644 --- a/example/main.rs +++ b/example/main.rs @@ -1,7 +1,7 @@ use std::env::args; use rayon::prelude::ParallelString; use wordcloud::font::{Font, FontSetBuilder}; -use wordcloud::{StopWordsIterator, StopWordsIteratorPar}; +use wordcloud::StopWordsIteratorPar; use wordcloud::{clean, Dimensions, WordCloudBuilder}; use wordcloud::{RankedWords, StopWords}; use rayon::iter::ParallelIterator; diff --git a/src/cloud/word.rs b/src/cloud/word.rs index 2c696f0..35aa38d 100644 --- a/src/cloud/word.rs +++ b/src/cloud/word.rs @@ -1,8 +1,5 @@ use std::cell::RefCell; use std::ops::Range; -use svg::Node; -use svg::node::element::{Rectangle, Text}; -use swash::scale::outline::Outline; use swash::scale::ScaleContext; use swash::shape::Direction::LeftToRight; use swash::shape::ShapeContext; @@ -87,7 +84,7 @@ impl<'a> Word<'a> { .iter() .map(|glyph| { let outline = - scaler.scale_outline(glyph.id).unwrap_or(Outline::new()); + scaler.scale_outline(glyph.id).unwrap_or_default(); let bounds = outline.bounds(); let bbox = Rect { @@ -328,7 +325,7 @@ impl<'a> Word<'a> { false } - pub(crate) fn get_font_size_range(&self, dimensions: Dimensions) -> Range { + pub(crate) fn _get_font_size_range(&self, dimensions: Dimensions) -> Range { let max = if self.rotation == Rotation::Ninety || self.rotation == Rotation::TwoSeventy { // compare mainly with height dimensions.width() as f32 * 0.8 @@ -400,6 +397,8 @@ impl<'a> WordBuilder<'a> { #[test] fn test() { + use svg::Node; + use svg::node::element::Text; let mut font = Vec::new(); font.extend_from_slice(include_bytes!("../../example/assets/OpenSans-Regular.ttf")); let f = Font::from_data(&mut font).unwrap(); @@ -417,7 +416,7 @@ fn test() { .set("stroke-width", 1) .set("d", word.d()); - let p2 = Rectangle::new() + let p2 = svg::node::element::Rectangle::new() .set("stroke", "green") .set("stroke-width", 1) .set("fill", "none") @@ -440,5 +439,5 @@ fn test() { dbg!(word.bounding_box); - svg::save("test.svg", &document); + svg::save("test.svg", &document).expect("TODO: panic message"); } diff --git a/src/cloud/word_cloud.rs b/src/cloud/word_cloud.rs index b8e19fb..9dd39ba 100644 --- a/src/cloud/word_cloud.rs +++ b/src/cloud/word_cloud.rs @@ -97,13 +97,13 @@ impl<'a> WordCloud<'a> { let search_area = AreaBuilder::default() .anchor((pos_x as u64, pos_y as u64).into()) - .dimensions(((4.) as u64, (4.) as u64)) + .dimensions((4. as u64, 4. as u64)) .build() .expect("Error while calculating dimensions"); let insert_area = AreaBuilder::default() .anchor(((x as f32) as u64, (y as f32) as u64).into()) - .dimensions(((1.) as u64, (1.) as u64)) + .dimensions((1. as u64, 1. as u64)) .build() .expect("Error while calculating dimensions"); @@ -317,14 +317,6 @@ impl<'a> WordCloud<'a> { Add new words to the [`WordCloud`]. For the best results, call this function only once. */ pub fn write_content(&self, content: RankedWords, max_word_count: usize) { - let max = content - .0 - .iter() - .take(max_word_count) - .map(|x| (x.count() as f32)) - .sum::() - / max_word_count as f32; - let max = content.0.iter().max_by_key(|x| x.count()).unwrap().count() as f32; let inp: Vec = content diff --git a/src/common/font.rs b/src/common/font.rs index dea1000..c149029 100644 --- a/src/common/font.rs +++ b/src/common/font.rs @@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher}; use std::io::Cursor; use std::sync::Arc; -use swash::text::{Codepoint, Script}; +use swash::text::{Codepoint}; use swash::scale::ScaleContext; use swash::{FontRef, StringId, Tag}; @@ -42,7 +42,7 @@ pub type FontLoadingError = String; pub type FontLoadingResult = Result; /** - Represents a Font stored in memory. By default it supports `OTF` and `TTF` fonts, with + Represents a Font stored in memory. By default, it supports `OTF` and `TTF` fonts, with the create features `woff` and `woff2` it also supports loading `WOFF` fonts. */ pub struct Font<'a> { @@ -51,7 +51,7 @@ pub struct Font<'a> { font_type: FontType, supported_scripts: HashSet, packed_font_data: Option>, - approximate_pixel_width: f32, + _approximate_pixel_width: f32, } impl<'a> Font<'a> { @@ -77,7 +77,7 @@ impl<'a> Font<'a> { } #[allow(clippy::unwrap_used)] - scripts_in_specs.insert(CScript::try_from(Script::Common).unwrap()); + scripts_in_specs.insert(CScript::try_from(swash::text::Script::Common).unwrap()); scripts_in_specs } @@ -148,7 +148,7 @@ impl<'a> Font<'a> { font_type, supported_scripts: Font::identify_scripts_in_font(&re), packed_font_data: packed_data, - approximate_pixel_width: outline.bounds().width() / 20., + _approximate_pixel_width: outline.bounds().width() / 20., }) } @@ -168,9 +168,6 @@ impl<'a> Font<'a> { &self.packed_font_data } - pub(crate) fn approximate_pixel_width(&self) -> f32 { - self.approximate_pixel_width - } #[allow(dead_code)] pub(crate) fn supported_features(&self) -> impl IntoIterator + '_ { self.reference().features().map(|f| (f.tag(), 1)) diff --git a/src/filtering/stop_words.rs b/src/filtering/stop_words.rs index 7bb96a0..ab4584b 100644 --- a/src/filtering/stop_words.rs +++ b/src/filtering/stop_words.rs @@ -55,7 +55,7 @@ impl StopWords { let entry = self .stop_word_map .entry(script) - .or_insert_with(HashSet::new); + .or_default(); entry.insert(word.clone()); } } diff --git a/src/types/point.rs b/src/types/point.rs index 72beab6..d4ad339 100644 --- a/src/types/point.rs +++ b/src/types/point.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, Mul, Range, Sub}; +use std::ops::{Add, Mul, Sub}; #[derive(Copy, Clone, Debug)] pub(crate) struct Point { diff --git a/src/types/rect.rs b/src/types/rect.rs index c9398bd..8471f15 100644 --- a/src/types/rect.rs +++ b/src/types/rect.rs @@ -205,18 +205,6 @@ where } } -impl Rect -where - T: PartialOrd + Copy + Sized, -{ - pub(crate) fn get_intersection(&self, rhs: &Rect) -> Rect { - Rect { - min: self.min.max(&rhs.min), - max: self.max.min(&rhs.max), - } - } -} - impl Rect where T: Copy + Sub, diff --git a/src/types/rotation.rs b/src/types/rotation.rs index 3ca888e..4eae689 100644 --- a/src/types/rotation.rs +++ b/src/types/rotation.rs @@ -1,6 +1,6 @@ use crate::types::point::Point; use crate::types::rect::Rect; -use rand::{thread_rng, Rng}; +use rand::random; #[derive(Copy, Clone, Debug, PartialEq)] #[allow(dead_code)] @@ -72,7 +72,7 @@ impl Rotation { } pub(crate) fn random() -> Self { - let (a, b): (bool, bool) = (thread_rng().gen(), thread_rng().gen()); + let (a, b): (bool, bool) = (random(), random()); match (a, b) { (true, true) => Rotation::Zero, (true, false) => Rotation::Ninety,