use std::sync::Arc;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::schema::{AppMeta, WorkloadV2};
pub type RawToken = String;
pub type NodeId = Uuid;
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InstanceId(Uuid);
impl InstanceId {
pub fn new_random() -> Self {
Self(Uuid::new_v4())
}
pub fn nil() -> Self {
Self(Uuid::nil())
}
pub fn to_uuid(&self) -> Uuid {
self.0
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
}
impl std::fmt::Display for InstanceId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for InstanceId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<uuid::Uuid> for InstanceId {
fn from(uuid: uuid::Uuid) -> Self {
Self(uuid)
}
}
impl From<InstanceId> for uuid::Uuid {
fn from(id: InstanceId) -> Self {
id.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkloadMeta {
pub workload: WorkloadV2,
pub app_meta: Option<AppMeta>,
}
impl WorkloadMeta {
pub fn agent(&self) -> Option<&str> {
None
}
}
#[derive(Debug, Clone)]
pub struct InstanceMeta {
pub id: InstanceId,
pub workload: Arc<WorkloadMeta>,
}
impl InstanceMeta {
pub fn new(workload: Arc<WorkloadMeta>) -> Self {
Self {
id: InstanceId::new_random(),
workload,
}
}
}