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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use super::{AuditLogChange, AuditLogEventType, AuditLogOptionalEntryInfo};
use crate::id::{
    marker::{AuditLogEntryMarker, GenericMarker, UserMarker},
    Id,
};
use serde::{Deserialize, Serialize};

/// Entry in an [`AuditLog`] possibly containing a number of detailed changes.
///
/// [`AuditLog`]: super::AuditLog
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct AuditLogEntry {
    /// Type of event to cause the entry.
    pub action_type: AuditLogEventType,
    /// List of changes included in the entry.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub changes: Vec<AuditLogChange>,
    /// ID of the entire entry.
    pub id: Id<AuditLogEntryMarker>,
    /// Optional information about the entry.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<AuditLogOptionalEntryInfo>,
    /// Optional application- or user-attached reason for the action that caused
    /// the entry.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    /// ID of the target entity.
    pub target_id: Option<Id<GenericMarker>>,
    /// ID of the [user] that performed the action.
    ///
    /// [user]: crate::user::User
    pub user_id: Option<Id<UserMarker>>,
}

#[cfg(test)]
mod tests {
    use super::{
        super::{AuditLogChange, AuditLogEventType},
        AuditLogEntry,
    };
    use crate::{id::Id, test::image_hash};
    use serde::{Deserialize, Serialize};
    use serde_test::Token;
    use static_assertions::{assert_fields, assert_impl_all};
    use std::{fmt::Debug, hash::Hash};

    assert_fields!(
        AuditLogEntry: action_type,
        changes,
        id,
        options,
        reason,
        target_id,
        user_id
    );
    assert_impl_all!(
        AuditLogEntry: Clone,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Send,
        Serialize,
        Sync
    );

    /// Test the deserialization and serialization of an audit log entry.
    #[test]
    fn serde() {
        let value = AuditLogEntry {
            action_type: AuditLogEventType::GuildUpdate,
            changes: Vec::from([AuditLogChange::IconHash {
                new: None,
                old: Some(image_hash::ICON),
            }]),
            id: Id::new(3),
            options: None,
            reason: Some("some reason".to_owned()),
            target_id: Some(Id::new(2)),
            user_id: Some(Id::new(1)),
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "AuditLogEntry",
                    len: 6,
                },
                Token::Str("action_type"),
                Token::U16(AuditLogEventType::GuildUpdate.into()),
                Token::Str("changes"),
                Token::Seq { len: Some(1) },
                Token::Struct {
                    name: "AuditLogChange",
                    len: 2,
                },
                Token::Str("key"),
                Token::Str("icon_hash"),
                Token::Str("old_value"),
                Token::Some,
                Token::Str(image_hash::ICON_INPUT),
                Token::StructEnd,
                Token::SeqEnd,
                Token::Str("id"),
                Token::NewtypeStruct { name: "Id" },
                Token::Str("3"),
                Token::Str("reason"),
                Token::Some,
                Token::Str("some reason"),
                Token::Str("target_id"),
                Token::Some,
                Token::NewtypeStruct { name: "Id" },
                Token::Str("2"),
                Token::Str("user_id"),
                Token::Some,
                Token::NewtypeStruct { name: "Id" },
                Token::Str("1"),
                Token::StructEnd,
            ],
        );
    }
}