use crate::Operation;
use std::time::Duration;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum Error {
#[error("{platform} runtime initialization failed: {message}")]
PlatformInitialization {
platform: &'static str,
message: String,
},
#[error(
"{operation:?} is not supported by the active runtime backend. {guidance}",
guidance = unsupported_guidance(*operation)
)]
Unsupported {
operation: Operation,
},
#[error("invalid {field}: {message}")]
InvalidInput {
field: &'static str,
message: String,
},
#[error("permission {permission} was not granted")]
PermissionDenied {
permission: String,
},
#[error("{operation} timed out after {timeout:?}")]
Timeout {
operation: &'static str,
timeout: Duration,
},
#[error("an online network path is required, but the current state is {state}")]
Offline {
state: &'static str,
},
#[error("stored value for {key:?} is corrupt: {message}")]
CorruptStorage {
key: String,
message: String,
},
#[error("store {store:?} requires migration from version {stored} to {current}")]
MigrationRequired {
store: String,
stored: u32,
current: u32,
},
#[error("{operation:?} failed: {message}")]
Backend {
operation: Operation,
message: String,
},
#[error("storage operation {action} failed for {path}: {message}")]
StorageIo {
action: &'static str,
path: String,
message: String,
},
}
impl Error {
pub fn platform_initialization(platform: &'static str, message: impl Into<String>) -> Self {
Self::PlatformInitialization {
platform,
message: message.into(),
}
}
pub const fn unsupported(operation: Operation) -> Self {
Self::Unsupported { operation }
}
pub fn backend(operation: Operation, message: impl Into<String>) -> Self {
Self::Backend {
operation,
message: message.into(),
}
}
pub fn invalid(field: &'static str, message: impl Into<String>) -> Self {
Self::InvalidInput {
field,
message: message.into(),
}
}
}
const fn unsupported_guidance(operation: Operation) -> &'static str {
match operation {
Operation::NetworkStatus | Operation::NetworkProbe => {
"Run `cargo ferry add network`, then rebuild the app."
}
Operation::Storage => "Run `cargo ferry add storage`, then rebuild the app.",
Operation::Haptics => "Run `cargo ferry add haptics`, then rebuild the app.",
Operation::ClipboardRead | Operation::ClipboardWrite => {
"Run `cargo ferry add clipboard`, then rebuild the app."
}
Operation::Share => "Run `cargo ferry add share`, then rebuild the app.",
Operation::NotificationPermissionStatus
| Operation::NotificationPermissionRequest
| Operation::NotificationSchedule
| Operation::NotificationShowNow
| Operation::NotificationCancel
| Operation::NotificationPending
| Operation::NotificationDelivered => {
"Run `cargo ferry add notifications`, then rebuild the app."
}
Operation::DeepLinkInitial => "Run `cargo ferry add deep-links`, then rebuild the app.",
Operation::WidgetUpdate => "Run `cargo ferry add widget`, then rebuild the app.",
Operation::LiveActivityStart
| Operation::LiveActivityUpdate
| Operation::LiveActivityEnd
| Operation::LiveActivityList => {
"Run `cargo ferry add live-activity`, then rebuild the app."
}
Operation::PermissionStatus | Operation::PermissionRequest => {
"Enable the related capability or `[permissions.<name>]` entry in `ferry.toml`; permission entries also require application-specific `purpose` text. Then rebuild the app."
}
Operation::OpenUrl
| Operation::OpenSettings
| Operation::AppInfo
| Operation::DeviceInfo
| Operation::Theme => {
"Ensure the target platform is listed in `ferry.toml` and rebuild its generated host; use `rustferry::testing::TestRuntime` for host tests."
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capability_operations_name_valid_add_commands() {
for (operation, capability) in [
(Operation::NetworkProbe, "network"),
(Operation::Storage, "storage"),
(Operation::Haptics, "haptics"),
(Operation::ClipboardWrite, "clipboard"),
(Operation::Share, "share"),
(Operation::NotificationSchedule, "notifications"),
(Operation::DeepLinkInitial, "deep-links"),
(Operation::WidgetUpdate, "widget"),
(Operation::LiveActivityStart, "live-activity"),
] {
let message = Error::unsupported(operation).to_string();
assert!(
message.contains(&format!("`cargo ferry add {capability}`")),
"missing remediation in {message:?}"
);
}
}
#[test]
fn permission_and_system_operations_point_to_configuration() {
let permission = Error::unsupported(Operation::PermissionRequest).to_string();
assert!(permission.contains("[permissions.<name>]"));
assert!(permission.contains("purpose"));
let system = Error::unsupported(Operation::OpenSettings).to_string();
assert!(system.contains("ferry.toml"));
assert!(system.contains("TestRuntime"));
}
}