Skip to content

Commit 984db61

Browse files
committed
Move gdkrust_json converter class into ga_rust class implementation
1 parent f575ba0 commit 984db61

File tree

2 files changed

+59
-54
lines changed

2 files changed

+59
-54
lines changed

src/ga_rust.cpp

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,76 @@
66
// ??
77
#endif
88

9+
#include "../subprojects/gdk_rust/gdk_rust.h"
10+
11+
#include "exception.hpp"
912
#include "ga_rust.hpp"
1013
#include "ga_tor.hpp"
11-
#include "exception.hpp"
1214
#include "logging.hpp"
1315
#include "session.hpp"
1416
#include "utils.hpp"
1517

1618
namespace ga {
1719
namespace sdk {
1820

19-
static const std::string TOR_SOCKS5_PREFIX("socks5://");
21+
namespace {
22+
static const std::string TOR_SOCKS5_PREFIX("socks5://");
2023

21-
static inline void check_code(const int32_t return_code)
22-
{
23-
switch (return_code) {
24-
case GA_OK:
25-
return;
24+
class gdkrust_json {
25+
public:
26+
explicit gdkrust_json(const nlohmann::json& val)
27+
: gdkrust_json(val.dump())
28+
{
29+
}
30+
31+
explicit gdkrust_json(GDKRUST_json* json) { m_json = json; }
32+
33+
explicit gdkrust_json(const std::string& str) { GDKRUST_convert_string_to_json(str.c_str(), &m_json); }
34+
35+
static inline nlohmann::json from_serde(GDKRUST_json* json)
36+
{
37+
char* output;
38+
GDKRUST_convert_json_to_string(json, &output);
39+
40+
auto cppjson = nlohmann::json::parse(output);
41+
42+
GDKRUST_destroy_json(json);
43+
GDKRUST_destroy_string(output);
2644

27-
case GA_RECONNECT:
28-
case GA_SESSION_LOST:
29-
throw reconnect_error();
45+
return cppjson;
46+
}
47+
48+
GDKRUST_json* get() { return m_json; }
49+
50+
~gdkrust_json() { GDKRUST_destroy_json(m_json); }
51+
52+
private:
53+
GDKRUST_json* m_json;
54+
};
3055

31-
case GA_TIMEOUT:
32-
throw timeout_error();
56+
static void check_code(const int32_t return_code)
57+
{
58+
switch (return_code) {
59+
case GA_OK:
60+
return;
3361

34-
case GA_NOT_AUTHORIZED:
35-
throw login_error(""); // TODO: msg from rust
62+
case GA_RECONNECT:
63+
case GA_SESSION_LOST:
64+
throw reconnect_error();
3665

37-
case GA_ERROR:
38-
default:
39-
throw std::runtime_error("call failed with: " + std::to_string(return_code));
40-
break;
66+
case GA_TIMEOUT:
67+
throw timeout_error();
68+
69+
case GA_NOT_AUTHORIZED:
70+
throw login_error(""); // TODO: msg from rust
71+
72+
case GA_ERROR:
73+
default:
74+
throw std::runtime_error("call failed with: " + std::to_string(return_code));
75+
break;
76+
}
4177
}
42-
}
78+
} // namespace
4379

4480
ga_rust::ga_rust(const nlohmann::json& net_params)
4581
: session_impl(net_params)

src/ga_rust.hpp

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,13 @@
11
#pragma once
22

3-
#include "../subprojects/gdk_rust/gdk_rust.h"
43
#include "session_impl.hpp"
54

5+
struct GDKRUST_session;
6+
67
namespace ga {
78
namespace sdk {
89
struct tor_controller;
910

10-
class gdkrust_json {
11-
public:
12-
explicit gdkrust_json(const nlohmann::json& val)
13-
: gdkrust_json(val.dump())
14-
{
15-
}
16-
17-
explicit gdkrust_json(GDKRUST_json* json) { m_json = json; }
18-
19-
explicit gdkrust_json(const std::string& str) { GDKRUST_convert_string_to_json(str.c_str(), &m_json); }
20-
21-
static inline nlohmann::json from_serde(GDKRUST_json* json)
22-
{
23-
char* output;
24-
GDKRUST_convert_json_to_string(json, &output);
25-
26-
auto cppjson = nlohmann::json::parse(output);
27-
28-
GDKRUST_destroy_json(json);
29-
GDKRUST_destroy_string(output);
30-
31-
return cppjson;
32-
}
33-
34-
GDKRUST_json* get() { return m_json; }
35-
36-
~gdkrust_json() { GDKRUST_destroy_json(m_json); }
37-
38-
private:
39-
GDKRUST_json* m_json;
40-
};
41-
4211
class ga_rust final : public session_impl {
4312
public:
4413
explicit ga_rust(const nlohmann::json& net_params);
@@ -187,12 +156,12 @@ namespace sdk {
187156
static int32_t spv_verify_tx(const nlohmann::json& details);
188157

189158
private:
190-
static void GDKRUST_notif_handler(void* self_context, GDKRUST_json* json);
159+
static void GDKRUST_notif_handler(void* self_context, struct GDKRUST_json* json);
191160

192161
std::shared_ptr<tor_controller> m_tor_ctrl;
193162
bool m_reconnect_restart;
194163

195-
GDKRUST_session* m_session;
164+
struct GDKRUST_session* m_session;
196165
GA_notification_handler m_ga_notif_handler;
197166
void* m_ga_notif_context;
198167
};

0 commit comments

Comments
 (0)