use alloc::string::String;
use serde::{Deserialize, Serialize};
use crate::db::Scope;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum Fallback {
#[default]
Spa,
None,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AssetMount {
pub scope: Option<Scope>,
pub index: Option<String>,
pub fallback: Fallback,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MountRequest {
pub mount: String,
pub api: Option<String>,
pub ws: Option<String>,
pub assets: Option<AssetMount>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct UnmountRequest {
pub mount: String,
}
pub const GATEWAY_ERR_INVALID_MOUNT: core::ffi::c_int = -30;
pub const GATEWAY_ERR_MOUNT_TAKEN: core::ffi::c_int = -31;
pub const GATEWAY_ERR_WRITE_FAILED: core::ffi::c_int = -32;
pub const GATEWAY_ERR_NOT_FOUND: core::ffi::c_int = -33;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GatewayError {
InvalidMount,
MountTaken,
WriteFailed,
NotFound,
}
impl TryFrom<core::ffi::c_int> for GatewayError {
type Error = core::ffi::c_int;
fn try_from(code: core::ffi::c_int) -> Result<Self, Self::Error> {
match code {
GATEWAY_ERR_INVALID_MOUNT => Ok(Self::InvalidMount),
GATEWAY_ERR_MOUNT_TAKEN => Ok(Self::MountTaken),
GATEWAY_ERR_WRITE_FAILED => Ok(Self::WriteFailed),
GATEWAY_ERR_NOT_FOUND => Ok(Self::NotFound),
_ => Err(code),
}
}
}
impl From<GatewayError> for &'static str {
fn from(err: GatewayError) -> Self {
match err {
GatewayError::InvalidMount => "invalid mount path",
GatewayError::MountTaken => "mount already owned by another cell",
GatewayError::WriteFailed => "failed to write gateway route",
GatewayError::NotFound => "mount not found",
}
}
}
pub const NAMESPACE_GATEWAY: &str = "gw";
pub const ASSETS_DB: &str = "gateway-assets";
#[must_use]
pub fn asset_scope(sri: &str) -> Scope {
Scope::public_owned(
String::from(NAMESPACE_GATEWAY),
Some(String::from(ASSETS_DB)),
Some(String::from(sri)),
)
}