• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "google/protobuf/compiler/rust/accessors/default_value.h"
9 
10 #include <cmath>
11 #include <limits>
12 #include <string>
13 
14 #include "absl/log/absl_log.h"
15 #include "absl/strings/escaping.h"
16 #include "absl/strings/str_cat.h"
17 #include "absl/strings/str_format.h"
18 #include "google/protobuf/compiler/rust/context.h"
19 #include "google/protobuf/compiler/rust/naming.h"
20 #include "google/protobuf/compiler/rust/rust_field_type.h"
21 #include "google/protobuf/descriptor.h"
22 #include "google/protobuf/io/strtod.h"
23 #include "google/protobuf/port.h"
24 
25 namespace google {
26 namespace protobuf {
27 namespace compiler {
28 namespace rust {
29 
DefaultValue(Context & ctx,const FieldDescriptor & field)30 std::string DefaultValue(Context& ctx, const FieldDescriptor& field) {
31   switch (GetRustFieldType(field)) {
32     case RustFieldType::DOUBLE:
33       if (std::isfinite(field.default_value_double())) {
34         return absl::StrCat(io::SimpleDtoa(field.default_value_double()),
35                             "f64");
36       } else if (std::isnan(field.default_value_double())) {
37         return std::string("f64::NAN");
38       } else if (field.default_value_double() ==
39                  std::numeric_limits<double>::infinity()) {
40         return std::string("f64::INFINITY");
41       } else if (field.default_value_double() ==
42                  -std::numeric_limits<double>::infinity()) {
43         return std::string("f64::NEG_INFINITY");
44       } else {
45         ABSL_LOG(FATAL) << "unreachable";
46       }
47     case RustFieldType::FLOAT:
48       if (std::isfinite(field.default_value_float())) {
49         return absl::StrCat(io::SimpleFtoa(field.default_value_float()), "f32");
50       } else if (std::isnan(field.default_value_float())) {
51         return std::string("f32::NAN");
52       } else if (field.default_value_float() ==
53                  std::numeric_limits<float>::infinity()) {
54         return std::string("f32::INFINITY");
55       } else if (field.default_value_float() ==
56                  -std::numeric_limits<float>::infinity()) {
57         return std::string("f32::NEG_INFINITY");
58       } else {
59         ABSL_LOG(FATAL) << "unreachable";
60       }
61     case RustFieldType::INT32:
62       return absl::StrFormat("%di32", field.default_value_int32());
63     case RustFieldType::INT64:
64       return absl::StrFormat("%di64", field.default_value_int64());
65     case RustFieldType::UINT64:
66       return absl::StrFormat("%uu64", field.default_value_uint64());
67     case RustFieldType::UINT32:
68       return absl::StrFormat("%uu32", field.default_value_uint32());
69     case RustFieldType::BOOL:
70       return absl::StrFormat("%v", field.default_value_bool());
71     case RustFieldType::STRING:
72     case RustFieldType::BYTES:
73       return absl::StrFormat("b\"%s\"",
74                              absl::CHexEscape(field.default_value_string()));
75     case RustFieldType::ENUM:
76       // `$EnumName$::default()` might seem like the right choice here, but
77       // it is not. The default value for the enum type isn't the same as the
78       // field, since in `syntax = "proto2"`, an enum field can have a default
79       // value other than the first listed in the enum.
80       //
81       // Even in cases where there is no custom field default, `default()` can't
82       // be used. This is because the vtables for field mutators store the
83       // default value. They are `static`s which are constructed with a `const`
84       // expression. Trait methods in a `const` context aren't currently stable.
85       return absl::StrCat(RsTypePath(ctx, field),
86                           "::", EnumValueRsName(*field.default_value_enum()));
87     case RustFieldType::MESSAGE:
88       ABSL_LOG(FATAL) << "Messages can't have defaults: " << field.type_name();
89   }
90   ABSL_LOG(ERROR) << "unreachable";
91   internal::Unreachable();
92 }
93 
94 }  // namespace rust
95 }  // namespace compiler
96 }  // namespace protobuf
97 }  // namespace google
98