use actionqueue_core::ids::RunId;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct LeaseOwner(String);
impl LeaseOwner {
pub fn new(owner: impl Into<String>) -> Self {
let value = owner.into();
assert!(!value.is_empty(), "LeaseOwner must not be empty");
Self(value)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for LeaseOwner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for LeaseOwner {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&str> for LeaseOwner {
fn from(value: &str) -> Self {
Self::new(value.to_owned())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct LeaseExpiry {
expires_at: u64,
}
impl LeaseExpiry {
pub const fn at(expires_at: u64) -> Self {
Self { expires_at }
}
pub const fn expires_at(&self) -> u64 {
self.expires_at
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Lease {
run_id: RunId,
owner: LeaseOwner,
expiry: LeaseExpiry,
}
impl Lease {
pub fn new(run_id: RunId, owner: LeaseOwner, expiry: LeaseExpiry) -> Self {
Self { run_id, owner, expiry }
}
pub fn run_id(&self) -> RunId {
self.run_id
}
pub fn owner(&self) -> &LeaseOwner {
&self.owner
}
pub fn expiry(&self) -> LeaseExpiry {
self.expiry
}
}