values_glue.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "base/rs_glue/values_glue.h"
  5. #include <stddef.h>
  6. #include <sstream>
  7. namespace base {
  8. namespace rs_glue {
  9. // This file has functions which are called from Rust code to populate
  10. // bits of a base::Value. The functions exist because Rust C++ FFI
  11. // is not yet quite good enough to operate on a base::Value directly
  12. // without these intermediate layer. With future inprovements in interop,
  13. // they may disappear.
  14. std::unique_ptr<ValueSlot> NewValueSlotForTesting() {
  15. return std::make_unique<ValueSlot>();
  16. }
  17. void ValueSetNoneKey(base::Value& v, rust::Str key) {
  18. v.GetDict().Set(base::RustStrToStringPiece(key), base::Value());
  19. }
  20. void ValueSetBoolKey(base::Value& v, rust::Str key, bool value) {
  21. v.GetDict().Set(base::RustStrToStringPiece(key), value);
  22. }
  23. void ValueSetIntegerKey(base::Value& v, rust::Str key, int value) {
  24. v.GetDict().Set(base::RustStrToStringPiece(key), value);
  25. }
  26. void ValueSetDoubleKey(base::Value& v, rust::Str key, double value) {
  27. v.GetDict().Set(base::RustStrToStringPiece(key), value);
  28. }
  29. void ValueSetStringKey(base::Value& v, rust::Str key, rust::Str value) {
  30. v.GetDict().Set(base::RustStrToStringPiece(key),
  31. base::RustStrToStringPiece(value));
  32. }
  33. base::Value& ValueSetDictKey(base::Value& v, rust::Str key) {
  34. return *v.GetDict().Set(base::RustStrToStringPiece(key), base::Value::Dict());
  35. }
  36. base::Value& ValueSetListKey(base::Value& v, rust::Str key) {
  37. return *v.GetDict().Set(base::RustStrToStringPiece(key), base::Value::List());
  38. }
  39. void ValueAppendNone(base::Value& v) {
  40. v.GetList().Append(base::Value());
  41. }
  42. void ValueAppendString(base::Value& v, rust::Str value) {
  43. v.GetList().Append(base::RustStrToStringPiece(value));
  44. }
  45. base::Value& ValueAppendDict(base::Value& v) {
  46. v.GetList().Append(base::Value::Dict());
  47. return v.GetList().back();
  48. }
  49. base::Value& ValueAppendList(base::Value& v) {
  50. v.GetList().Append(base::Value::List());
  51. return v.GetList().back();
  52. }
  53. void ValueReserveSize(base::Value& v, size_t len) {
  54. v.GetList().reserve(len);
  55. }
  56. rust::String DumpValueSlot(const ValueSlot& v) {
  57. std::ostringstream os;
  58. if (v.has_value()) {
  59. os << *v;
  60. } else {
  61. os << "(empty)";
  62. }
  63. return rust::String(os.str());
  64. }
  65. void ConstructNoneValue(ValueSlot& v) {
  66. v.emplace(base::Value::Type::NONE);
  67. }
  68. void ConstructBoolValue(ValueSlot& v, bool value) {
  69. v.emplace(value);
  70. }
  71. void ConstructIntegerValue(ValueSlot& v, int value) {
  72. v.emplace(value);
  73. }
  74. void ConstructDoubleValue(ValueSlot& v, double value) {
  75. v.emplace(value);
  76. }
  77. void ConstructStringValue(ValueSlot& v, rust::Str value) {
  78. v.emplace(base::RustStrToStringPiece(value));
  79. }
  80. base::Value& ConstructDictValue(ValueSlot& v) {
  81. return v.emplace(base::Value::Type::DICTIONARY);
  82. }
  83. base::Value& ConstructListValue(ValueSlot& v) {
  84. return v.emplace(base::Value::Type::LIST);
  85. }
  86. } // namespace rs_glue
  87. } // namespace base