Skip to main content

mas_storage/
policy_data.rs

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