Skip to main content

functora_core/
progress.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6pub enum Stage {
7    #[default]
8    Attach,
9    Zip,
10    Encrypt,
11    Decrypt,
12    Unzip,
13    Download,
14    Preview,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct Job<S> {
19    pub stage: S,
20    pub done: u64,
21    pub total: u64,
22    pub name: Option<String>,
23}
24
25impl<S> Job<S> {
26    #[must_use]
27    pub fn percent(&self) -> u8 {
28        (u128::from(self.done) * 100 / u128::from(self.total.max(1))).min(100) as u8
29    }
30}
31
32pub async fn yield_to_paint() {
33    #[cfg(target_arch = "wasm32")]
34    {
35        gloo_timers::future::TimeoutFuture::new(1).await;
36    }
37    #[cfg(not(target_arch = "wasm32"))]
38    {
39        YieldOnce(false).await;
40    }
41}
42
43pub struct YieldOnce(bool);
44
45impl YieldOnce {
46    #[must_use]
47    pub const fn new(ready: bool) -> Self {
48        Self(ready)
49    }
50}
51
52impl Future for YieldOnce {
53    type Output = ();
54
55    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
56        if self.0 {
57            Poll::Ready(())
58        } else {
59            self.0 = true;
60            cx.waker().wake_by_ref();
61            Poll::Pending
62        }
63    }
64}