use super::StoreError;
#[derive(Clone, Debug)]
pub struct AttachmentIntent {
pub attachment_id: crate::AttachmentId,
pub session_id: String,
pub canonical_uri: String,
pub intent_at_epoch_ms: u64,
}
#[derive(Clone, Debug)]
pub struct AttachmentManifestEntry {
pub attachment_id: crate::AttachmentId,
pub session_id: String,
pub canonical_uri: String,
pub intent_at_epoch_ms: u64,
pub committed_at_epoch_ms: Option<u64>,
}
pub trait AttachmentManifest: Send + Sync {
fn record_intent(&self, intent: AttachmentIntent) -> Result<(), StoreError>;
fn commit_refs(
&self,
session_id: &str,
attachment_ids: &[crate::AttachmentId],
) -> Result<(), StoreError>;
fn list_uncommitted(
&self,
older_than_epoch_ms: u64,
) -> Result<Vec<AttachmentManifestEntry>, StoreError>;
fn forget_aged_uncommitted_intents(
&self,
intent_grace_cutoff_epoch_ms: u64,
) -> Result<(), StoreError> {
for aged in self.list_uncommitted(intent_grace_cutoff_epoch_ms)? {
self.forget(&aged.session_id, &aged.attachment_id)?;
}
Ok(())
}
fn has_live_ref_for_id(
&self,
attachment_id: &crate::AttachmentId,
intent_grace_cutoff_epoch_ms: u64,
) -> Result<bool, StoreError> {
let _ = intent_grace_cutoff_epoch_ms;
Ok(self
.list_all_refs()?
.iter()
.any(|ref_id| ref_id == attachment_id))
}
fn forget(
&self,
session_id: &str,
attachment_id: &crate::AttachmentId,
) -> Result<(), StoreError>;
fn holds_ref(
&self,
session_id: &str,
attachment_id: &crate::AttachmentId,
) -> Result<bool, StoreError>;
fn list_all_refs(&self) -> Result<Vec<crate::AttachmentId>, StoreError>;
}
#[macro_export]
macro_rules! impl_noop_attachment_manifest {
($ty:ty) => {
impl $crate::AttachmentManifest for $ty {
fn record_intent(
&self,
_intent: $crate::AttachmentIntent,
) -> ::std::result::Result<(), $crate::StoreError> {
Ok(())
}
fn commit_refs(
&self,
_session_id: &str,
_attachment_ids: &[$crate::AttachmentId],
) -> ::std::result::Result<(), $crate::StoreError> {
Ok(())
}
fn list_uncommitted(
&self,
_older_than_epoch_ms: u64,
) -> ::std::result::Result<Vec<$crate::AttachmentManifestEntry>, $crate::StoreError>
{
Ok(Vec::new())
}
fn forget(
&self,
_session_id: &str,
_attachment_id: &$crate::AttachmentId,
) -> ::std::result::Result<(), $crate::StoreError> {
Ok(())
}
fn holds_ref(
&self,
_session_id: &str,
_attachment_id: &$crate::AttachmentId,
) -> ::std::result::Result<bool, $crate::StoreError> {
Ok(true)
}
fn list_all_refs(
&self,
) -> ::std::result::Result<Vec<$crate::AttachmentId>, $crate::StoreError> {
Ok(Vec::new())
}
}
};
}