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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use lemmy_api_common::{
    comment::*, community::*, custom_emoji::*, person::*, post::*, private_message::*, site::*,
};
use serde::Serialize;
use std::fmt;

use crate::utils::impl_marker_trait;

pub trait LemmyForm: Serialize + Clone + fmt::Debug {}

#[derive(Debug, Clone)]
/// A request to send to lemmy. If you don't want to set the JWT for each request, you can set the Authorization header with [`LemmyClient::headers_mut`](lemmy_client::LemmyClient.headers_mut).
pub struct LemmyRequest<Body>
where
    Body: LemmyForm,
{
    /// The body to send with the request. Uses [`unit`] for when a body is not required.
    pub body: Body,
    /// The JWT that is used when authorization is required.
    pub jwt: Option<String>,
}

impl LemmyRequest<()> {
    /// Returns a request with no body or JWT.
    pub fn empty() -> Self {
        Self {
            body: (),
            jwt: None,
        }
    }

    /// Returns a request with no body and JWT if [`Some`].
    pub fn from_jwt(jwt: Option<String>) -> Self {
        Self { body: (), jwt }
    }
}

impl<Form> From<Form> for LemmyRequest<Form>
where
    Form: LemmyForm,
{
    fn from(body: Form) -> Self {
        Self { body, jwt: None }
    }
}

impl_marker_trait!(
    LemmyForm,
    [
        (),
        String,
        // Comments
        CreateComment,
        CreateCommentLike,
        CreateCommentReport,
        DeleteComment,
        DistinguishComment,
        EditComment,
        GetComment,
        GetComments,
        ListCommentLikes,
        ListCommentReports,
        RemoveComment,
        ResolveCommentReport,
        SaveComment,
        // Communities
        AddModToCommunity,
        BanFromCommunity,
        BlockCommunity,
        CreateCommunity,
        DeleteCommunity,
        EditCommunity,
        FollowCommunity,
        GetCommunity,
        HideCommunity,
        ListCommunities,
        RemoveCommunity,
        TransferCommunity,
        // Emojis
        CreateCustomEmoji,
        DeleteCustomEmoji,
        EditCustomEmoji,
        // Person
        AddAdmin,
        BanPerson,
        BlockPerson,
        ChangePassword,
        DeleteAccount,
        GetPersonDetails,
        GetPersonMentions,
        GetReplies,
        GetReportCount,
        Login,
        MarkCommentReplyAsRead,
        MarkPersonMentionAsRead,
        PasswordChangeAfterReset,
        PasswordReset,
        PersonMentionResponse,
        Register,
        SaveUserSettings,
        UpdateTotp,
        VerifyEmail,
        // Posts
        CreatePost,
        CreatePostLike,
        CreatePostReport,
        DeletePost,
        EditPost,
        FeaturePost,
        GetPost,
        GetPosts,
        GetSiteMetadata,
        ListPostLikes,
        ListPostReports,
        LockPost,
        MarkPostAsRead,
        RemovePost,
        ResolvePostReport,
        SavePost,
        HidePost,
        // Private Messages
        CreatePrivateMessage,
        CreatePrivateMessageReport,
        DeletePrivateMessage,
        EditPrivateMessage,
        GetPrivateMessages,
        ListPrivateMessageReports,
        MarkPrivateMessageAsRead,
        ResolvePrivateMessageReport,
        // Site
        ApproveRegistrationApplication,
        BlockInstance,
        CreateSite,
        EditSite,
        FederatedInstances,
        GetModlog,
        InstanceWithFederationState,
        ListRegistrationApplications,
        PurgeComment,
        PurgeCommunity,
        PurgePerson,
        PurgePost,
        ResolveObject,
        Search,
        // Media
        ListMedia
    ]
);