use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ChildId {
pub value: String,
}
impl ChildId {
pub fn new(value: impl Into<String>) -> Self {
Self {
value: value.into(),
}
}
}
impl Display for ChildId {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.value)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SupervisorId {
pub value: Uuid,
}
impl SupervisorId {
pub fn new() -> Self {
Self {
value: Uuid::new_v4(),
}
}
}
impl Default for SupervisorId {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SupervisorPath {
pub segments: Vec<String>,
}
impl SupervisorPath {
pub fn root() -> Self {
Self {
segments: Vec::new(),
}
}
pub fn join(&self, segment: impl Into<String>) -> Self {
let mut segments = self.segments.clone();
segments.push(segment.into());
Self { segments }
}
pub fn parent(&self) -> Option<Self> {
let mut segments = self.segments.clone();
segments.pop()?;
Some(Self { segments })
}
}
impl Display for SupervisorPath {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
if self.segments.is_empty() {
formatter.write_str("/")
} else {
write!(formatter, "/{}", self.segments.join("/"))
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Attempt {
pub value: u64,
}
impl Attempt {
pub fn first() -> Self {
Self { value: 1 }
}
pub fn next(self) -> Self {
Self {
value: self.value.saturating_add(1),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Generation {
pub value: u64,
}
impl Generation {
pub fn initial() -> Self {
Self { value: 0 }
}
pub fn next(self) -> Self {
Self {
value: self.value.saturating_add(1),
}
}
}