nifty_runtime/download.rs
1//! What a `download(…)` call can bind, shared by the checker, which reads the call, and
2//! everything that runs it.
3
4coded_enum! {
5 /// A piece of state a download reports into. Compiled apps pass it to the runtime by
6 /// its code.
7 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
8 pub enum DownloadField {
9 /// The percentage done, `0` to `100`.
10 Progress,
11 /// Where the download stands, in words.
12 Status,
13 /// The bytes saved so far.
14 Received,
15 /// The file's size in bytes, once the server says; `0` until then.
16 Total,
17 /// The bytes received per second, over the last moments.
18 Speed,
19 /// The download's id, which code passes to `cancel_download` and the like, and
20 /// which its other bindings follow when their list changes.
21 Id,
22 }
23}
24
25impl DownloadField {
26 /// The setting that binds it, such as `progress`.
27 #[must_use]
28 pub const fn name(self) -> &'static str {
29 match self {
30 Self::Progress => "progress",
31 Self::Status => "status",
32 Self::Received => "received",
33 Self::Total => "total",
34 Self::Speed => "speed",
35 Self::Id => "id",
36 }
37 }
38
39 /// Whether every call must bind it.
40 #[must_use]
41 pub const fn is_required(self) -> bool {
42 matches!(self, Self::Progress | Self::Status)
43 }
44
45 /// Whether it holds text rather than a whole number.
46 #[must_use]
47 pub const fn is_text(self) -> bool {
48 matches!(self, Self::Status | Self::Id)
49 }
50}
51
52/// What code asks of a running download, by its id. `repr(u32)` because compiled apps
53/// pass it to the runtime, which every platform's C calling convention passes without
54/// widening.
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56#[repr(u32)]
57pub enum DownloadCommand {
58 /// Stops it for good, removing what it saved so far.
59 Cancel,
60 /// Stops it for now, keeping what it saved so far.
61 Pause,
62 /// Carries on with a paused download from where it stopped, when the server can.
63 Resume,
64}
65
66impl DownloadCommand {
67 /// The built-in function that asks for it, such as `cancel_download`.
68 #[must_use]
69 pub const fn function(self) -> &'static str {
70 match self {
71 Self::Cancel => "cancel_download",
72 Self::Pause => "pause_download",
73 Self::Resume => "resume_download",
74 }
75 }
76}