-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from deeplex/dev/status-code-string-ref
feat: Add codecs for `system_error2` `string_ref` and `cncr::uuid`
- Loading branch information
Showing
10 changed files
with
270 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
// Copyright 2023 Henrik Steffen Gaßmann | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include "dplx/dp/codecs/system_error2.hpp" | ||
|
||
#include <dplx/dp/api.hpp> | ||
#include <dplx/dp/items/emit_core.hpp> | ||
#include <dplx/dp/items/item_size_of_core.hpp> | ||
|
||
namespace dplx::dp | ||
{ | ||
|
||
auto codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref>::size_of( | ||
emit_context &ctx, value_type const &value) noexcept -> std::uint64_t | ||
{ | ||
return dp::item_size_of_u8string(ctx, value.size()); | ||
} | ||
auto codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref>::encode( | ||
emit_context &ctx, value_type const &value) noexcept -> result<void> | ||
{ | ||
return dp::emit_u8string(ctx, value.data(), value.size()); | ||
} | ||
|
||
} // namespace dplx::dp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
// Copyright 2023 Henrik Steffen Gaßmann | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#pragma once | ||
|
||
#include <concepts> | ||
|
||
#include <status-code/status_code_domain.hpp> | ||
|
||
#include <dplx/dp/fwd.hpp> | ||
|
||
namespace dplx::dp | ||
{ | ||
|
||
template <> | ||
class codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref> | ||
{ | ||
using value_type = SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref; | ||
|
||
public: | ||
static auto size_of(emit_context &ctx, value_type const &value) noexcept | ||
-> std::uint64_t; | ||
static auto encode(emit_context &ctx, value_type const &value) noexcept | ||
-> result<void>; | ||
}; | ||
template <> | ||
class codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain:: | ||
atomic_refcounted_string_ref> | ||
: public codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref> | ||
{ | ||
}; | ||
template <std::derived_from< | ||
SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref> StringRef> | ||
class codec<StringRef> | ||
: public codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref> | ||
{ | ||
}; | ||
|
||
} // namespace dplx::dp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
// Copyright 2023 Henrik Steffen Gaßmann | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include "dplx/dp/codecs/system_error2.hpp" | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <dplx/dp/api.hpp> | ||
|
||
#include "blob_matcher.hpp" | ||
#include "item_sample_ct.hpp" | ||
#include "test_input_stream.hpp" | ||
#include "test_output_stream.hpp" | ||
#include "test_utils.hpp" | ||
|
||
namespace dp_tests | ||
{ | ||
|
||
using string_ref = dp::system_error::status_code_domain::string_ref; | ||
|
||
TEST_CASE("system_error2::status_code_domain::string_ref should be encodable") | ||
{ | ||
item_sample_ct<std::string_view> const sample{ | ||
"some", 5, {0x64, u8's', u8'o', u8'm', u8'e'} | ||
}; | ||
string_ref value(sample.value.data(), sample.value.size()); | ||
|
||
SECTION("to a stream") | ||
{ | ||
simple_test_output_stream outputStream(sample.encoded_length); | ||
|
||
REQUIRE(dp::encode(outputStream, value)); | ||
|
||
CHECK_BLOB_EQ(outputStream.written(), sample.encoded_bytes()); | ||
} | ||
SECTION("with a size_of operator") | ||
{ | ||
CHECK(dp::encoded_size_of(value) == sample.encoded_length); | ||
} | ||
} | ||
|
||
} // namespace dp_tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
// Copyright 2023 Henrik Steffen Gaßmann | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include "dplx/dp/codecs/uuid.hpp" | ||
|
||
#include <dplx/dp/items.hpp> | ||
|
||
namespace dplx::dp | ||
{ | ||
|
||
auto dplx::dp::codec<cncr::uuid>::decode(parse_context &ctx, | ||
cncr::uuid &value) noexcept | ||
-> result<void> | ||
{ | ||
constexpr auto stateSize = cncr::uuid::state_size; | ||
DPLX_TRY(dp::expect_item_head(ctx, type_code::binary, stateSize)); | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) | ||
cncr::blob<std::byte, stateSize, stateSize> raw; | ||
DPLX_TRY(ctx.in.bulk_read(static_cast<std::byte *>(raw.values), stateSize)); | ||
|
||
value = cncr::uuid(raw.values); | ||
return oc::success(); | ||
} | ||
|
||
auto codec<cncr::uuid>::encode(emit_context &ctx, | ||
cncr::uuid const value) noexcept -> result<void> | ||
{ | ||
auto const canonical = value.canonical(); | ||
return dp::emit_binary(ctx, | ||
static_cast<std::byte const *>(canonical.values), | ||
sizeof(canonical.values)); | ||
} | ||
|
||
auto codec<cncr::uuid>::size_of(emit_context &ctx, cncr::uuid) noexcept | ||
-> std::uint64_t | ||
{ | ||
return dp::item_size_of_binary(ctx, cncr::uuid::state_size); | ||
} | ||
|
||
} // namespace dplx::dp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
// Copyright 2023 Henrik Steffen Gaßmann | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#pragma once | ||
|
||
#include <dplx/cncr/uuid.hpp> | ||
|
||
#include <dplx/dp/fwd.hpp> | ||
|
||
namespace dplx::dp | ||
{ | ||
|
||
template <> | ||
class codec<cncr::uuid> | ||
{ | ||
public: | ||
static auto decode(parse_context &ctx, cncr::uuid &value) noexcept | ||
-> result<void>; | ||
static auto encode(emit_context &ctx, cncr::uuid value) noexcept | ||
-> result<void>; | ||
static auto size_of(emit_context &ctx, cncr::uuid value) noexcept | ||
-> std::uint64_t; | ||
}; | ||
|
||
} // namespace dplx::dp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
// Copyright 2023 Henrik Steffen Gaßmann | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#include "dplx/dp/codecs/uuid.hpp" | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
#include <catch2/generators/catch_generators.hpp> | ||
|
||
#include <dplx/dp/api.hpp> | ||
|
||
#include "blob_matcher.hpp" | ||
#include "item_sample_ct.hpp" | ||
#include "range_generator.hpp" | ||
#include "test_input_stream.hpp" | ||
#include "test_output_stream.hpp" | ||
#include "test_utils.hpp" | ||
|
||
namespace dp_tests | ||
{ | ||
|
||
namespace | ||
{ | ||
|
||
using namespace cncr::uuid_literals; | ||
|
||
constexpr item_sample_ct<cncr::uuid, 20U> uuid_samples[] = { | ||
{"00000000-0000-0000-0000-000000000000"_uuid, | ||
17, {0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, | ||
{"00000000-0000-4000-bfff-ffffffffffff"_uuid, | ||
17, {0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0xbf, 0xff, | ||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, | ||
{"ffffffff-ffff-ffff-ffff-ffffffffffff"_uuid, | ||
17, {0x50, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | ||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_CASE("cncr::uuid has a codec") | ||
{ | ||
auto const sample = GENERATE(borrowed_range(uuid_samples)); | ||
INFO(sample); | ||
|
||
SECTION("with encode") | ||
{ | ||
simple_test_output_stream outputStream(sample.encoded_length); | ||
|
||
REQUIRE(dp::encode(outputStream, sample.value)); | ||
|
||
CHECK_BLOB_EQ(outputStream.written(), sample.encoded_bytes()); | ||
} | ||
SECTION("with size_of") | ||
{ | ||
CHECK(dp::encoded_size_of(sample.value) == sample.encoded_length); | ||
} | ||
SECTION("with decode") | ||
{ | ||
simple_test_input_stream inputStream(sample.encoded_bytes()); | ||
|
||
cncr::uuid value{}; | ||
REQUIRE(dp::decode(inputStream, value)); | ||
|
||
CHECK(value == sample.value); | ||
} | ||
} | ||
|
||
} // namespace dp_tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters