| 1 | #include "cpr/accept_encoding.h" |
|---|---|
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <cassert> |
| 5 | #include <initializer_list> |
| 6 | #include <iterator> |
| 7 | #include <numeric> |
| 8 | #include <stdexcept> |
| 9 | |
| 10 | namespace cpr { |
| 11 | |
| 12 | AcceptEncoding::AcceptEncoding(const std::initializer_list<AcceptEncodingMethods>& methods) { |
| 13 | methods_.clear(); |
| 14 | std::transform(first: methods.begin(), last: methods.end(), result: std::inserter(x&: methods_, i: methods_.begin()), unary_op: [&](cpr::AcceptEncodingMethods method) { return cpr::AcceptEncodingMethodsStringMap.at(k: method); }); |
| 15 | } |
| 16 | |
| 17 | AcceptEncoding::AcceptEncoding(const std::initializer_list<std::string>& string_methods) : methods_{string_methods} {} |
| 18 | |
| 19 | bool AcceptEncoding::empty() const noexcept { |
| 20 | return methods_.empty(); |
| 21 | } |
| 22 | |
| 23 | const std::string AcceptEncoding::getString() const { |
| 24 | return std::accumulate(first: std::next(x: methods_.begin()), last: methods_.end(), init: *methods_.begin(), binary_op: [](std::string a, std::string b) { return std::move(a) + ", "+ std::move(b); }); |
| 25 | } |
| 26 | |
| 27 | [[nodiscard]] bool AcceptEncoding::disabled() const { |
| 28 | if (methods_.find(x: cpr::AcceptEncodingMethodsStringMap.at(k: AcceptEncodingMethods::disabled)) != methods_.end()) { |
| 29 | if (methods_.size() != 1) { |
| 30 | throw std::invalid_argument("AcceptEncoding does not accept any other values if 'disabled' is present. You set the following encodings: "+ getString()); |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | } // namespace cpr |
| 38 |