test_string_traits.cc 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "test_string_traits.h"
  5. namespace crdtp {
  6. // Test-only. Real-life bindings use UTF8/16 conversions as needed.
  7. bool ProtocolTypeTraits<std::string>::Deserialize(DeserializerState* state,
  8. std::string* value) {
  9. if (state->tokenizer()->TokenTag() == cbor::CBORTokenTag::STRING8) {
  10. auto cbor_span = state->tokenizer()->GetString8();
  11. value->assign(reinterpret_cast<const char*>(cbor_span.data()),
  12. cbor_span.size());
  13. return true;
  14. }
  15. state->RegisterError(Error::BINDINGS_STRING8_VALUE_EXPECTED);
  16. return false;
  17. }
  18. // static
  19. void ProtocolTypeTraits<std::string>::Serialize(const std::string& value,
  20. std::vector<uint8_t>* bytes) {
  21. cbor::EncodeString8(
  22. span<uint8_t>(reinterpret_cast<const uint8_t*>(value.data()),
  23. value.size()),
  24. bytes);
  25. }
  26. } // namespace crdtp