Skip to main content

zaun_community_protocol/command/
audit.rs

1//! `audit.list` (specification §8.8).
2
3use serde::{Deserialize, Serialize};
4
5use super::object::{AUDIT_ACTIONS, AuditEntry};
6use super::{BodyError, Validate, check};
7use crate::ids::AuditId;
8use crate::limits::PAGE_SMALL;
9
10/// `audit.list` (§8.8).
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct List {
13    /// Only entries older than this one.
14    #[serde(default, skip_serializing_if = "Option::is_none")]
15    pub before: Option<AuditId>,
16    /// 1–100.
17    pub limit: u32,
18    /// Only this action; one of [`AUDIT_ACTIONS`].
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub action: Option<String>,
21}
22
23/// `audit.list` response.
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25pub struct Page {
26    /// Newest first.
27    pub entries: Vec<AuditEntry>,
28    /// Older entries exist.
29    pub has_more: bool,
30}
31
32impl Validate for List {
33    fn validate(&self) -> Result<(), BodyError> {
34        check::page(self.limit, PAGE_SMALL)?;
35        match &self.action {
36            Some(action) if !AUDIT_ACTIONS.contains(&action.as_str()) => {
37                Err(BodyError::bad("action", "unknown audit action"))
38            }
39            _ => Ok(()),
40        }
41    }
42}