Skip to content

Commit

Permalink
Merge pull request #15 from deeplex/dev/status-code-string-ref
Browse files Browse the repository at this point in the history
feat: Add codecs for `system_error2` `string_ref` and `cncr::uuid`
  • Loading branch information
BurningEnlightenment authored Apr 19, 2023
2 parents ca7b0a0 + 7fdd5cf commit 9d5015b
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ CheckOptions:
value: true
- key: cppcoreguidelines-macro-usage.AllowedRegexp
value: "DPLX_.+_WORKAROUND(_TESTED_AT)?"
- key: readability-function-cognitive-complexity.IgnoreMacros
value: true
4 changes: 2 additions & 2 deletions .github/workflows/cpp-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
uses: lukka/run-vcpkg@v10
with:
vcpkgDirectory: ${{ github.workspace }}/build/vcpkg
vcpkgGitCommitId: 1271068e139c1fc30bae405c0bca0e379e155bd2
vcpkgGitCommitId: 501db0f17ef6df184fcdbfbe0f87cde2313b6ab1
prependedCacheKey: compiler=${{ matrix.compiler }}
#appendedCacheKey: r00

Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
uses: lukka/run-vcpkg@v10
with:
vcpkgDirectory: ${{ github.workspace }}/../vcpkg
vcpkgGitCommitId: 1271068e139c1fc30bae405c0bca0e379e155bd2
vcpkgGitCommitId: 501db0f17ef6df184fcdbfbe0f87cde2313b6ab1
prependedCacheKey: compiler=clang-15
#appendedCacheKey: r00

Expand Down
2 changes: 2 additions & 0 deletions sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ dplx_target_sources(deeppack
dp/codecs/std-chrono
dp/codecs/std-filesystem
dp/codecs/std-string
dp/codecs/system_error2
dp/codecs/uuid

dp/items/skip_item
)
Expand Down
28 changes: 28 additions & 0 deletions src/dplx/dp/codecs/system_error2.cpp
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
43 changes: 43 additions & 0 deletions src/dplx/dp/codecs/system_error2.hpp
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
46 changes: 46 additions & 0 deletions src/dplx/dp/codecs/system_error2.test.cpp
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
45 changes: 45 additions & 0 deletions src/dplx/dp/codecs/uuid.cpp
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
29 changes: 29 additions & 0 deletions src/dplx/dp/codecs/uuid.hpp
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
72 changes: 72 additions & 0 deletions src/dplx/dp/codecs/uuid.test.cpp
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
2 changes: 1 addition & 1 deletion vcpkg-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg-configuration.schema.json",
"default-registry": {
"kind": "builtin",
"baseline": "1271068e139c1fc30bae405c0bca0e379e155bd2"
"baseline": "501db0f17ef6df184fcdbfbe0f87cde2313b6ab1"
},
"registries": [
{
Expand Down

0 comments on commit 9d5015b

Please sign in to comment.