Skip to content

Commit f7e4fbf

Browse files
committed
Code review cleanup
1 parent 0a9617f commit f7e4fbf

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

src/config/contacts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ blinded_contact_info::blinded_contact_info(
313313
comm{community(
314314
std::move(community_base_url), blinded_id.substr(2), std::move(community_pubkey))} {
315315
auto prefix = get_session_id_prefix(blinded_id);
316-
legacy_blinding = (prefix == session::SessionIdPrefix::community_blinded_legacy);
316+
legacy_blinding = (prefix == session::SessionIDPrefix::community_blinded_legacy);
317317

318-
if (prefix != session::SessionIdPrefix::community_blinded &&
319-
prefix != session::SessionIdPrefix::community_blinded_legacy)
318+
if (prefix != session::SessionIDPrefix::community_blinded &&
319+
prefix != session::SessionIDPrefix::community_blinded_legacy)
320320
throw std::invalid_argument{
321321
"Invalid blinded ID: Expected '15' or '25' prefix; got " + std::string{blinded_id}};
322322
}

src/config/internal.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,13 @@ namespace session::config {
1717

1818
namespace {
1919

20-
constexpr std::array<session::SessionIdPrefix, 6> all_session_id_prefixes = {
21-
session::SessionIdPrefix::standard,
22-
session::SessionIdPrefix::group,
23-
session::SessionIdPrefix::community_blinded_legacy,
24-
session::SessionIdPrefix::community_blinded,
25-
session::SessionIdPrefix::version_blinded,
26-
session::SessionIdPrefix::unblinded};
27-
28-
inline std::string to_string(session::SessionIdPrefix prefix) {
29-
switch (prefix) {
30-
case session::SessionIdPrefix::unblinded: return "00";
31-
case session::SessionIdPrefix::group: return "03";
32-
case session::SessionIdPrefix::standard: return "05";
33-
case session::SessionIdPrefix::community_blinded_legacy: return "15";
34-
case session::SessionIdPrefix::community_blinded: return "25";
35-
case session::SessionIdPrefix::version_blinded: return "07";
36-
}
37-
38-
return "05"; // Fallback to standard, shouldn't occur
39-
};
20+
constexpr std::array all_session_id_prefixes = {
21+
session::SessionIDPrefix::standard,
22+
session::SessionIDPrefix::group,
23+
session::SessionIDPrefix::community_blinded_legacy,
24+
session::SessionIDPrefix::community_blinded,
25+
session::SessionIDPrefix::version_blinded,
26+
session::SessionIDPrefix::unblinded};
4027

4128
} // namespace
4229

@@ -48,13 +35,10 @@ void check_session_id(std::string_view session_id, std::string_view prefix) {
4835
"; got " + std::string{session_id}};
4936
}
5037

51-
SessionIdPrefix get_session_id_prefix(std::string_view id) {
52-
std::string prefix_list_str = "";
53-
54-
if (oxenc::is_hex(id)) {
38+
SessionIDPrefix get_session_id_prefix(std::string_view id) {
39+
if (oxenc::is_hex(id) && id.size() == 66) {
5540
for (auto prefix : all_session_id_prefixes) {
5641
auto prefix_str = to_string(prefix);
57-
prefix_list_str += (prefix_list_str.empty() ? prefix_str : ", {}"_format(prefix_str));
5842

5943
if ((id.size() == 64 + prefix_str.size() &&
6044
id.substr(0, prefix_str.size()) == prefix_str))
@@ -63,9 +47,10 @@ SessionIdPrefix get_session_id_prefix(std::string_view id) {
6347
}
6448

6549
// If we get here then the id wasn't any of the currently defined prefixes
66-
throw std::invalid_argument{
67-
"Invalid session ID: expected 66 hex digits starting with one of [" + prefix_list_str +
68-
"]; got " + std::string{id}};
50+
throw std::invalid_argument{fmt::format(
51+
"Invalid session ID: expected 66 hex digits starting with one of [{}]; got {}",
52+
fmt::join(all_session_id_prefixes, ", "),
53+
id)};
6954
}
7055

7156
std::string session_id_to_bytes(std::string_view session_id, std::string_view prefix) {

src/config/internal.hpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace session {
1616

17-
enum class SessionIdPrefix {
17+
enum class SessionIDPrefix {
1818
standard,
1919
group,
2020
community_blinded_legacy,
@@ -23,8 +23,21 @@ enum class SessionIdPrefix {
2323
unblinded,
2424
};
2525

26+
inline constexpr std::string_view to_string(session::SessionIDPrefix prefix) {
27+
switch (prefix) {
28+
case session::SessionIDPrefix::unblinded: return "00"sv;
29+
case session::SessionIDPrefix::group: return "03"sv;
30+
case session::SessionIDPrefix::standard: return "05"sv;
31+
case session::SessionIDPrefix::community_blinded_legacy: return "15"sv;
32+
case session::SessionIDPrefix::community_blinded: return "25"sv;
33+
case session::SessionIDPrefix::version_blinded: return "07"sv;
34+
}
35+
36+
return "05"sv; // Fallback to standard, shouldn't occur
2637
};
2738

39+
}; // namespace session
40+
2841
namespace session::config {
2942

3043
template <typename ConfigT, typename... Args>
@@ -136,7 +149,7 @@ config_string_list* make_string_list(Container vals) {
136149
void check_session_id(std::string_view session_id, std::string_view prefix = "05");
137150

138151
// Throws std::invalid_argument if id doesn't look valid.
139-
SessionIdPrefix get_session_id_prefix(std::string_view id);
152+
SessionIDPrefix get_session_id_prefix(std::string_view id);
140153

141154
// Checks the session_id (throwing if invalid) then returns it as bytes
142155
std::string session_id_to_bytes(std::string_view session_id, std::string_view prefix = "05");
@@ -257,3 +270,14 @@ std::optional<std::vector<unsigned char>> zstd_decompress(
257270
std::span<const unsigned char> data, size_t max_size = 0);
258271

259272
} // namespace session::config
273+
274+
namespace fmt {
275+
276+
template <>
277+
struct formatter<session::SessionIDPrefix, char> : formatter<std::string_view> {
278+
auto format(const session::SessionIDPrefix& val, fmt::format_context& ctx) const {
279+
return formatter<std::string_view>::format(to_string(val), ctx);
280+
}
281+
};
282+
283+
} // namespace fmt

0 commit comments

Comments
 (0)