From 32b4800b466e1f919792d34ea87c907b678d86b0 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Wed, 31 May 2023 12:14:11 +0200 Subject: [PATCH] fix!: remove possible panic from client builder fix: simplify integer divisions --- cli/src/main.rs | 5 ++++- codegen/src/collect_video_dates.rs | 3 ++- codegen/src/download_testfiles.rs | 3 ++- src/client/mod.rs | 12 ++++++------ src/client/player.rs | 5 ++--- src/lib.rs | 3 +-- src/util/mod.rs | 11 +++++++++++ src/util/timeago.rs | 10 +++++----- tests/youtube.rs | 9 +++++++-- 9 files changed, 40 insertions(+), 21 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 0a670ef..4cd1142 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -393,7 +393,10 @@ async fn main() { storage_dir.push("rustypipe"); _ = std::fs::create_dir(&storage_dir); - let rp = RustyPipe::builder().storage_dir(storage_dir).build(); + let rp = RustyPipe::builder() + .storage_dir(storage_dir) + .build() + .unwrap(); match cli.command { Commands::Download { diff --git a/codegen/src/collect_video_dates.rs b/codegen/src/collect_video_dates.rs index cff90e5..472df8c 100644 --- a/codegen/src/collect_video_dates.rs +++ b/codegen/src/collect_video_dates.rs @@ -16,7 +16,8 @@ pub async fn collect_video_dates(concurrency: usize) { let json_path = path!(*DICT_DIR / "timeago_samples_short.json"); let rp = RustyPipe::builder() .visitor_data("Cgtwel9tMkh2eHh0USiyzc6jBg%3D%3D") - .build(); + .build() + .unwrap(); let channels = [ "UCeY0bbntWzzVIaj2z3QigXg", diff --git a/codegen/src/download_testfiles.rs b/codegen/src/download_testfiles.rs index 41daacc..0d1357f 100644 --- a/codegen/src/download_testfiles.rs +++ b/codegen/src/download_testfiles.rs @@ -134,6 +134,7 @@ fn rp_testfile(json_path: &Path) -> RustyPipe { .report() .strict() .build() + .unwrap() } async fn player() { @@ -155,7 +156,7 @@ async fn player() { } async fn player_model() { - let rp = RustyPipe::builder().strict().build(); + let rp = RustyPipe::builder().strict().build().unwrap(); for (name, id) in [("multilanguage", "tVWWp1PqDus"), ("hdr", "LXb3EKWsInQ")] { let json_path = diff --git a/src/client/mod.rs b/src/client/mod.rs index 22fc832..761667c 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -446,8 +446,7 @@ impl RustyPipeBuilder { } /// Return a new, configured RustyPipe instance. - #[must_use] - pub fn build(self) -> RustyPipe { + pub fn build(self) -> Result { let mut client_builder = ClientBuilder::new() .user_agent(self.user_agent.unwrap_or_else(|| DEFAULT_UA.to_owned())) .gzip(true) @@ -458,7 +457,7 @@ impl RustyPipeBuilder { client_builder = client_builder.timeout(timeout); } - let http = client_builder.build().unwrap(); + let http = client_builder.build()?; let storage_dir = self.storage_dir.unwrap_or_default(); @@ -480,7 +479,7 @@ impl RustyPipeBuilder { }) .unwrap_or_default(); - RustyPipe { + Ok(RustyPipe { inner: Arc::new(RustyPipeRef { http, storage, @@ -503,7 +502,7 @@ impl RustyPipeBuilder { }, default_opts: self.default_opts, }), - } + }) } /// Set the default directory to store the cachefile and reports. @@ -687,8 +686,9 @@ impl RustyPipe { /// /// To create an instance with custom options, use [`RustyPipeBuilder`] instead. #[must_use] + #[allow(clippy::missing_panics_doc)] pub fn new() -> Self { - RustyPipeBuilder::new().build() + RustyPipeBuilder::new().build().unwrap() } /// Create a new [`RustyPipeBuilder`] diff --git a/src/client/player.rs b/src/client/player.rs index 4061aac..9f74ef9 100644 --- a/src/client/player.rs +++ b/src/client/player.rs @@ -342,9 +342,8 @@ impl MapResponse for response::Player { + "&sigh=" + sigh; - let sprite_count = (f64::from(total_count) - / f64::from(frames_per_page_x * frames_per_page_y)) - .ceil() as u32; + let sprite_count = + util::div_ceil(total_count, frames_per_page_x * frames_per_page_y); Some(Frameset { url_template: url, diff --git a/src/lib.rs b/src/lib.rs index 3ea4373..e6868d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,8 +11,7 @@ clippy::cast_sign_loss, clippy::cast_precision_loss, clippy::single_match_else, - clippy::missing_errors_doc, - clippy::missing_panics_doc + clippy::missing_errors_doc )] //! ## Go to diff --git a/src/util/mod.rs b/src/util/mod.rs index 5f05ae4..f148c6f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -272,6 +272,17 @@ pub fn sanitize_yt_url(url: &str) -> String { sanitize_yt_url_inner(url).unwrap_or_else(|| url.to_string()) } +pub fn div_ceil(a: u32, b: u32) -> u32 { + let d = a / b; + let r = a % b; + + if r > 0 && b > 0 { + d + 1 + } else { + d + } +} + pub trait TryRemove { /// Removes and returns the element at position `index` within the vector, /// shifting all elements after it to the left. diff --git a/src/util/timeago.rs b/src/util/timeago.rs index 5c90b16..f3b42f4 100644 --- a/src/util/timeago.rs +++ b/src/util/timeago.rs @@ -77,7 +77,7 @@ pub enum DateCmp { } impl TimeUnit { - pub fn secs(self) -> i64 { + pub fn secs(self) -> u32 { match self { TimeUnit::Second => 1, TimeUnit::Minute => 60, @@ -91,8 +91,8 @@ impl TimeUnit { } impl TimeAgo { - fn secs(self) -> i64 { - i64::from(self.n) * self.unit.secs() + fn secs(self) -> u32 { + u32::from(self.n) * self.unit.secs() } } @@ -109,7 +109,7 @@ impl Mul for TimeAgo { impl From for Duration { fn from(ta: TimeAgo) -> Self { - Duration::seconds(ta.secs()) + Duration::seconds(ta.secs().into()) } } @@ -331,7 +331,7 @@ pub fn parse_video_duration(lang: Language, video_duration: &str) -> Option tokens.peek()?; tokens.for_each(|ta| { - secs += n * ta.secs() as u32; + secs += n * ta.secs(); n = 1; }); } diff --git a/tests/youtube.rs b/tests/youtube.rs index 3aacb2f..7982e6a 100644 --- a/tests/youtube.rs +++ b/tests/youtube.rs @@ -1695,7 +1695,7 @@ fn music_search_videos(rp: RustyPipe, unlocalized: bool) { #[tokio::test] async fn music_search_episode() { - let rp = RustyPipe::builder().strict().build(); + let rp = RustyPipe::builder().strict().build().unwrap(); let res = rp .query() .music_search_videos("Blond - Da muss man dabei gewesen sein: Das Hörspiel - Fall #1") @@ -2342,6 +2342,7 @@ fn rp(lang: Language) -> RustyPipe { .lang(lang) .visitor_data_opt(vdata) .build() + .unwrap() } /// Get a flag signaling if the language is set to English @@ -2352,7 +2353,11 @@ fn unlocalized(lang: Language) -> bool { /// Get a new RustyPipe instance with pre-set visitor data fn rp_visitor_data(vdata: &str) -> RustyPipe { - RustyPipe::builder().strict().visitor_data(vdata).build() + RustyPipe::builder() + .strict() + .visitor_data(vdata) + .build() + .unwrap() } /// Assert equality within 10% margin