use lgwks_std::json::serde::{Deserialize, Serialize};
mod vocabulary;
pub mod workspace_layout;
mod workspace_vocabulary;
pub use vocabulary::{
ActorType, Capability, CompensationStrategy, EgressDestination, LocalActionStatus,
OfflineBehavior, SyncStatus, TrustBin,
};
pub use workspace_vocabulary::{
ChromePlacement, WidgetSurface, WorkspaceSlot, WorkspaceSurfaceRole, WorkspaceTheme,
WorkspaceToolIcon,
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(crate = "lgwks_std::json::serde")]
#[non_exhaustive]
pub struct SessionInvariant {
work_object_id: String,
session_id: String,
policy_bundle_id: String,
tenant_id: String,
user_sub: String,
}
#[derive(Debug, Clone, PartialEq, Eq, lgwks_macros::Error)]
#[non_exhaustive]
pub enum SessionInvariantError {
#[error("work_object_id is not a UUID")]
InvalidWorkObjectId,
#[error("session_id is not a UUID")]
InvalidSessionId,
#[error("policy_bundle_id is not a UUID")]
InvalidPolicyBundleId,
#[error("tenant_id is empty or over 512 bytes")]
InvalidTenantId,
#[error("user_sub is not a UUID")]
InvalidUserSub,
}
const INVARIANT_FIELD_BYTES: usize = 512;
fn require_uuid(value: &str, error: SessionInvariantError) -> Result<(), SessionInvariantError> {
(value.len() <= INVARIANT_FIELD_BYTES && lgwks_std::id::Uuid::parse(value).is_ok())
.then_some(())
.ok_or(error)
}
impl SessionInvariant {
pub fn new(
work_object_id: String,
session_id: String,
policy_bundle_id: String,
tenant_id: String,
user_sub: String,
) -> Result<Self, SessionInvariantError> {
let invariant = Self {
work_object_id,
session_id,
policy_bundle_id,
tenant_id,
user_sub,
};
invariant.check_shape()?;
Ok(invariant)
}
pub(crate) fn check_shape(&self) -> Result<(), SessionInvariantError> {
require_uuid(
&self.work_object_id,
SessionInvariantError::InvalidWorkObjectId,
)?;
require_uuid(&self.session_id, SessionInvariantError::InvalidSessionId)?;
require_uuid(
&self.policy_bundle_id,
SessionInvariantError::InvalidPolicyBundleId,
)?;
(!self.tenant_id.is_empty() && self.tenant_id.len() <= INVARIANT_FIELD_BYTES)
.then_some(())
.ok_or(SessionInvariantError::InvalidTenantId)?;
require_uuid(&self.user_sub, SessionInvariantError::InvalidUserSub)
}
#[must_use]
pub fn work_object_id(&self) -> &str {
&self.work_object_id
}
#[must_use]
pub fn session_id(&self) -> &str {
&self.session_id
}
#[must_use]
pub fn policy_bundle_id(&self) -> &str {
&self.policy_bundle_id
}
#[must_use]
pub fn tenant_id(&self) -> &str {
&self.tenant_id
}
#[must_use]
pub fn user_sub(&self) -> &str {
&self.user_sub
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(crate = "lgwks_std::json::serde")]
#[non_exhaustive]
pub enum NodeKind {
Vault,
DataEngine,
App,
VisionLayer,
Relay,
}
impl NodeKind {
#[must_use]
pub const fn max_action_tier(self) -> u8 {
match self {
Self::Vault => 3,
Self::DataEngine => 2,
Self::App | Self::VisionLayer => 1,
Self::Relay => 0,
}
}
#[must_use]
pub const fn data_exposure_level(self) -> u8 {
match self {
Self::Vault => 4,
Self::DataEngine => 3,
Self::App => 2,
Self::VisionLayer => 1,
Self::Relay => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::NodeKind;
fn round_trip(kind: NodeKind, wire: &str) -> Result<(), String> {
let encoded = lgwks_std::json::to_string(&kind)
.map_err(|error| format!("encode {kind:?}: {error}"))?;
if encoded != wire {
return Err(format!(
"wire form drifted for {kind:?}: got {encoded}, want {wire}"
));
}
let back: NodeKind =
lgwks_std::json::from_str(wire).map_err(|error| format!("decode {wire}: {error}"))?;
if back != kind {
return Err(format!("round-trip mismatch for {kind:?}"));
}
Ok(())
}
#[test]
fn node_kind_wire_form_is_pinned() -> Result<(), String> {
for (kind, wire) in [
(NodeKind::Vault, "\"Vault\""),
(NodeKind::DataEngine, "\"DataEngine\""),
(NodeKind::App, "\"App\""),
(NodeKind::VisionLayer, "\"VisionLayer\""),
(NodeKind::Relay, "\"Relay\""),
] {
round_trip(kind, wire)?;
}
if lgwks_std::json::from_str::<NodeKind>("\"Quantum\"").is_ok() {
return Err(String::from(
"unknown kind from a future peer must fail closed",
));
}
Ok(())
}
}