libkazv
factory.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2023 tusooa <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include <libkazv-config.hpp>
9 
10 #include <client-model.hpp>
11 
12 namespace Kazv
13 {
14  namespace Factory
15  {
16  template<class ModelType>
17  struct ComposedModifier : std::function<void(ModelType &)>
18  {
19  using BaseT = std::function<void(ModelType &)>;
20  using BaseT::BaseT;
21  using BaseT::operator();
22 
23  ComposedModifier() : BaseT([](ModelType &) {}) {}
24 
26  {
27  return ComposedModifier([that, *this](ModelType &model) {
28  (*this)(model);
29  that(model);
30  });
31  }
32  };
33 
34  template<class PointerToMember>
35  struct AttrModifier
36  {};
37 
38  template<class ModelType, class DataType>
39  struct AttrModifier<DataType ModelType::*> : public ComposedModifier<ModelType>
40  {
42  using PtrT = DataType ModelType::*;
43  using ModelT = ModelType;
44  using DataT = DataType;
45 
46  AttrModifier(PtrT ptr, const DataType &data)
47  : BaseT([ptr, data](ModelType &m) {
48  m.*ptr = data;
49  })
50  {}
51  };
52 
53  template<class PointerToMember>
55  {
56  return AttrModifier<PointerToMember>(p, data);
57  }
58 
59  ClientModel makeClient(const ComposedModifier<ClientModel> &mod = {});
60  ComposedModifier<ClientModel> withRoom(RoomModel room);
61  ComposedModifier<ClientModel> withAccountData(immer::flex_vector<Event> accountDataEvent);
62  ComposedModifier<ClientModel> withCrypto(const Crypto &crypto);
63  ComposedModifier<ClientModel> withDevice(std::string userId, DeviceKeyInfo info);
64 
65  DeviceKeyInfo makeDeviceKeyInfo(const ComposedModifier<DeviceKeyInfo> &mod = {});
66  ComposedModifier<DeviceKeyInfo> withDeviceId(std::string deviceId);
67  ComposedModifier<DeviceKeyInfo> withDeviceDisplayName(std::string displayName);
68  ComposedModifier<DeviceKeyInfo> withDeviceTrustLevel(DeviceTrustLevel trustLevel);
69 
70  Crypto makeCrypto(const ComposedModifier<Crypto> &mod = {});
71 
72  RoomModel makeRoom(const ComposedModifier<RoomModel> &mod = {});
73 
74  ComposedModifier<RoomModel> withRoomId(std::string id);
75  ComposedModifier<RoomModel> withRoomAccountData(immer::flex_vector<Event> accountDataEvent);
76  ComposedModifier<RoomModel> withRoomState(immer::flex_vector<Event> stateEvent);
77  ComposedModifier<RoomModel> withRoomInviteState(immer::flex_vector<Event> stateEvent);
78  ComposedModifier<RoomModel> withRoomTimeline(immer::flex_vector<Event> timelineEvents);
79  ComposedModifier<RoomModel> withRoomTimelineGaps(immer::map<std::string, std::string> timelineGaps);
82 
83  Event makeEvent(const ComposedModifier<Event> &mod = {});
84  Event makeMemberEvent(const ComposedModifier<Event> &mod = {});
85  ComposedModifier<Event> withEventJson(const json &j);
86  ComposedModifier<Event> withEventKV(const json::json_pointer &k, const json &v);
87  ComposedModifier<Event> withEventId(std::string id);
88  ComposedModifier<Event> withEventType(std::string type);
89  ComposedModifier<Event> withEventContent(const json &content);
90  ComposedModifier<Event> withStateKey(std::string id);
91  ComposedModifier<Event> withMembership(std::string membership);
92  ComposedModifier<Event> withMemberDisplayName(std::string displayName);
93  ComposedModifier<Event> withMemberAvatarUrl(std::string avatarUrl);
94  ComposedModifier<Event> withEventSenderId(std::string sender);
95  ComposedModifier<Event> withEventRelationship(std::string relType, std::string eventId);
96  ComposedModifier<Event> withEventReplyTo(std::string eventId);
97 
98  Response makeResponse(std::string jobId, const ComposedModifier<Response> &mod = {});
99  ComposedModifier<Response> withResponseStatusCode(int code);
100  ComposedModifier<Response> withResponseJsonBody(const json &body);
101  ComposedModifier<Response> withResponseBytesBody(const Bytes &body);
102  ComposedModifier<Response> withResponseFileBody(const FileDesc &body);
103  ComposedModifier<Response> withResponseDataKV(std::string k, const json &v);
104  }
105 }
Definition: event.hpp:21
ComposedModifier< RoomModel > withRoomState(immer::flex_vector< Event > stateEvent)
Definition: factory.cpp:142
RoomModel makeRoom(const ComposedModifier< RoomModel > &mod)
Definition: factory.cpp:122
ComposedModifier< ClientModel > withCrypto(const Crypto &crypto)
Definition: factory.cpp:67
ComposedModifier< Event > withEventReplyTo(std::string eventId)
Definition: factory.cpp:273
ComposedModifier< DeviceKeyInfo > withDeviceId(std::string deviceId)
Definition: factory.cpp:100
ComposedModifier< RoomModel > withRoomTimelineGaps(immer::map< std::string, std::string > timelineGaps)
Definition: factory.cpp:156
ComposedModifier< Event > withEventId(std::string id)
Definition: factory.cpp:225
ComposedModifier< Event > withEventRelationship(std::string relType, std::string eventId)
Definition: factory.cpp:265
ComposedModifier< Event > withEventContent(const json &content)
Definition: factory.cpp:235
ComposedModifier< Event > withMemberAvatarUrl(std::string avatarUrl)
Definition: factory.cpp:255
ComposedModifier< RoomModel > withRoomEncrypted(bool encrypted)
Definition: factory.cpp:170
DeviceKeyInfo makeDeviceKeyInfo(const ComposedModifier< DeviceKeyInfo > &mod)
Definition: factory.cpp:84
ComposedModifier< RoomModel > withRoomTimeline(immer::flex_vector< Event > timelineEvents)
Definition: factory.cpp:149
ComposedModifier< RoomModel > withRoomMembership(RoomMembership membership)
Definition: factory.cpp:163
ComposedModifier< Event > withEventJson(const json &j)
Definition: factory.cpp:209
ClientModel makeClient(const ComposedModifier< ClientModel > &mod)
Definition: factory.cpp:41
ComposedModifier< DeviceKeyInfo > withDeviceDisplayName(std::string displayName)
Definition: factory.cpp:105
ComposedModifier< Event > withStateKey(std::string id)
Definition: factory.cpp:240
ComposedModifier< Response > withResponseStatusCode(int code)
Definition: factory.cpp:290
ComposedModifier< Event > withEventSenderId(std::string sender)
Definition: factory.cpp:260
ComposedModifier< Event > withEventType(std::string type)
Definition: factory.cpp:230
ComposedModifier< ClientModel > withRoom(RoomModel room)
Definition: factory.cpp:53
ComposedModifier< Response > withResponseJsonBody(const json &body)
Definition: factory.cpp:295
ComposedModifier< Response > withResponseDataKV(std::string k, const json &v)
Definition: factory.cpp:310
ComposedModifier< Response > withResponseBytesBody(const Bytes &body)
Definition: factory.cpp:300
ComposedModifier< Event > withEventKV(const json::json_pointer &k, const json &v)
Definition: factory.cpp:216
ComposedModifier< ClientModel > withAccountData(immer::flex_vector< Event > accountDataEvent)
Definition: factory.cpp:60
Event makeMemberEvent(const ComposedModifier< Event > &mod)
Definition: factory.cpp:193
ComposedModifier< Response > withResponseFileBody(const FileDesc &body)
Definition: factory.cpp:305
Event makeEvent(const ComposedModifier< Event > &mod)
Definition: factory.cpp:177
ComposedModifier< Event > withMembership(std::string membership)
Definition: factory.cpp:245
ComposedModifier< DeviceKeyInfo > withDeviceTrustLevel(DeviceTrustLevel trustLevel)
Definition: factory.cpp:110
ComposedModifier< Event > withMemberDisplayName(std::string displayName)
Definition: factory.cpp:250
ComposedModifier< ClientModel > withDevice(std::string userId, DeviceKeyInfo info)
Definition: factory.cpp:74
Crypto makeCrypto(const ComposedModifier< Crypto > &mod)
Definition: factory.cpp:115
ComposedModifier< RoomModel > withRoomId(std::string id)
Definition: factory.cpp:130
Response makeResponse(std::string jobId, const ComposedModifier< Response > &mod)
Definition: factory.cpp:280
ComposedModifier< RoomModel > withRoomInviteState(immer::flex_vector< Event > stateEvent)
ComposedModifier< RoomModel > withRoomAccountData(immer::flex_vector< Event > accountDataEvent)
Definition: factory.cpp:135
AttrModifier< PointerToMember > withAttr(PointerToMember p, const typename AttrModifier< PointerToMember >::DataT &data)
Definition: factory.hpp:54
Definition: location.hpp:10
DeviceTrustLevel
Definition: device-list-tracker.hpp:27
nlohmann::json json
Definition: jsonwrap.hpp:20
std::string Bytes
Definition: types.hpp:27
RoomMembership
Definition: types.hpp:128
Definition: client-model.hpp:59
DataType ModelType::* PtrT
Definition: factory.hpp:42
AttrModifier(PtrT ptr, const DataType &data)
Definition: factory.hpp:46
ModelType ModelT
Definition: factory.hpp:43
DataType DataT
Definition: factory.hpp:44
Definition: factory.hpp:36
Definition: factory.hpp:18
ComposedModifier operator|(const ComposedModifier &that) const
Definition: factory.hpp:25
std::function< void(ModelType &)> BaseT
Definition: factory.hpp:19
ComposedModifier()
Definition: factory.hpp:23