mas_storage/
policy_data.rs

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