Skip to main content

ahqstore_types/
lib.rs

1#![allow(
2  dead_code,
3  unused_imports,
4  non_local_definitions,
5  reason = "Conditional compilation"
6)]
7
8//! **You should use cli**
9//! ```sh
10//! cargo install ahqstore_cli_rs
11//! ```
12//! or visit app / api sub module
13//!
14//! This Module:
15//! - This module lists the standard commands & types that AHQ Store sends to AHQ Store Service
16//! - Defines schemas for the AHQ Store File Formats
17
18use serde::{Deserialize, Serialize};
19use serde_json::{from_str, to_string, to_string_pretty};
20use std::{borrow::Cow, fs::read, sync::Arc};
21use tokio::task::JoinHandle;
22
23pub type AppId = String;
24pub type Str = String;
25pub type AppData = (String, String);
26pub type RefId = u64;
27
28pub type Success = bool;
29
30pub mod app;
31pub use app::*;
32
33pub mod api;
34pub use api::*;
35
36pub mod data;
37pub use data::*;
38
39pub mod winget;
40
41#[cfg_attr(feature = "export", derive(specta::Type))]
42#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
43#[derive(Debug, Serialize)]
44pub struct StatusUpdateData {
45  #[cfg(not(feature = "export"))]
46  pub queue: Box<[QueuedAppData]>,
47  #[cfg(feature = "export")]
48  pub queue: Vec<QueuedAppData>,
49  #[serde(rename = "supportsUpdate")]
50  pub disable_update: bool,
51  #[serde(rename = "queueOverflow")]
52  pub overflow: bool,
53}
54
55#[cfg_attr(feature = "export", derive(specta::Type))]
56#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
57#[derive(Debug, Serialize, Clone)]
58pub enum AppActionIntent {
59  Install,
60  Uninstall,
61  Update,
62}
63
64#[cfg_attr(feature = "export", derive(specta::Type))]
65#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
66#[derive(Debug, Serialize, Clone)]
67pub struct QueuedAppData {
68  #[cfg(not(feature = "export"))]
69  pub id: Arc<str>,
70  #[cfg(feature = "export")]
71  pub id: String,
72  #[cfg_attr(feature = "export", specta(type = u32))]
73  pub transaction: u64,
74  pub status: AppUpdateInstallStatus,
75  pub intent: AppActionIntent,
76}
77
78impl QueuedAppData {
79  pub fn from(data: &QueuedApp) -> Self {
80    Self {
81      id: data.id.clone(),
82      transaction: data.transaction,
83      intent: data.intent.clone(),
84      status: data.status.clone(),
85    }
86  }
87}
88
89#[derive(Debug, Serialize)]
90pub struct QueuedAppUpdate {
91  pub transaction: u64,
92  pub status: AppUpdateInstallStatus,
93}
94
95#[derive(Debug, Serialize)]
96pub struct QueuedApp {
97  #[cfg(not(feature = "export"))]
98  pub id: Arc<str>,
99  #[cfg(feature = "export")]
100  pub id: String,
101  pub transaction: u64,
102  pub status: AppUpdateInstallStatus,
103  pub intent: AppActionIntent,
104  #[serde(skip)]
105  pub task: Option<JoinHandle<()>>,
106}
107
108#[cfg_attr(feature = "export", derive(specta::Type))]
109#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
110#[derive(Debug, Serialize, Clone)]
111#[serde(tag = "status")]
112pub enum AppUpdateInstallStatus {
113  /// { "status": "Pending" }
114  Pending,
115  /// { "status": "PendingUserAction" }
116  PendingUserAction,
117  /// { "status": "Cancelled" }
118  Cancelled {
119    // Shows for 5seconds
120    #[serde(skip)]
121    time: u64,
122  },
123  /// { "status": "Downloading", "progress": 100.0 }
124  Downloading { progress: f64 },
125  /// { "status": "AVSCanning" }
126  AVScanning,
127  /// { "status": "PendingInstall" }
128  PendingInstall,
129  /// { "status": "Installing", "progress": null }
130  ///
131  /// OR
132  ///
133  /// { "status": "Installing", "progress": 30.0 }
134  Installing { progress: Option<f64> },
135  /// { "status": "MoreDwnlNeeded" }
136  MoreDwnlNeeded {
137    // Total progress
138    progress: f64,
139    current: usize,
140    items: usize,
141  },
142  /// { "status": "CopyingFiles", "percentage": 67, "total": 100 }
143  CopyingFiles { percentage: f64, total: usize },
144  /// { "status": "Finalizing" }
145  Finalizing,
146  /// { "status": "Updating" }
147  Updating,
148  /// { "status": "Uninstalling" }
149  Uninstalling,
150  /// { "status": "Successful" }
151  Successful {
152    // This is a time delta used by us to auto prune >2s entries
153    #[serde(skip)]
154    time: u64,
155  },
156  /// { "status": "Error", "err": "ERROR DESC" }
157  Error {
158    err: Cow<'static, str>,
159    // >10s time delta
160    #[serde(skip)]
161    time: u64,
162  },
163}
164
165#[cfg(test)]
166mod tests {
167  #[test]
168  #[cfg(feature = "export")]
169  fn export() {
170    // Since the `u64`s represent time deltas, and counters. We can safely treat as JS Number.
171    let export =
172      specta::ts::ExportConfiguration::new().bigint(specta::ts::BigIntExportBehavior::Number);
173
174    _ = specta::export::ts_with_cfg("./pkg/ahqstore.types.d.ts", &export).unwrap();
175    _ = specta::export::ts_with_cfg("./types/ahqstore.types.d.ts", &export).unwrap();
176  }
177}