diff options
Diffstat (limited to 'td/telegram/LabeledPricePart.cpp')
| -rw-r--r-- | td/telegram/LabeledPricePart.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/td/telegram/LabeledPricePart.cpp b/td/telegram/LabeledPricePart.cpp index 375146272..ff2776e1e 100644 --- a/td/telegram/LabeledPricePart.cpp +++ b/td/telegram/LabeledPricePart.cpp @@ -6,6 +6,8 @@ // #include "td/telegram/LabeledPricePart.h" +#include "td/telegram/misc.h" + #include "td/utils/algorithm.h" namespace td { @@ -19,6 +21,39 @@ vector<telegram_api::object_ptr<telegram_api::labeledPrice>> LabeledPricePart::g return transform(parts, [](const LabeledPricePart &price) { return price.get_input_labeled_price(); }); } +Result<vector<LabeledPricePart>> LabeledPricePart::get_labeled_price_parts( + vector<td_api::object_ptr<td_api::labeledPricePart>> &&parts, int64 *result_total_amount) { + if (parts.empty()) { + return Status::Error(400, "There must be at least one price part"); + } + vector<LabeledPricePart> result; + result.reserve(parts.size()); + int64 total_amount = 0; + for (auto &part : parts) { + if (part == nullptr) { + return Status::Error(400, "Price part must be non-empty"); + } + if (!clean_input_string(part->label_)) { + return Status::Error(400, "Price part label must be encoded in UTF-8"); + } + if (!check_currency_amount(part->amount_)) { + return Status::Error(400, "Too big amount of the currency specified"); + } + result.emplace_back(std::move(part->label_), part->amount_); + total_amount += part->amount_; + } + if (total_amount <= 0) { + return Status::Error(400, "Total price must be positive"); + } + if (!check_currency_amount(total_amount)) { + return Status::Error(400, "Total price is too big"); + } + if (result_total_amount != nullptr) { + *result_total_amount = total_amount; + } + return std::move(result); +} + StringBuilder &operator<<(StringBuilder &string_builder, const LabeledPricePart &labeled_price_part) { return string_builder << '[' << labeled_price_part.label << ": " << labeled_price_part.amount << ']'; } |
