Skip to main content

quickgui_system/
lib.rs

1//! Bounded, cross-platform operating-system integrations independent of QuickGUI's renderer.
2
3mod app_environment;
4#[cfg(feature = "autostart")]
5mod autostart;
6mod power;
7mod preferences;
8mod process;
9#[cfg(feature = "protocol")]
10mod protocol;
11#[cfg(feature = "secure-storage")]
12mod secure_storage;
13#[cfg(feature = "updater")]
14mod update;
15
16pub use app_environment::{
17    AppInfo, AppPaths, MAX_APP_IDENTIFIER_BYTES, MAX_APP_NAME_BYTES, MAX_APP_VERSION_BYTES,
18    MAX_PREFERRED_LANGUAGES, MAX_SYSTEM_LOCALE_BYTES, MAX_SYSTEM_LOCALES_TOTAL_BYTES,
19    MAX_SYSTEM_TEXT_BYTES, OperatingSystem, OperatingSystemFamily, SystemBitness, SystemInfo,
20};
21#[cfg(feature = "autostart")]
22pub use autostart::{AutoStart, AutoStartMode, AutoStartOptions};
23pub use power::{
24    BatteryState, BatteryStatus, IdleState, MAX_IDLE_THRESHOLD, MAX_POWER_ASSERTION_REASON_BYTES,
25    PowerAssertion, PowerAssertionKind, PowerMonitor, PowerSource, PowerState, SessionState,
26    ThermalState,
27};
28pub use preferences::{
29    ColorScheme, PermissionKind, PermissionManager, PermissionStatus, SystemColor, SystemColorRole,
30    SystemPreferences,
31};
32pub use process::{
33    MAX_RELAUNCH_ARGUMENT_BYTES, MAX_RELAUNCH_ARGUMENTS, MAX_RELAUNCH_VALUE_BYTES, RelaunchOptions,
34    RelaunchRequest, RelaunchedProcess,
35};
36#[cfg(feature = "protocol")]
37pub use protocol::{ProtocolRegistration, ProtocolRegistrationOptions};
38#[cfg(feature = "secure-storage")]
39pub use secure_storage::SecureStorage;
40#[cfg(feature = "updater")]
41pub use update::{
42    AvailableUpdate, DEFAULT_MAX_EXPANDED_UPDATE_BYTES, DEFAULT_MAX_UPDATE_BYTES, InstalledUpdate,
43    MAX_UPDATE_ARCHIVE_ENTRIES, MAX_UPDATE_INSTALLER_ARGUMENT_BYTES,
44    MAX_UPDATE_INSTALLER_ARGUMENTS, MAX_UPDATE_MANIFEST_BYTES, MAX_UPDATE_SIGNATURE_BYTES,
45    UpdateCancellation, UpdateClient, UpdateInstallDisposition, UpdateInstallOptions,
46    UpdateProgress, WindowsUpdateInstallMode, default_update_target,
47};
48
49use std::sync::Arc;
50
51use thiserror::Error;
52
53/// An operating-system integration rejected or failed an operation.
54#[derive(Clone, Debug, Error, Eq, PartialEq)]
55pub enum SystemIntegrationError {
56    #[error("the operating-system integration operation was cancelled")]
57    Cancelled,
58    #[error("this integration is not supported on the current operating system")]
59    Unsupported,
60    #[error("invalid system integration input: {0}")]
61    InvalidInput(Arc<str>),
62    #[error("operating-system integration failed: {0}")]
63    Platform(Arc<str>),
64    #[error("network operation failed: {0}")]
65    Network(Arc<str>),
66    #[error("update metadata is invalid: {0}")]
67    InvalidUpdate(Arc<str>),
68    #[error("update signature verification failed: {0}")]
69    InvalidSignature(Arc<str>),
70}
71
72pub type Result<T> = std::result::Result<T, SystemIntegrationError>;
73
74fn invalid(message: impl Into<Arc<str>>) -> SystemIntegrationError {
75    SystemIntegrationError::InvalidInput(message.into())
76}
77
78fn platform(error: impl ToString) -> SystemIntegrationError {
79    SystemIntegrationError::Platform(Arc::from(error.to_string()))
80}
81
82#[cfg(any(
83    feature = "autostart",
84    feature = "protocol",
85    feature = "secure-storage"
86))]
87fn validate_text(value: &str, name: &str, maximum: usize) -> Result<()> {
88    if value.is_empty() || value.contains('\0') || value.len() > maximum {
89        return Err(invalid(format!(
90            "{name} must be nonempty, NUL-free, and at most {maximum} UTF-8 bytes"
91        )));
92    }
93    Ok(())
94}