From 3f326713ba440cf38661e7e1d6776a1cd3e5eeb0 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 9 Jul 2023 11:48:06 +0200 Subject: [PATCH] Fix clippy hints * https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if (-D clippy::collapsible-else-if) * https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow (-D clippy::needless-borrow) * https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref (-D clippy::into-iter-on-ref) * https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten (-D clippy::map-flatten) * https://rust-lang.github.io/rust-clippy/master/index.html#len_zero (-D clippy::len-zero) --- src/ilias.rs | 15 ++++----- src/ilias/course.rs | 4 +-- src/ilias/file.rs | 2 +- src/ilias/folder.rs | 2 +- src/ilias/video.rs | 74 +++++++++++++++++++++----------------------- src/ilias/weblink.rs | 2 +- src/iliasignore.rs | 2 +- 7 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/ilias.rs b/src/ilias.rs index 3c758c8..c249757 100644 --- a/src/ilias.rs +++ b/src/ilias.rs @@ -45,12 +45,9 @@ pub struct ILIAS { fn error_is_http2(error: &reqwest::Error) -> bool { error .source() // hyper::Error - .map(|x| x.source()) // h2::Error - .flatten() - .map(|x| x.downcast_ref::()) - .flatten() - .map(|x| x.reason()) - .flatten() + .and_then(|x| x.source()) // h2::Error + .and_then(|x| x.downcast_ref::()) + .and_then(|x| x.reason()) .map(|x| x == h2::Reason::NO_ERROR) .unwrap_or(false) } @@ -349,8 +346,8 @@ impl Object { | Presentation { name, .. } | ExerciseHandler { name, .. } | PluginDispatch { name, .. } - | Generic { name, .. } => &name, - Thread { url } => &url.thr_pk.as_ref().unwrap(), + | Generic { name, .. } => name, + Thread { url } => url.thr_pk.as_ref().unwrap(), Video { url } => &url.url, Dashboard { url } => &url.url, } @@ -371,7 +368,7 @@ impl Object { | ExerciseHandler { url, .. } | PluginDispatch { url, .. } | Video { url } - | Generic { url, .. } => &url, + | Generic { url, .. } => url, } } diff --git a/src/ilias/course.rs b/src/ilias/course.rs index 76c0f9f..e04f792 100644 --- a/src/ilias/course.rs +++ b/src/ilias/course.rs @@ -28,12 +28,12 @@ pub async fn download(path: PathBuf, ilias: Arc, url: &URL, name: &str) - return Ok(()); // ignore groups we are not in } warning!(name, "falling back to incomplete course content extractor!", e); - let (items, main_text, _) = ilias.get_course_content(&url).await?; + let (items, main_text, _) = ilias.get_course_content(url).await?; (items, main_text) }, } } else { - let (items, main_text, _) = ilias.get_course_content(&url).await?; + let (items, main_text, _) = ilias.get_course_content(url).await?; (items, main_text) }; if ilias.opt.save_ilias_pages { diff --git a/src/ilias/file.rs b/src/ilias/file.rs index 1df7b2a..a4e1690 100644 --- a/src/ilias/file.rs +++ b/src/ilias/file.rs @@ -17,6 +17,6 @@ pub async fn download(path: &Path, relative_path: &Path, ilias: Arc, url: } let data = ilias.download(&url.url).await?; log!(0, "Writing {}", relative_path.to_string_lossy()); - write_stream_to_file(&path, data.bytes_stream()).await?; + write_stream_to_file(path, data.bytes_stream()).await?; Ok(()) } diff --git a/src/ilias/folder.rs b/src/ilias/folder.rs index 898ecc5..56ef63b 100644 --- a/src/ilias/folder.rs +++ b/src/ilias/folder.rs @@ -17,7 +17,7 @@ static EXPAND_LINK: Lazy = Lazy::new(|| Regex::new("expand=\\d").unwrap() #[async_recursion] pub async fn download(path: &Path, ilias: Arc, url: &URL) -> Result<()> { - let content = ilias.get_course_content(&url).await?; + let content = ilias.get_course_content(url).await?; // expand all sessions for href in content.2 { diff --git a/src/ilias/video.rs b/src/ilias/video.rs index 2437ddc..27640bd 100644 --- a/src/ilias/video.rs +++ b/src/ilias/video.rs @@ -34,7 +34,7 @@ pub async fn download(path: &Path, relative_path: &Path, ilias: Arc, url: let json = &json_capture.next().context("xoct player json not found")?[1]; log!(2, "{}", json); let json = json.split(",\n").next().context("invalid xoct player json")?; - serde_json::from_str(&json.trim())? + serde_json::from_str(json.trim())? }; log!(2, "{}", json); let streams = json @@ -49,52 +49,50 @@ pub async fn download(path: &Path, relative_path: &Path, ilias: Arc, url: .as_str() .context("video src not string")?; download_to_path(&ilias, path, relative_path, url).await?; + } else if !ilias.opt.combine_videos { + fs::create_dir(path).await.context("failed to create video directory")?; + download_all(path, streams, ilias, relative_path).await?; } else { - if !ilias.opt.combine_videos { - fs::create_dir(path).await.context("failed to create video directory")?; - download_all(path, streams, ilias, relative_path).await?; - } else { - let dir = tempdir()?; - // construct ffmpeg command to combine all files - let mut arguments = vec![]; - for file in download_all(dir.path(), streams, ilias, relative_path).await? { - arguments.push("-i".to_owned()); - arguments.push(file.to_str().context("invalid UTF8")?.into()); - } - arguments.push("-c".into()); - arguments.push("copy".into()); - for i in 0..(arguments.len() / 2) - 1 { - arguments.push("-map".into()); - arguments.push(format!("{}", i)); - } - arguments.push(path.to_str().context("invalid UTF8 in path")?.into()); - let status = Command::new("ffmpeg") - .args(&arguments) - .stderr(Stdio::null()) - .stdout(Stdio::null()) - .spawn() - .context("failed to start ffmpeg")? - .wait() - .await - .context("failed to wait for ffmpeg")?; - if !status.success() { - error!(format!("ffmpeg failed to merge video files into {}", path.display())); - error!(format!("check this directory: {}", dir.into_path().display())); - error!(format!("ffmpeg command: {}", arguments.join(" "))); - } - }; + let dir = tempdir()?; + // construct ffmpeg command to combine all files + let mut arguments = vec![]; + for file in download_all(dir.path(), streams, ilias, relative_path).await? { + arguments.push("-i".to_owned()); + arguments.push(file.to_str().context("invalid UTF8")?.into()); + } + arguments.push("-c".into()); + arguments.push("copy".into()); + for i in 0..(arguments.len() / 2) - 1 { + arguments.push("-map".into()); + arguments.push(format!("{}", i)); + } + arguments.push(path.to_str().context("invalid UTF8 in path")?.into()); + let status = Command::new("ffmpeg") + .args(&arguments) + .stderr(Stdio::null()) + .stdout(Stdio::null()) + .spawn() + .context("failed to start ffmpeg")? + .wait() + .await + .context("failed to wait for ffmpeg")?; + if !status.success() { + error!(format!("ffmpeg failed to merge video files into {}", path.display())); + error!(format!("check this directory: {}", dir.into_path().display())); + error!(format!("ffmpeg command: {}", arguments.join(" "))); + } } Ok(()) } async fn download_all( path: &Path, - streams: &Vec, + streams: &[serde_json::Value], ilias: Arc, relative_path: &Path, ) -> Result> { let mut paths = Vec::new(); - for (i, stream) in streams.into_iter().enumerate() { + for (i, stream) in streams.iter().enumerate() { let url = stream .pointer("/sources/mp4/0/src") .context("video src not found")? @@ -126,9 +124,9 @@ async fn download_to_path(ilias: &ILIAS, path: &Path, relative_path: &Path, url: } } } else { - let resp = ilias.download(&url).await?; + let resp = ilias.download(url).await?; log!(0, "Writing {}", relative_path.to_string_lossy()); - write_stream_to_file(&path, resp.bytes_stream()).await?; + write_stream_to_file(path, resp.bytes_stream()).await?; } Ok(()) } diff --git a/src/ilias/weblink.rs b/src/ilias/weblink.rs index 72e7763..4ada810 100644 --- a/src/ilias/weblink.rs +++ b/src/ilias/weblink.rs @@ -27,7 +27,7 @@ pub async fn download(path: &Path, relative_path: &Path, ilias: Arc, url: if url.starts_with(ILIAS_URL) { // is a link list if fs::metadata(&path).await.is_err() { - create_dir(&path).await?; + create_dir(path).await?; log!(0, "Writing {}", relative_path.to_string_lossy()); } diff --git a/src/iliasignore.rs b/src/iliasignore.rs index 1f35516..13712c4 100644 --- a/src/iliasignore.rs +++ b/src/iliasignore.rs @@ -24,7 +24,7 @@ impl IliasIgnore { if let Some(err) = error { warning!(err); } - if ignore.len() > 0 { + if !ignore.is_empty() { ignores.push(IgnoreFile { ignore, prefix: prefix.iter().fold(OsString::new(), |mut acc, el| {