Use TRY_RESULT_ASSIGN.

GitOrigin-RevId: 99b60b9044fab9357a3440aed7da55f146677e6b
This commit is contained in:
levlam 2019-12-08 09:57:33 +03:00
parent a0d761f27e
commit 8c495d99e6
13 changed files with 30 additions and 56 deletions

View File

@ -847,16 +847,14 @@ class MessagesDbImpl : public MessagesDbSyncInterface {
left_cnt++; left_cnt++;
} }
TRY_RESULT(left_tmp, get_messages_inner(stmt.desc_stmt_, dialog_id, left_message_id, left_cnt)); TRY_RESULT_ASSIGN(left, get_messages_inner(stmt.desc_stmt_, dialog_id, left_message_id, left_cnt));
left = std::move(left_tmp);
if (right_cnt == 1 && !left.empty() && false /*get_message_id(left[0].as_slice()) == message_id*/) { if (right_cnt == 1 && !left.empty() && false /*get_message_id(left[0].as_slice()) == message_id*/) {
right_cnt = 0; right_cnt = 0;
} }
} }
if (right_cnt != 0) { if (right_cnt != 0) {
TRY_RESULT(right_tmp, get_messages_inner(stmt.asc_stmt_, dialog_id, right_message_id, right_cnt)); TRY_RESULT_ASSIGN(right, get_messages_inner(stmt.asc_stmt_, dialog_id, right_message_id, right_cnt));
right = std::move(right_tmp);
std::reverse(right.begin(), right.end()); std::reverse(right.begin(), right.end());
} }
if (left.empty()) { if (left.empty()) {

View File

@ -3210,8 +3210,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
if (mtpeer.type() != JsonValue::Type::Null) { if (mtpeer.type() != JsonValue::Type::Null) {
TRY_RESULT(ah, get_json_object_string_field(mtpeer.get_object(), "ah")); TRY_RESULT(ah, get_json_object_string_field(mtpeer.get_object(), "ah"));
if (!ah.empty()) { if (!ah.empty()) {
TRY_RESULT(sender_access_hash_safe, to_integer_safe<int64>(ah)); TRY_RESULT_ASSIGN(sender_access_hash, to_integer_safe<int64>(ah));
sender_access_hash = sender_access_hash_safe;
} }
TRY_RESULT(ph, get_json_object_field(mtpeer.get_object(), "ph", JsonValue::Type::Object)); TRY_RESULT(ph, get_json_object_field(mtpeer.get_object(), "ph", JsonValue::Type::Object));
if (ph.type() != JsonValue::Type::Null) { if (ph.type() != JsonValue::Type::Null) {

View File

@ -380,8 +380,8 @@ static Result<InlineKeyboardButton> get_inline_keyboard_button(tl_object_ptr<td_
switch (button_type_id) { switch (button_type_id) {
case td_api::inlineKeyboardButtonTypeUrl::ID: { case td_api::inlineKeyboardButtonTypeUrl::ID: {
current_button.type = InlineKeyboardButton::Type::Url; current_button.type = InlineKeyboardButton::Type::Url;
TRY_RESULT(url, check_url(static_cast<const td_api::inlineKeyboardButtonTypeUrl *>(button->type_.get())->url_)); TRY_RESULT_ASSIGN(current_button.data,
current_button.data = std::move(url); check_url(static_cast<const td_api::inlineKeyboardButtonTypeUrl *>(button->type_.get())->url_));
if (!clean_input_string(current_button.data)) { if (!clean_input_string(current_button.data)) {
return Status::Error(400, "Inline keyboard button url must be encoded in UTF-8"); return Status::Error(400, "Inline keyboard button url must be encoded in UTF-8");
} }
@ -420,8 +420,7 @@ static Result<InlineKeyboardButton> get_inline_keyboard_button(tl_object_ptr<td_
case td_api::inlineKeyboardButtonTypeLoginUrl::ID: { case td_api::inlineKeyboardButtonTypeLoginUrl::ID: {
current_button.type = InlineKeyboardButton::Type::UrlAuth; current_button.type = InlineKeyboardButton::Type::UrlAuth;
auto login_url = td_api::move_object_as<td_api::inlineKeyboardButtonTypeLoginUrl>(button->type_); auto login_url = td_api::move_object_as<td_api::inlineKeyboardButtonTypeLoginUrl>(button->type_);
TRY_RESULT(url, check_url(login_url->url_)); TRY_RESULT_ASSIGN(current_button.data, check_url(login_url->url_));
current_button.data = std::move(url);
current_button.forward_text = std::move(login_url->forward_text_); current_button.forward_text = std::move(login_url->forward_text_);
if (!clean_input_string(current_button.data)) { if (!clean_input_string(current_button.data)) {
return Status::Error(400, "Inline keyboard button login url must be encoded in UTF-8"); return Status::Error(400, "Inline keyboard button login url must be encoded in UTF-8");

View File

@ -892,19 +892,15 @@ static Result<SecureValue> get_identity_document(SecureValueType type, FileManag
} }
} }
TRY_RESULT(front_side, get_secure_file(file_manager, std::move(identity_document->front_side_))); TRY_RESULT_ASSIGN(res.front_side, get_secure_file(file_manager, std::move(identity_document->front_side_)));
res.front_side = std::move(front_side);
if (identity_document->reverse_side_ != nullptr) { if (identity_document->reverse_side_ != nullptr) {
TRY_RESULT(reverse_side, get_secure_file(file_manager, std::move(identity_document->reverse_side_))); TRY_RESULT_ASSIGN(res.reverse_side, get_secure_file(file_manager, std::move(identity_document->reverse_side_)));
res.reverse_side = std::move(reverse_side);
} }
if (identity_document->selfie_ != nullptr) { if (identity_document->selfie_ != nullptr) {
TRY_RESULT(selfie, get_secure_file(file_manager, std::move(identity_document->selfie_))); TRY_RESULT_ASSIGN(res.selfie, get_secure_file(file_manager, std::move(identity_document->selfie_)));
res.selfie = std::move(selfie);
} }
if (!identity_document->translation_.empty()) { if (!identity_document->translation_.empty()) {
TRY_RESULT(translations, get_secure_files(file_manager, std::move(identity_document->translation_))); TRY_RESULT_ASSIGN(res.translations, get_secure_files(file_manager, std::move(identity_document->translation_)));
res.translations = std::move(translations);
} }
return res; return res;
} }
@ -962,11 +958,9 @@ static Result<SecureValue> get_personal_document(
if (personal_document->files_.empty()) { if (personal_document->files_.empty()) {
return Status::Error(400, "Document's files are required"); return Status::Error(400, "Document's files are required");
} }
TRY_RESULT(files, get_secure_files(file_manager, std::move(personal_document->files_))); TRY_RESULT_ASSIGN(res.files, get_secure_files(file_manager, std::move(personal_document->files_)));
res.files = std::move(files);
if (!personal_document->translation_.empty()) { if (!personal_document->translation_.empty()) {
TRY_RESULT(translations, get_secure_files(file_manager, std::move(personal_document->translation_))); TRY_RESULT_ASSIGN(res.translations, get_secure_files(file_manager, std::move(personal_document->translation_)));
res.translations = std::move(translations);
} }
return res; return res;
} }
@ -1002,8 +996,7 @@ Result<SecureValue> get_secure_value(FileManager *file_manager,
case td_api::inputPassportElementPersonalDetails::ID: { case td_api::inputPassportElementPersonalDetails::ID: {
auto input = td_api::move_object_as<td_api::inputPassportElementPersonalDetails>(input_passport_element); auto input = td_api::move_object_as<td_api::inputPassportElementPersonalDetails>(input_passport_element);
res.type = SecureValueType::PersonalDetails; res.type = SecureValueType::PersonalDetails;
TRY_RESULT(personal_details, get_personal_details(std::move(input->personal_details_))); TRY_RESULT_ASSIGN(res.data, get_personal_details(std::move(input->personal_details_)));
res.data = std::move(personal_details);
break; break;
} }
case td_api::inputPassportElementPassport::ID: { case td_api::inputPassportElementPassport::ID: {
@ -1455,7 +1448,7 @@ static auto credentials_as_jsonable(const std::vector<SecureValueCredentials> &c
})); }));
} }
})); }));
o(rename_payload_to_nonce ? "nonce" : "payload", nonce); o(rename_payload_to_nonce ? Slice("nonce") : Slice("payload"), nonce);
}); });
} }

View File

@ -135,8 +135,7 @@ Status FileDownloader::on_ok(int64 size) {
if (only_check_) { if (only_check_) {
path = path_; path = path_;
} else { } else {
TRY_RESULT(perm_path, create_from_temp(path_, dir, name_)); TRY_RESULT_ASSIGN(path, create_from_temp(path_, dir, name_));
path = std::move(perm_path);
} }
callback_->on_ok(FullLocalFileLocation(remote_.file_type_, std::move(path), 0), size, !only_check_); callback_->on_ok(FullLocalFileLocation(remote_.file_type_, std::move(path), 0), size, !only_check_);
return Status::OK(); return Status::OK();
@ -492,11 +491,9 @@ void FileDownloader::try_release_fd() {
Status FileDownloader::acquire_fd() { Status FileDownloader::acquire_fd() {
if (fd_.empty()) { if (fd_.empty()) {
if (path_.empty()) { if (path_.empty()) {
TRY_RESULT(file_path, open_temp_file(remote_.file_type_)); TRY_RESULT_ASSIGN(std::tie(fd_, path_), open_temp_file(remote_.file_type_));
std::tie(fd_, path_) = std::move(file_path);
} else { } else {
TRY_RESULT(fd, FileFd::open(path_, (only_check_ ? 0 : FileFd::Write) | FileFd::Read)); TRY_RESULT_ASSIGN(fd_, FileFd::open(path_, (only_check_ ? 0 : FileFd::Write) | FileFd::Read));
fd_ = std::move(fd);
} }
} }
return Status::OK(); return Status::OK();

View File

@ -167,8 +167,7 @@ Result<FileLoader::PrefixInfo> FileUploader::on_update_local_location(const Loca
} }
if (local_is_ready) { if (local_is_ready) {
CHECK(!fd_.empty()); CHECK(!fd_.empty());
TRY_RESULT(local_file_size, fd_.get_size()); TRY_RESULT_ASSIGN(local_size, fd_.get_size());
local_size = local_file_size;
LOG(INFO) << "Set file local_size to " << local_size; LOG(INFO) << "Set file local_size to " << local_size;
if (local_size == 0) { if (local_size == 0) {
return Status::Error("Can't upload empty file"); return Status::Error("Can't upload empty file");
@ -338,8 +337,7 @@ void FileUploader::try_release_fd() {
Status FileUploader::acquire_fd() { Status FileUploader::acquire_fd() {
if (fd_.empty()) { if (fd_.empty()) {
TRY_RESULT(fd, FileFd::open(fd_path_, FileFd::Read)); TRY_RESULT_ASSIGN(fd_, FileFd::open(fd_path_, FileFd::Read));
fd_ = std::move(fd);
} }
return Status::OK(); return Status::OK();
} }

View File

@ -685,8 +685,7 @@ Result<SocketFd> ConnectionCreator::find_connection(const Proxy &proxy, const IP
TRY_RESULT(info, dc_options_set_.find_connection( TRY_RESULT(info, dc_options_set_.find_connection(
dc_id, allow_media_only, proxy.use_proxy() && proxy.use_socks5_proxy(), prefer_ipv6, only_http)); dc_id, allow_media_only, proxy.use_proxy() && proxy.use_socks5_proxy(), prefer_ipv6, only_http));
extra.stat = info.stat; extra.stat = info.stat;
TRY_RESULT(transport_type, get_transport_type(proxy, info)); TRY_RESULT_ASSIGN(extra.transport_type, get_transport_type(proxy, info));
extra.transport_type = std::move(transport_type);
extra.debug_str = PSTRING() << " to " << (info.option->is_media_only() ? "MEDIA " : "") << dc_id extra.debug_str = PSTRING() << " to " << (info.option->is_media_only() ? "MEDIA " : "") << dc_id
<< (info.use_http ? " over HTTP" : ""); << (info.use_http ? " over HTTP" : "");

View File

@ -81,8 +81,7 @@ inline Status from_json(int32 &to, JsonValue &from) {
return Status::Error(PSLICE() << "Expected Number, got " << from.type()); return Status::Error(PSLICE() << "Expected Number, got " << from.type());
} }
Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number(); Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number();
TRY_RESULT(res, to_integer_safe<int32>(number)); TRY_RESULT_ASSIGN(to, to_integer_safe<int32>(number));
to = res;
return Status::OK(); return Status::OK();
} }
@ -105,8 +104,7 @@ inline Status from_json(int64 &to, JsonValue &from) {
return Status::Error(PSLICE() << "Expected String or Number, got " << from.type()); return Status::Error(PSLICE() << "Expected String or Number, got " << from.type());
} }
Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number(); Slice number = from.type() == JsonValue::Type::String ? from.get_string() : from.get_number();
TRY_RESULT(res, to_integer_safe<int64>(number)); TRY_RESULT_ASSIGN(to, to_integer_safe<int64>(number));
to = res;
return Status::OK(); return Status::OK();
} }
@ -130,8 +128,7 @@ inline Status from_json_bytes(string &to, JsonValue &from) {
if (from.type() != JsonValue::Type::String) { if (from.type() != JsonValue::Type::String) {
return Status::Error(PSLICE() << "Expected String, got " << from.type()); return Status::Error(PSLICE() << "Expected String, got " << from.type());
} }
TRY_RESULT(decoded, base64_decode(from.get_string())); TRY_RESULT_ASSIGN(to, base64_decode(from.get_string()));
to = std::move(decoded);
return Status::OK(); return Status::OK();
} }
@ -180,8 +177,7 @@ std::enable_if_t<!std::is_constructible<T>::value, Status> from_json(tl_object_p
if (constructor_value.type() == JsonValue::Type::Number) { if (constructor_value.type() == JsonValue::Type::Number) {
constructor = to_integer<int32>(constructor_value.get_number()); constructor = to_integer<int32>(constructor_value.get_number());
} else if (constructor_value.type() == JsonValue::Type::String) { } else if (constructor_value.type() == JsonValue::Type::String) {
TRY_RESULT(t_constructor, tl_constructor_from_string(to.get(), constructor_value.get_string().str())); TRY_RESULT_ASSIGN(constructor, tl_constructor_from_string(to.get(), constructor_value.get_string().str()));
constructor = t_constructor;
} else { } else {
return Status::Error(PSLICE() << "Expected String or Integer, got " << constructor_value.type()); return Status::Error(PSLICE() << "Expected String or Integer, got " << constructor_value.type());
} }

View File

@ -38,8 +38,7 @@ Wget::Wget(Promise<unique_ptr<HttpQuery>> promise, string url, std::vector<std::
Status Wget::try_init() { Status Wget::try_init() {
TRY_RESULT(url, parse_url(input_url_)); TRY_RESULT(url, parse_url(input_url_));
TRY_RESULT(ascii_host, idn_to_ascii(url.host_)); TRY_RESULT_ASSIGN(url.host_, idn_to_ascii(url.host_));
url.host_ = std::move(ascii_host);
HttpHeaderCreator hc; HttpHeaderCreator hc;
if (content_.empty()) { if (content_.empty()) {

View File

@ -40,8 +40,7 @@ Status FileLog::init(string path, int64 rotate_threshold, bool redirect_stderr)
} else { } else {
path_ = r_path.move_as_ok(); path_ = r_path.move_as_ok();
} }
TRY_RESULT(size, fd_.get_size()); TRY_RESULT_ASSIGN(size_, fd_.get_size());
size_ = size;
rotate_threshold_ = rotate_threshold; rotate_threshold_ = rotate_threshold;
redirect_stderr_ = redirect_stderr; redirect_stderr_ = redirect_stderr;
return Status::OK(); return Status::OK();

View File

@ -41,8 +41,7 @@ template <class T>
Result<T> read_file_impl(CSlice path, int64 size, int64 offset) { Result<T> read_file_impl(CSlice path, int64 size, int64 offset) {
TRY_RESULT(from_file, FileFd::open(path, FileFd::Read)); TRY_RESULT(from_file, FileFd::open(path, FileFd::Read));
if (size == -1) { if (size == -1) {
TRY_RESULT(file_size, from_file.get_size()); TRY_RESULT_ASSIGN(size, from_file.get_size());
size = file_size;
} }
if (size < 0) { if (size < 0) {
return Status::Error("Failed to read file: invalid size"); return Status::Error("Failed to read file: invalid size");

View File

@ -386,7 +386,7 @@ Status IPAddress::init_host_port(CSlice host, CSlice port, bool prefer_ipv6) {
} }
#endif #endif
TRY_RESULT(ascii_host, idn_to_ascii(host)); TRY_RESULT(ascii_host, idn_to_ascii(host));
host = ascii_host; host = ascii_host; // assign string to CSlice
// some getaddrinfo implementations use inet_pton instead of inet_aton and support only decimal-dotted IPv4 form, // some getaddrinfo implementations use inet_pton instead of inet_aton and support only decimal-dotted IPv4 form,
// and so doesn't recognize 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001 as valid IPv4 addresses // and so doesn't recognize 0x12.0x34.0x56.0x78, or 0x12345678, or 0x7f.001 as valid IPv4 addresses

View File

@ -56,8 +56,7 @@ Status set_temporary_dir(CSlice dir) {
input_dir += TD_DIR_SLASH; input_dir += TD_DIR_SLASH;
} }
TRY_STATUS(mkpath(input_dir, 0750)); TRY_STATUS(mkpath(input_dir, 0750));
TRY_RESULT(real_dir, realpath(input_dir)); TRY_RESULT_ASSIGN(temporary_dir, realpath(input_dir));
temporary_dir = std::move(real_dir);
return Status::OK(); return Status::OK();
} }
@ -419,8 +418,7 @@ Result<string> realpath(CSlice slice, bool ignore_access_denied) {
return OS_ERROR(PSLICE() << "GetFullPathNameW failed for \"" << slice << '"'); return OS_ERROR(PSLICE() << "GetFullPathNameW failed for \"" << slice << '"');
} }
} else { } else {
TRY_RESULT(t_res, from_wstring(buf)); TRY_RESULT_ASSIGN(res, from_wstring(buf));
res = std::move(t_res);
} }
if (res.empty()) { if (res.empty()) {
return Status::Error("Empty path"); return Status::Error("Empty path");