/* * Copyright 2018 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // independent from idl_parser, since this code is not needed for most clients #include "idl_gen_rust.h" #include <cmath> #include "flatbuffers/code_generators.h" #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" #include "idl_namer.h" namespace flatbuffers { namespace { static Namer::Config RustDefaultConfig() { … } static std::set<std::string> RustKeywords() { … } // Encapsulate all logical field types in this enum. This allows us to write // field logic based on type switches, instead of branches on the properties // set on the Type. // TODO(rw): for backwards compatibility, we can't use a strict `enum class` // declaration here. could we use the `-Wswitch-enum` warning to // achieve the same effect? enum FullType { … }; // Convert a Type to a FullType (exhaustive). static FullType GetFullType(const Type &type) { … } static bool IsBitFlagsEnum(const EnumDef &enum_def) { … } // TableArgs make required non-scalars "Option<_>". // TODO(cneo): Rework how we do defaults and stuff. static bool IsOptionalToBuilder(const FieldDef &field) { … } } // namespace static bool GenerateRustModuleRootFile(const Parser &parser, const std::string &output_dir) { … } namespace rust { class RustGenerator : public BaseGenerator { … }; } // namespace rust static bool GenerateRust(const Parser &parser, const std::string &path, const std::string &file_name) { … } static std::string RustMakeRule(const Parser &parser, const std::string &path, const std::string &file_name) { … } namespace { class RustCodeGenerator : public CodeGenerator { … }; } // namespace std::unique_ptr<CodeGenerator> NewRustCodeGenerator() { … } } // namespace flatbuffers // TODO(rw): Generated code should import other generated files. // TODO(rw): Generated code should refer to namespaces in included files in a // way that makes them referrable. // TODO(rw): Generated code should indent according to nesting level. // TODO(rw): Generated code should generate endian-safe Debug impls. // TODO(rw): Generated code could use a Rust-only enum type to access unions, // instead of making the user use _type() to manually switch. // TODO(maxburke): There should be test schemas added that use language // keywords as fields of structs, tables, unions, enums, to make sure // that internal code generated references escaped names correctly. // TODO(maxburke): We should see if there is a more flexible way of resolving // module paths for use declarations. Right now if schemas refer to // other flatbuffer files, the include paths in emitted Rust bindings // are crate-relative which may undesirable.