vstorage 0.6.0

Common API for various icalendar/vcard storages.
Documentation
// Copyright 2025 Hugo Osvaldo Barrera
//
// SPDX-License-Identifier: EUPL-1.2

use futures_util::future::BoxFuture;
use std::time::Duration;
use tokio::time::Interval;

use crate::{
    Result,
    watch::{Event, StorageMonitor},
};

use super::VdirStorage;

/// Monitor for a [`VdirStorage`] instance.
///
/// # Quirks
///
/// This implementation does not use any in-kernel API, and simply polls every `interval`,
/// returning an [`Event::General`], which indicates that the entire storage must be re-scanned.
///
/// [general]: [crate::watch::Event::General]
pub struct VdirMonitor {
    timer: Interval,
}

impl VdirMonitor {
    /// Create a new monitor for `storage`.
    ///
    /// # Errors
    ///
    /// This portable implementation is infallible.
    pub fn new(_: &VdirStorage, interval: Duration) -> Result<VdirMonitor> {
        let mut timer = tokio::time::interval(interval);
        timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        Ok(VdirMonitor { timer })
    }
}

impl StorageMonitor for VdirMonitor {
    fn next_event(&mut self) -> BoxFuture<'_, Event> {
        Box::pin(async {
            self.timer.tick().await;
            Event::General
        })
    }
}