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);
80  ComposedModifier<RoomModel> withRoomMembership(RoomMembership membership);
81  ComposedModifier<RoomModel> withRoomEncrypted(bool encrypted);
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 }
Kazv::Factory::withAccountData
ComposedModifier< ClientModel > withAccountData(immer::flex_vector< Event > accountDataEvent)
Definition: factory.cpp:60
Kazv::Factory::AttrModifier< DataType ModelType::* >::ModelT
ModelType ModelT
Definition: factory.hpp:43
Kazv::Factory::withRoomMembership
ComposedModifier< RoomModel > withRoomMembership(RoomMembership membership)
Definition: factory.cpp:163
Kazv::Factory::AttrModifier< DataType ModelType::* >::AttrModifier
AttrModifier(PtrT ptr, const DataType &data)
Definition: factory.hpp:46
Kazv::Factory::withEventRelationship
ComposedModifier< Event > withEventRelationship(std::string relType, std::string eventId)
Definition: factory.cpp:265
Kazv::Factory::withResponseStatusCode
ComposedModifier< Response > withResponseStatusCode(int code)
Definition: factory.cpp:290
Kazv::Factory::withRoomTimeline
ComposedModifier< RoomModel > withRoomTimeline(immer::flex_vector< Event > timelineEvents)
Definition: factory.cpp:149
Kazv::RoomMembership
RoomMembership
Definition: types.hpp:116
Kazv::Factory::makeClient
ClientModel makeClient(const ComposedModifier< ClientModel > &mod)
Definition: factory.cpp:41
Kazv::Factory::withRoomId
ComposedModifier< RoomModel > withRoomId(std::string id)
Definition: factory.cpp:130
Kazv::Factory::withResponseBytesBody
ComposedModifier< Response > withResponseBytesBody(const Bytes &body)
Definition: factory.cpp:300
Kazv::Factory::withEventContent
ComposedModifier< Event > withEventContent(const json &content)
Definition: factory.cpp:235
Kazv::Factory::withRoom
ComposedModifier< ClientModel > withRoom(RoomModel room)
Definition: factory.cpp:53
Kazv::Factory::withEventId
ComposedModifier< Event > withEventId(std::string id)
Definition: factory.cpp:225
Kazv::Factory::withRoomAccountData
ComposedModifier< RoomModel > withRoomAccountData(immer::flex_vector< Event > accountDataEvent)
Definition: factory.cpp:135
Kazv::Factory::withAttr
AttrModifier< PointerToMember > withAttr(PointerToMember p, const typename AttrModifier< PointerToMember >::DataT &data)
Definition: factory.hpp:54
Kazv::Factory::withCrypto
ComposedModifier< ClientModel > withCrypto(const Crypto &crypto)
Definition: factory.cpp:67
Kazv::Factory::withMembership
ComposedModifier< Event > withMembership(std::string membership)
Definition: factory.cpp:245
Kazv
Definition: location.hpp:10
Kazv::Factory::ComposedModifier
Definition: factory.hpp:17
Kazv::Factory::withResponseFileBody
ComposedModifier< Response > withResponseFileBody(const FileDesc &body)
Definition: factory.cpp:305
client-model.hpp
Kazv::Factory::withEventJson
ComposedModifier< Event > withEventJson(const json &j)
Definition: factory.cpp:209
Kazv::Factory::makeMemberEvent
Event makeMemberEvent(const ComposedModifier< Event > &mod)
Definition: factory.cpp:193
Kazv::Factory::withMemberAvatarUrl
ComposedModifier< Event > withMemberAvatarUrl(std::string avatarUrl)
Definition: factory.cpp:255
Kazv::Factory::ComposedModifier::operator|
ComposedModifier operator|(const ComposedModifier &that) const
Definition: factory.hpp:25
Kazv::Factory::withRoomState
ComposedModifier< RoomModel > withRoomState(immer::flex_vector< Event > stateEvent)
Definition: factory.cpp:142
Kazv::Factory::withEventKV
ComposedModifier< Event > withEventKV(const json::json_pointer &k, const json &v)
Definition: factory.cpp:216
Kazv::json
nlohmann::json json
Definition: jsonwrap.hpp:20
Kazv::Factory::ComposedModifier::BaseT
std::function< void(ModelType &)> BaseT
Definition: factory.hpp:19
Kazv::Factory::withDeviceTrustLevel
ComposedModifier< DeviceKeyInfo > withDeviceTrustLevel(DeviceTrustLevel trustLevel)
Definition: factory.cpp:110
Kazv::Factory::makeRoom
RoomModel makeRoom(const ComposedModifier< RoomModel > &mod)
Definition: factory.cpp:122
Kazv::Factory::withEventType
ComposedModifier< Event > withEventType(std::string type)
Definition: factory.cpp:230
Kazv::Factory::withDeviceDisplayName
ComposedModifier< DeviceKeyInfo > withDeviceDisplayName(std::string displayName)
Definition: factory.cpp:105
Kazv::Factory::AttrModifier< DataType ModelType::* >::DataT
DataType DataT
Definition: factory.hpp:44
Kazv::Factory::ComposedModifier::ComposedModifier
ComposedModifier()
Definition: factory.hpp:23
Kazv::Bytes
std::string Bytes
Definition: types.hpp:27
Kazv::Factory::withDevice
ComposedModifier< ClientModel > withDevice(std::string userId, DeviceKeyInfo info)
Definition: factory.cpp:74
Kazv::Factory::makeDeviceKeyInfo
DeviceKeyInfo makeDeviceKeyInfo(const ComposedModifier< DeviceKeyInfo > &mod)
Definition: factory.cpp:84
Kazv::Factory::makeCrypto
Crypto makeCrypto(const ComposedModifier< Crypto > &mod)
Definition: factory.cpp:115
Kazv::Factory::withEventSenderId
ComposedModifier< Event > withEventSenderId(std::string sender)
Definition: factory.cpp:260
Kazv::Factory::AttrModifier
Definition: factory.hpp:35
Kazv::Factory::makeResponse
Response makeResponse(std::string jobId, const ComposedModifier< Response > &mod)
Definition: factory.cpp:280
Kazv::Factory::withRoomEncrypted
ComposedModifier< RoomModel > withRoomEncrypted(bool encrypted)
Definition: factory.cpp:170
libkazv-config.hpp
Kazv::Factory::withMemberDisplayName
ComposedModifier< Event > withMemberDisplayName(std::string displayName)
Definition: factory.cpp:250
Kazv::Factory::withRoomInviteState
ComposedModifier< RoomModel > withRoomInviteState(immer::flex_vector< Event > stateEvent)
Kazv::Factory::withResponseJsonBody
ComposedModifier< Response > withResponseJsonBody(const json &body)
Definition: factory.cpp:295
Kazv::ClientModel
Definition: client-model.hpp:58
Kazv::DeviceTrustLevel
DeviceTrustLevel
Definition: device-list-tracker.hpp:26
Kazv::Factory::withResponseDataKV
ComposedModifier< Response > withResponseDataKV(std::string k, const json &v)
Definition: factory.cpp:310
Kazv::Factory::withStateKey
ComposedModifier< Event > withStateKey(std::string id)
Definition: factory.cpp:240
Kazv::Factory::withDeviceId
ComposedModifier< DeviceKeyInfo > withDeviceId(std::string deviceId)
Definition: factory.cpp:100
Kazv::Factory::withEventReplyTo
ComposedModifier< Event > withEventReplyTo(std::string eventId)
Definition: factory.cpp:273
Kazv::Factory::withRoomTimelineGaps
ComposedModifier< RoomModel > withRoomTimelineGaps(immer::map< std::string, std::string > timelineGaps)
Definition: factory.cpp:156
Kazv::Factory::makeEvent
Event makeEvent(const ComposedModifier< Event > &mod)
Definition: factory.cpp:177
Kazv::Factory::AttrModifier< DataType ModelType::* >::PtrT
DataType ModelType::* PtrT
Definition: factory.hpp:42