use facet::Facet;
use crate::{BacktraceId, EntityId, Json, PTime, next_entity_id};
#[derive(Facet)]
pub struct Entity {
pub id: EntityId,
pub birth: PTime,
#[facet(skip_unless_truthy)]
pub removed_at: Option<PTime>,
pub backtrace: BacktraceId,
pub name: String,
pub body: EntityBody,
}
impl Entity {
pub fn new(backtrace: BacktraceId, name: impl Into<String>, body: EntityBody) -> Entity {
Entity {
id: next_entity_id(),
birth: PTime::now(),
removed_at: None,
backtrace,
name: name.into(),
body,
}
}
}
crate::define_entity_body! {
pub enum EntityBody {
Future(FutureEntity),
Lock(LockEntity),
MpscTx(MpscTxEntity),
MpscRx(MpscRxEntity),
BroadcastTx(BroadcastTxEntity),
BroadcastRx(BroadcastRxEntity),
WatchTx(WatchTxEntity),
WatchRx(WatchRxEntity),
OneshotTx(OneshotTxEntity),
OneshotRx(OneshotRxEntity),
Semaphore(SemaphoreEntity),
Notify(NotifyEntity),
OnceCell(OnceCellEntity),
Command(CommandEntity),
FileOp(FileOpEntity),
NetConnect(NetConnectEntity),
NetAccept(NetAcceptEntity),
NetRead(NetReadEntity),
NetWrite(NetWriteEntity),
Request(RequestEntity),
Response(ResponseEntity),
Custom(CustomEntity),
Aether(AetherEntity),
}
}
#[derive(Facet, Default)]
pub struct FutureEntity {
#[facet(skip_unless_truthy)]
pub skip_entry_frames: Option<u8>,
}
#[derive(Facet)]
pub struct LockEntity {
pub kind: LockKind,
}
#[derive(Facet)]
#[repr(u8)]
#[facet(rename_all = "snake_case")]
pub enum LockKind {
Mutex,
RwLock,
Other,
}
#[derive(Facet)]
pub struct MpscTxEntity {
pub queue_len: u32,
pub capacity: Option<u32>,
}
#[derive(Facet, Clone, Copy, Debug, PartialEq, Eq)]
pub struct MpscRxEntity {}
#[derive(Facet)]
pub struct BroadcastTxEntity {
pub capacity: u32,
}
#[derive(Facet)]
pub struct BroadcastRxEntity {
pub lag: u32,
}
#[derive(Facet)]
pub struct WatchTxEntity {
pub last_update_at: Option<PTime>,
}
#[derive(Facet)]
pub struct WatchRxEntity {}
#[derive(Facet)]
pub struct OneshotTxEntity {
pub sent: bool,
}
#[derive(Facet, Clone, Copy, Debug, PartialEq, Eq)]
pub struct OneshotRxEntity {}
#[derive(Facet)]
pub struct SemaphoreEntity {
pub max_permits: u32,
pub handed_out_permits: u32,
}
#[derive(Facet)]
pub struct NotifyEntity {
pub waiter_count: u32,
}
#[derive(Facet)]
pub struct OnceCellEntity {
pub waiter_count: u32,
pub state: OnceCellState,
}
#[derive(Facet, Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
#[facet(rename_all = "snake_case")]
pub enum OnceCellState {
Empty,
Initializing,
Initialized,
}
#[derive(Facet)]
pub struct CommandEntity {
pub program: String,
pub args: Vec<String>,
pub env: Vec<String>,
}
#[derive(Facet)]
pub struct FileOpEntity {
pub op: FileOpKind,
pub path: String,
}
#[derive(Facet)]
#[repr(u8)]
#[facet(rename_all = "snake_case")]
pub enum FileOpKind {
Open,
Read,
Write,
Sync,
Metadata,
Remove,
Rename,
Other,
}
#[derive(Facet)]
pub struct NetConnectEntity {
pub addr: String,
}
#[derive(Facet)]
pub struct NetAcceptEntity {
pub addr: String,
}
#[derive(Facet)]
pub struct NetReadEntity {
pub addr: String,
}
#[derive(Facet)]
pub struct NetWriteEntity {
pub addr: String,
}
#[derive(Facet)]
pub struct RequestEntity {
pub service_name: String,
pub method_name: String,
pub args_json: Json,
}
#[derive(Facet)]
pub struct ResponseEntity {
pub service_name: String,
pub method_name: String,
pub status: ResponseStatus,
}
#[derive(Facet, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
#[facet(rename_all = "snake_case")]
pub enum ResponseStatus {
Pending,
Ok(Json),
Error(ResponseError),
Cancelled,
}
#[derive(Facet, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
#[facet(rename_all = "snake_case")]
pub enum ResponseError {
Internal(String),
UserJson(Json),
}
#[derive(Facet)]
pub struct CustomEntity {
pub kind: String,
pub display_name: String,
pub category: String,
pub icon: String,
pub attrs: Json,
}
#[derive(Facet)]
pub struct AetherEntity {
pub task_id: String,
}