1 // Copyright 2018 The Fuchsia 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 
5 #include "fidl/attributes.h"
6 
7 namespace fidl {
8 
Insert(std::unique_ptr<raw::Attribute> attribute)9 bool AttributesBuilder::Insert(std::unique_ptr<raw::Attribute> attribute) {
10     auto attribute_name = attribute->name;
11     auto attribute_location = attribute->location();
12     auto result = InsertHelper(std::move(attribute));
13     switch (result.kind) {
14     case InsertResult::Kind::kDuplicate: {
15         std::string message("duplicate attribute with name '");
16         message.append(attribute_name);
17         message.append("'");
18         error_reporter_->ReportError(attribute_location, message);
19         return false;
20     }
21     case InsertResult::Kind::kOk:
22         return true;
23     } // switch
24 }
25 
Done()26 std::vector<std::unique_ptr<raw::Attribute>> AttributesBuilder::Done() {
27     return std::move(attributes_);
28 }
29 
InsertHelper(std::unique_ptr<raw::Attribute> attribute)30 AttributesBuilder::InsertResult AttributesBuilder::InsertHelper(std::unique_ptr<raw::Attribute> attribute) {
31     if (!names_.emplace(attribute->name).second) {
32         return InsertResult(InsertResult::kDuplicate, "");
33     }
34     auto attribute_name = attribute->name;
35     auto attribute_value = attribute->value;
36     attributes_.push_back(std::move(attribute));
37     return InsertResult(InsertResult::kOk, "");
38 }
39 
40 } // namespace fidl
41