mas_storage/policy_data.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
// Copyright 2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//! Repositories to interact with the policy data saved in the storage backend.
use async_trait::async_trait;
use mas_data_model::PolicyData;
use rand_core::RngCore;
use crate::{Clock, repository_impl};
/// A [`PolicyDataRepository`] helps interacting with the policy data saved in
/// the storage backend.
#[async_trait]
pub trait PolicyDataRepository: Send + Sync {
/// The error type returned by the repository
type Error;
/// Get the latest policy data
///
/// Returns the latest policy data, or `None` if no policy data is
/// available.
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails
async fn get(&mut self) -> Result<Option<PolicyData>, Self::Error>;
/// Set the latest policy data
///
/// Returns the newly created policy data.
///
/// # Parameters
///
/// * `rng`: The random number generator to use
/// * `clock`: The clock used to generate the timestamps
/// * `data`: The policy data to set
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails
async fn set(
&mut self,
rng: &mut (dyn RngCore + Send),
clock: &dyn Clock,
data: serde_json::Value,
) -> Result<PolicyData, Self::Error>;
/// Prune old policy data
///
/// Returns the number of entries pruned.
///
/// # Parameters
///
/// * `keep`: the number of old entries to keep
///
/// # Errors
///
/// Returns [`Self::Error`] if the underlying repository fails
async fn prune(&mut self, keep: usize) -> Result<usize, Self::Error>;
}
repository_impl!(PolicyDataRepository:
async fn get(&mut self) -> Result<Option<PolicyData>, Self::Error>;
async fn set(
&mut self,
rng: &mut (dyn RngCore + Send),
clock: &dyn Clock,
data: serde_json::Value,
) -> Result<PolicyData, Self::Error>;
async fn prune(&mut self, keep: usize) -> Result<usize, Self::Error>;
);