churn/crates/churn-server/src/lease.rs

46 lines
954 B
Rust
Raw Normal View History

use std::sync::Arc;
use axum::async_trait;
use tokio::sync::Mutex;
#[derive(Clone)]
pub struct LeaseService(Arc<dyn LeaseServiceTrait + Send + Sync + 'static>);
impl std::ops::Deref for LeaseService {
type Target = Arc<dyn LeaseServiceTrait + Send + Sync + 'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Default for LeaseService {
fn default() -> Self {
Self(Arc::new(DefaultLeaseService::default()))
}
}
#[derive(Default)]
struct DefaultLeaseService {
leases: Arc<Mutex<Vec<String>>>,
}
#[async_trait]
pub trait LeaseServiceTrait {
async fn create_lease(&self) -> anyhow::Result<String>;
}
#[async_trait]
impl LeaseServiceTrait for DefaultLeaseService {
async fn create_lease(&self) -> anyhow::Result<String> {
let mut leases = self.leases.lock().await;
let lease = uuid::Uuid::new_v4().to_string();
leases.push(lease.clone());
Ok(lease)
}
}