Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
kingwingfly committed Jun 6, 2024
1 parent 22cbfdc commit c2ff380
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 90 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

## [0.2.25] - 2024-06-07

- improve follow [this](https://users.rust-lang.org/t/i-just-wrote-the-hardest-code-in-my-life-any-improvements/112596)

## [0.2.24] - 2024-06-06

- fix: batch pull SIGINT hint missed
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace.package]
version = "0.2.24"
version = "0.2.25"
authors = ["Louis <[email protected]>"]
description = "Back up your favorite online resources with CLI."
license = "MIT"
Expand All @@ -16,8 +16,8 @@ documentation = ""
[workspace.dependencies]
fav_core = { path = "fav_core", version = "0.1.0" }
fav_derive = { path = "fav_derive", version = "0.0.1" }
fav_utils = { path = "fav_utils", version = "0.0.8" }
fav_cli = { path = "fav_cli", version = "0.2.24" }
fav_utils = { path = "fav_utils", version = "0.0.9" }
fav_cli = { path = "fav_cli", version = "0.2.25" }

[profile.release]
lto = "fat"
Expand Down
4 changes: 2 additions & 2 deletions fav_core/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ pub trait SaveLocal: Net {
&self,
res: &mut R,
urls: Vec<Url>,
f: F,
cancelled: F,
) -> impl Future<Output = FavCoreResult<()>>
where
R: Res,
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send;
}
6 changes: 3 additions & 3 deletions fav_core/src/test_utils/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl ResOps for App {

async fn fetch_res<F, Fut, Any>(&self, _: &mut Self::Res, _: F) -> FavCoreResult<()>
where
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send,
{
Expand All @@ -52,7 +52,7 @@ impl ResOps for App {

async fn pull_res<F, Fut, Any>(&self, _: &mut Self::Res, _: F) -> FavCoreResult<()>
where
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send,
{
Expand All @@ -65,7 +65,7 @@ impl SetOps for App {

async fn fetch_set<F, Fut, Any>(&self, _: &mut Self::Set, _: F) -> FavCoreResult<()>
where
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send,
{
Expand Down
2 changes: 1 addition & 1 deletion fav_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fav_utils"
version = "0.0.8"
version = "0.0.9"
authors.workspace = true
description = "Fav's utils crate; A collection of utilities and data structures for the fav project"
license.workspace = true
Expand Down
93 changes: 39 additions & 54 deletions fav_utils/src/bili/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use core::future::Future;
use fav_core::prelude::*;
use reqwest::header::CONTENT_LENGTH;
use std::io::{BufWriter, Write as _};
use tracing::error;

impl PathInfo for Bili {
#[cfg(test)]
Expand All @@ -29,7 +28,7 @@ impl SaveLocal for Bili {
) -> FavCoreResult<()>
where
R: Res,
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send,
{
Expand All @@ -54,59 +53,45 @@ impl SaveLocal for Bili {
let mut file_a = BufWriter::new(tempfile::NamedTempFile::new()?);
let mut finish_v = false;
let mut finish_a = false;
let mut failed_reason = None;
loop {
tokio::select! {
chunk = resp_v.chunk(), if !finish_v => {
match chunk {
Ok(Some(chunk)) => {
pb.inc(chunk.len() as u64);
file_v.write_all(&chunk).unwrap();
}
Ok(None) => finish_v = true,
Err(e) => {
error!("Failed to download video: {}", res.id());
failed_reason = Some(e);
}
}
},
chunk = resp_a.chunk(), if !finish_a => {
match chunk {
Ok(Some(chunk)) => {
pb.inc(chunk.len() as u64);
file_a.write_all(&chunk).unwrap();
}
Ok(None) => finish_a = true,
Err(e) => {
error!("Failed to download video: {}", res.id());
failed_reason = Some(e);
}
}
},
_ = async {}, if finish_v && finish_a => {
file_v.flush().unwrap();
file_a.flush().unwrap();
pb.finish();
merge(
title,
&id,
file_v.into_inner().unwrap().path().to_str().unwrap(),
file_a.into_inner().unwrap().path().to_str().unwrap(),
)
.await?;
res.on_status(StatusFlags::SAVED);
return Ok(())
},
_ = async {}, if failed_reason.is_some() => {
file_v.into_inner().unwrap().close()?;
file_a.into_inner().unwrap().close()?;
return Err(failed_reason.unwrap().into());
},
_ = f() => {
file_v.into_inner().unwrap().close()?;
file_a.into_inner().unwrap().close()?;
return Err(FavCoreError::Cancel)
tokio::select! {
res = async {
while let Some(chunk) = resp_v.chunk().await? {
pb.inc(chunk.len() as u64);
file_v.write_all(&chunk)?;
}
finish_v = true;
Ok(())
}, if !finish_v => {
res
},
res = async {
while let Some(chunk) = resp_a.chunk().await? {
pb.inc(chunk.len() as u64);
file_a.write_all(&chunk)?;
}
finish_a = true;
Ok(())
}, if !finish_a => {
res
},
_ = async {}, if finish_v && finish_a => {
file_v.flush().unwrap();
file_a.flush().unwrap();
pb.finish();
merge(
title,
&id,
file_v.into_inner().unwrap().path().to_str().unwrap(),
file_a.into_inner().unwrap().path().to_str().unwrap(),
)
.await?;
res.on_status(StatusFlags::SAVED);
Ok(())
},
_ = f() => {
file_v.into_inner().unwrap().close()?;
file_a.into_inner().unwrap().close()?;
Err(FavCoreError::Cancel)
}
}
}
Expand Down
49 changes: 25 additions & 24 deletions fav_utils/src/bili/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::StreamExt;
use reqwest::Response;
use std::collections::HashMap;
use tokio::time::{sleep, Duration};
use tracing::{error, info};
use tracing::info;
use url::Url;

const POLL_INTERVAL: u64 = 3;
Expand Down Expand Up @@ -65,9 +65,9 @@ impl SetsOps for Bili {
impl SetOps for Bili {
type Set = BiliSet;

async fn fetch_set<F, Fut, Any>(&self, set: &mut Self::Set, f: F) -> FavCoreResult<()>
async fn fetch_set<F, Fut, Any>(&self, set: &mut Self::Set, cancelled: F) -> FavCoreResult<()>
where
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send,
{
Expand All @@ -80,21 +80,22 @@ impl SetOps for Bili {
let params = vec![id.clone(), pn, "20".to_string()];
self.request_proto::<BiliSet>(ApiKind::FetchSet, params, "/data")
})
.buffer_unordered(10);
loop {
tokio::select! {
res = stream.next() => {
.buffer_unordered(8);
tokio::select! {
res = async {
while let Some(res) = stream.next().await {
match res {
Some(Ok(res)) => *set |= res.with_res_status_on(StatusFlags::FAV),
Some(Err(e)) => error!("{}", e),
None => break
Ok(res) => *set |= res.with_res_status_on(StatusFlags::FAV),
Err(e) => return Err(e),
}
}
_ = f() => return Err(FavCoreError::Cancel)
info!("Fetch set<{}> successfully.", id);
Ok(())
} => {
res
}
_ = cancelled() => Err(FavCoreError::Cancel)
}
info!("Fetch set<{}> successfully.", id);
Ok(())
}
}

Expand All @@ -103,30 +104,30 @@ impl ResOps for Bili {

async fn fetch_res<F, Fut, Any>(&self, resource: &mut Self::Res, f: F) -> FavCoreResult<()>
where
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send,
{
let params = vec![resource.bvid.clone()];
tokio::select! {
res = self.request_proto::<BiliRes>(ApiKind::FetchRes, params, "/data") => {
match res {
Ok(res) => *resource |= res,
Err(e) if matches!(e, FavCoreError::NetworkError(_)) => Err(e)?,
_ => resource.on_status(StatusFlags::EXPIRED),
}
resource.on_status(StatusFlags::FETCHED);
},
match res {
Ok(res) => *resource |= res,
Err(FavCoreError::NetworkError(e)) => Err(e)?,
_ => resource.on_status(StatusFlags::EXPIRED),
}
resource.on_status(StatusFlags::FETCHED);
Ok(())
},
_ = f() => {
return Err(FavCoreError::Cancel);
Err(FavCoreError::Cancel)
}
}
Ok(())
}

async fn pull_res<F, Fut, Any>(&self, resource: &mut Self::Res, f: F) -> FavCoreResult<()>
where
F: Fn() -> Fut + Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send,
{
Expand Down

0 comments on commit c2ff380

Please sign in to comment.