1 // Copyright 2018 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "amber/result.h" 16 17 #include <sstream> 18 19 namespace amber { 20 Result(const std::string & err)21Result::Result(const std::string& err) { 22 errors_.emplace_back(err); 23 } 24 operator +=(const Result & res)25Result& Result::operator+=(const Result& res) { 26 errors_.insert(std::end(errors_), std::begin(res.errors_), 27 std::end(res.errors_)); 28 return *this; 29 } 30 operator +=(const std::string & err)31Result& Result::operator+=(const std::string& err) { 32 errors_.emplace_back(err); 33 return *this; 34 } 35 Error() const36std::string Result::Error() const { 37 static const char* kNoErrorMsg = "<no error message given>"; 38 switch (errors_.size()) { 39 case 0: 40 return ""; 41 case 1: 42 return errors_[0].size() > 0 ? errors_[0] : kNoErrorMsg; 43 default: { 44 std::stringstream ss; 45 ss << errors_.size() << " errors:"; 46 for (size_t i = 0; i < errors_.size(); i++) { 47 auto& err = errors_[i]; 48 ss << "\n"; 49 ss << " (" << (i + 1) << ") "; 50 ss << (err.size() > 0 ? err : kNoErrorMsg); 51 } 52 return ss.str(); 53 } 54 } 55 } 56 57 } // namespace amber 58