Marked some functions noexcept.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tastytea 2020-01-12 15:27:11 +01:00
parent 7fc19639b1
commit 79c5087ca5
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 79 additions and 49 deletions

View File

@ -61,7 +61,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
inline string_view get_hostname() const inline string_view get_hostname() const noexcept
{ {
return _hostname; return _hostname;
} }
@ -74,7 +74,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
inline string_view get_baseuri() const inline string_view get_baseuri() const noexcept
{ {
return _baseuri; return _baseuri;
} }
@ -85,7 +85,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
inline string_view get_access_token() const inline string_view get_access_token() const noexcept
{ {
return _access_token; return _access_token;
} }
@ -115,7 +115,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
uint64_t get_max_chars(); uint64_t get_max_chars() noexcept;
/*! @copydoc CURLWrapper::set_proxy(string_view) /*! @copydoc CURLWrapper::set_proxy(string_view)
* *
@ -136,7 +136,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
string_view get_proxy() const string_view get_proxy() const noexcept
{ {
return _proxy; return _proxy;
} }
@ -164,7 +164,7 @@ public:
* *
* @since 0.3.0 * @since 0.3.0
*/ */
vector<string> get_post_formats(); vector<string> get_post_formats() noexcept;
/*! /*!
* @brief Set path to Certificate Authority (CA) bundle. * @brief Set path to Certificate Authority (CA) bundle.
@ -187,7 +187,7 @@ public:
* *
* @since 0.2.1 * @since 0.2.1
*/ */
string_view get_cainfo() string_view get_cainfo() const noexcept
{ {
return _cainfo; return _cainfo;
} }

View File

@ -35,46 +35,48 @@ Instance::Instance(const string_view hostname, const string_view access_token)
, _max_chars{0} , _max_chars{0}
{} {}
uint64_t Instance::get_max_chars() uint64_t Instance::get_max_chars() noexcept
{ {
constexpr uint64_t default_max_chars{500}; constexpr uint64_t default_max_chars{500};
if (_max_chars == 0) if (_max_chars != 0)
{ {
try return _max_chars;
{ }
debuglog << "Querying " << _hostname << " for max_toot_chars…\n";
const auto answer{make_request(http_method::GET,
_baseuri + "/api/v1/instance", {})};
if (!answer)
{
debuglog << "Could not get instance info.\n";
return default_max_chars;
}
_max_chars = [&answer] try
{ {
auto &body{answer.body}; debuglog << "Querying " << _hostname << " for max_toot_chars…\n";
size_t pos_start{body.find("max_toot_chars")}; const auto answer{make_request(http_method::GET,
if (pos_start == string::npos) _baseuri + "/api/v1/instance", {})};
{ if (!answer)
debuglog << "max_toot_chars not found.\n";
return default_max_chars;
}
pos_start = body.find(':', pos_start) + 1;
const size_t pos_end{body.find(',', pos_start)};
const auto max_toot_chars{body.substr(pos_start,
pos_end - pos_start)};
return static_cast<uint64_t>(stoull(max_toot_chars));
}();
debuglog << "Set _max_chars to: " << _max_chars << '\n';
}
catch (const exception &e)
{ {
debuglog << "Unexpected exception: " << e.what() << '\n'; debuglog << "Could not get instance info.\n";
return default_max_chars; return default_max_chars;
} }
_max_chars = [&answer]
{
auto &body{answer.body};
size_t pos_start{body.find("max_toot_chars")};
if (pos_start == string::npos)
{
debuglog << "max_toot_chars not found.\n";
return default_max_chars;
}
pos_start = body.find(':', pos_start) + 1;
const size_t pos_end{body.find(',', pos_start)};
const auto max_toot_chars{body.substr(pos_start,
pos_end - pos_start)};
return static_cast<uint64_t>(stoull(max_toot_chars));
}();
debuglog << "Set _max_chars to: " << _max_chars << '\n';
}
catch (const exception &e)
{
debuglog << "Unexpected exception: " << e.what() << '\n';
return default_max_chars;
} }
return _max_chars; return _max_chars;
@ -107,7 +109,7 @@ answer_type Instance::get_nodeinfo()
return make_request(http_method::GET, hrefs.back(), {}); return make_request(http_method::GET, hrefs.back(), {});
} }
vector<string> Instance::get_post_formats() vector<string> Instance::get_post_formats() noexcept
{ {
constexpr auto default_value{"text/plain"}; constexpr auto default_value{"text/plain"};
@ -116,18 +118,46 @@ vector<string> Instance::get_post_formats()
return _post_formats; return _post_formats;
} }
debuglog << "Querying " << _hostname << " for postFormats…\n"; try
const auto answer{get_nodeinfo()};
if (!answer)
{ {
debuglog << "Couldn't get NodeInfo.\n"; debuglog << "Querying " << _hostname << " for postFormats…\n";
_post_formats = {default_value}; const auto answer{get_nodeinfo()};
return _post_formats; if (!answer)
{
debuglog << "Couldn't get NodeInfo.\n";
_post_formats = {default_value};
return _post_formats;
}
constexpr string_view searchstring{R"("postFormats":[)"};
auto pos{answer.body.find(searchstring)};
if (pos == string::npos)
{
debuglog << "Couldn't find metadata.postFormats.\n";
_post_formats = {default_value};
return _post_formats;
}
pos += searchstring.size();
auto endpos{answer.body.find("],", pos)};
string formats{answer.body.substr(pos, endpos - pos)};
debuglog << "Extracted postFormats: " << formats << '\n';
while ((pos = formats.find('"', 1)) != string::npos)
{
_post_formats.push_back(formats.substr(1, pos - 1));
formats.erase(0, pos + 2); // 2 is the length of: ",
debuglog << "Found postFormat: " << _post_formats.back() << '\n';
}
}
catch (const std::exception &e)
{
debuglog << "Unexpected exception: " << e.what() << '\n';
return {default_value};
} }
constexpr string_view searchstring{R"("postFormats":[)"}; return _post_formats;
auto pos{answer.body.find(searchstring)}; }
if (pos == string::npos)
{ {
debuglog << "Couldn't find metadata.postFormats.\n"; debuglog << "Couldn't find metadata.postFormats.\n";
_post_formats = {default_value}; _post_formats = {default_value};