fix!: remove possible panic from client builder

fix: simplify integer divisions
This commit is contained in:
ThetaDev 2023-05-31 12:14:11 +02:00
parent 182f9ebfb8
commit 32b4800b46
9 changed files with 40 additions and 21 deletions

View file

@ -393,7 +393,10 @@ async fn main() {
storage_dir.push("rustypipe"); storage_dir.push("rustypipe");
_ = std::fs::create_dir(&storage_dir); _ = 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 { match cli.command {
Commands::Download { Commands::Download {

View file

@ -16,7 +16,8 @@ pub async fn collect_video_dates(concurrency: usize) {
let json_path = path!(*DICT_DIR / "timeago_samples_short.json"); let json_path = path!(*DICT_DIR / "timeago_samples_short.json");
let rp = RustyPipe::builder() let rp = RustyPipe::builder()
.visitor_data("Cgtwel9tMkh2eHh0USiyzc6jBg%3D%3D") .visitor_data("Cgtwel9tMkh2eHh0USiyzc6jBg%3D%3D")
.build(); .build()
.unwrap();
let channels = [ let channels = [
"UCeY0bbntWzzVIaj2z3QigXg", "UCeY0bbntWzzVIaj2z3QigXg",

View file

@ -134,6 +134,7 @@ fn rp_testfile(json_path: &Path) -> RustyPipe {
.report() .report()
.strict() .strict()
.build() .build()
.unwrap()
} }
async fn player() { async fn player() {
@ -155,7 +156,7 @@ async fn player() {
} }
async fn player_model() { 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")] { for (name, id) in [("multilanguage", "tVWWp1PqDus"), ("hdr", "LXb3EKWsInQ")] {
let json_path = let json_path =

View file

@ -446,8 +446,7 @@ impl RustyPipeBuilder {
} }
/// Return a new, configured RustyPipe instance. /// Return a new, configured RustyPipe instance.
#[must_use] pub fn build(self) -> Result<RustyPipe, Error> {
pub fn build(self) -> RustyPipe {
let mut client_builder = ClientBuilder::new() let mut client_builder = ClientBuilder::new()
.user_agent(self.user_agent.unwrap_or_else(|| DEFAULT_UA.to_owned())) .user_agent(self.user_agent.unwrap_or_else(|| DEFAULT_UA.to_owned()))
.gzip(true) .gzip(true)
@ -458,7 +457,7 @@ impl RustyPipeBuilder {
client_builder = client_builder.timeout(timeout); 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(); let storage_dir = self.storage_dir.unwrap_or_default();
@ -480,7 +479,7 @@ impl RustyPipeBuilder {
}) })
.unwrap_or_default(); .unwrap_or_default();
RustyPipe { Ok(RustyPipe {
inner: Arc::new(RustyPipeRef { inner: Arc::new(RustyPipeRef {
http, http,
storage, storage,
@ -503,7 +502,7 @@ impl RustyPipeBuilder {
}, },
default_opts: self.default_opts, default_opts: self.default_opts,
}), }),
} })
} }
/// Set the default directory to store the cachefile and reports. /// 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. /// To create an instance with custom options, use [`RustyPipeBuilder`] instead.
#[must_use] #[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn new() -> Self { pub fn new() -> Self {
RustyPipeBuilder::new().build() RustyPipeBuilder::new().build().unwrap()
} }
/// Create a new [`RustyPipeBuilder`] /// Create a new [`RustyPipeBuilder`]

View file

@ -342,9 +342,8 @@ impl MapResponse<VideoPlayer> for response::Player {
+ "&sigh=" + "&sigh="
+ sigh; + sigh;
let sprite_count = (f64::from(total_count) let sprite_count =
/ f64::from(frames_per_page_x * frames_per_page_y)) util::div_ceil(total_count, frames_per_page_x * frames_per_page_y);
.ceil() as u32;
Some(Frameset { Some(Frameset {
url_template: url, url_template: url,

View file

@ -11,8 +11,7 @@
clippy::cast_sign_loss, clippy::cast_sign_loss,
clippy::cast_precision_loss, clippy::cast_precision_loss,
clippy::single_match_else, clippy::single_match_else,
clippy::missing_errors_doc, clippy::missing_errors_doc
clippy::missing_panics_doc
)] )]
//! ## Go to //! ## Go to

View file

@ -272,6 +272,17 @@ pub fn sanitize_yt_url(url: &str) -> String {
sanitize_yt_url_inner(url).unwrap_or_else(|| url.to_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<T> { pub trait TryRemove<T> {
/// Removes and returns the element at position `index` within the vector, /// Removes and returns the element at position `index` within the vector,
/// shifting all elements after it to the left. /// shifting all elements after it to the left.

View file

@ -77,7 +77,7 @@ pub enum DateCmp {
} }
impl TimeUnit { impl TimeUnit {
pub fn secs(self) -> i64 { pub fn secs(self) -> u32 {
match self { match self {
TimeUnit::Second => 1, TimeUnit::Second => 1,
TimeUnit::Minute => 60, TimeUnit::Minute => 60,
@ -91,8 +91,8 @@ impl TimeUnit {
} }
impl TimeAgo { impl TimeAgo {
fn secs(self) -> i64 { fn secs(self) -> u32 {
i64::from(self.n) * self.unit.secs() u32::from(self.n) * self.unit.secs()
} }
} }
@ -109,7 +109,7 @@ impl Mul<u8> for TimeAgo {
impl From<TimeAgo> for Duration { impl From<TimeAgo> for Duration {
fn from(ta: TimeAgo) -> Self { 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<u32>
tokens.peek()?; tokens.peek()?;
tokens.for_each(|ta| { tokens.for_each(|ta| {
secs += n * ta.secs() as u32; secs += n * ta.secs();
n = 1; n = 1;
}); });
} }

View file

@ -1695,7 +1695,7 @@ fn music_search_videos(rp: RustyPipe, unlocalized: bool) {
#[tokio::test] #[tokio::test]
async fn music_search_episode() { async fn music_search_episode() {
let rp = RustyPipe::builder().strict().build(); let rp = RustyPipe::builder().strict().build().unwrap();
let res = rp let res = rp
.query() .query()
.music_search_videos("Blond - Da muss man dabei gewesen sein: Das Hörspiel - Fall #1") .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) .lang(lang)
.visitor_data_opt(vdata) .visitor_data_opt(vdata)
.build() .build()
.unwrap()
} }
/// Get a flag signaling if the language is set to English /// 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 /// Get a new RustyPipe instance with pre-set visitor data
fn rp_visitor_data(vdata: &str) -> RustyPipe { 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 /// Assert equality within 10% margin