option: rename to to_string_opt and to_string_view_opt

to_option_string -> to_string_opt
to_string_view   -> to_string_view_opt
This commit is contained in:
Dirk-Jan C. Binnema 2022-04-16 13:16:29 +03:00
parent 4f9c154d1a
commit 7c51bc68d4
9 changed files with 104 additions and 77 deletions

View File

@ -41,9 +41,6 @@ MessagePart::mime_object() const noexcept
return *mime_obj; return *mime_obj;
} }
Option<std::string> Option<std::string>
MessagePart::cooked_filename() const noexcept MessagePart::cooked_filename() const noexcept
{ {
@ -123,7 +120,7 @@ MessagePart::to_string() const noexcept
if (mime_object().is_part()) if (mime_object().is_part())
return MimePart{mime_object()}.to_string(); return MimePart{mime_object()}.to_string();
else else
return mime_object().object_to_string(); return mime_object().to_string_opt();
} }

View File

@ -89,8 +89,10 @@ Message::Message(const std::string& path, const std::string& mdir):
else else
priv_->mime_msg = std::move(msg.value()); priv_->mime_msg = std::move(msg.value());
priv_->doc.add(Field::Id::Path, auto xpath{to_string_opt_gchar(g_canonicalize_filename(path.c_str(), NULL))};
Mu::from_gchars(g_canonicalize_filename(path.c_str(), NULL))); if (xpath)
priv_->doc.add(Field::Id::Path, std::move(xpath.value()));
if (!mdir.empty()) if (!mdir.empty())
priv_->doc.add(Field::Id::Maildir, mdir); priv_->doc.add(Field::Id::Maildir, mdir);
priv_->doc.add(Field::Id::Size, static_cast<int64_t>(statbuf.st_size)); priv_->doc.add(Field::Id::Size, static_cast<int64_t>(statbuf.st_size));
@ -103,10 +105,10 @@ Message::Message(const std::string& text, const std::string& path,
const std::string& mdir): const std::string& mdir):
priv_{std::make_unique<Private>()} priv_{std::make_unique<Private>()}
{ {
if (!path.empty()) auto xpath{to_string_opt_gchar(g_canonicalize_filename(path.c_str(), NULL))};
priv_->doc.add(Field::Id::Path, if (xpath)
Mu::from_gchars( priv_->doc.add(Field::Id::Path, std::move(xpath.value()));
g_canonicalize_filename(path.c_str(), NULL)));
if (!mdir.empty()) if (!mdir.empty())
priv_->doc.add(Field::Id::Maildir, mdir); priv_->doc.add(Field::Id::Maildir, mdir);
@ -246,7 +248,7 @@ get_mailing_list(const MimeMessage& mime_msg)
g_free(dechdr); g_free(dechdr);
return from_gchars(std::move(res)); return to_string_opt_gchar(std::move(res));
} }
static bool /* heuristic */ static bool /* heuristic */

View File

@ -153,7 +153,7 @@ struct MimeContentType: public Object {
} }
Option<std::string> mime_type() const noexcept { Option<std::string> mime_type() const noexcept {
return to_option_string(g_mime_content_type_get_mime_type(self())); return to_string_opt(g_mime_content_type_get_mime_type(self()));
} }
bool is_type(const std::string& type, const std::string& subtype) const { bool is_type(const std::string& type, const std::string& subtype) const {
@ -193,7 +193,7 @@ struct MimeStream: public Object {
}; };
template<typename S, typename T> template<typename S, typename T>
constexpr Option<std::string_view> to_string_view(const S& seq, T t) { constexpr Option<std::string_view> to_string_view_opt(const S& seq, T t) {
auto&& it = seq_find_if(seq, [&](auto&& item){return item.first == t;}); auto&& it = seq_find_if(seq, [&](auto&& item){return item.first == t;});
if (it == seq.cend()) if (it == seq.cend())
return Nothing; return Nothing;
@ -283,27 +283,27 @@ struct MimeCertificate: public Object {
} }
Option<std::string> issuer_serial() const { Option<std::string> issuer_serial() const {
return to_option_string(g_mime_certificate_get_issuer_serial(self())); return to_string_opt(g_mime_certificate_get_issuer_serial(self()));
} }
Option<std::string> issuer_name() const { Option<std::string> issuer_name() const {
return to_option_string(g_mime_certificate_get_issuer_name(self())); return to_string_opt(g_mime_certificate_get_issuer_name(self()));
} }
Option<std::string> fingerprint() const { Option<std::string> fingerprint() const {
return to_option_string(g_mime_certificate_get_fingerprint(self())); return to_string_opt(g_mime_certificate_get_fingerprint(self()));
} }
Option<std::string> key_id() const { Option<std::string> key_id() const {
return to_option_string(g_mime_certificate_get_key_id(self())); return to_string_opt(g_mime_certificate_get_key_id(self()));
} }
Option<std::string> name() const { Option<std::string> name() const {
return to_option_string(g_mime_certificate_get_name(self())); return to_string_opt(g_mime_certificate_get_name(self()));
} }
Option<std::string> user_id() const { Option<std::string> user_id() const {
return to_option_string(g_mime_certificate_get_user_id(self())); return to_string_opt(g_mime_certificate_get_user_id(self()));
} }
Option<::time_t> created() const { Option<::time_t> created() const {
@ -341,8 +341,8 @@ AllPubkeyAlgos = {{
{ MimeCertificate::PubkeyAlgo::EdDsa, "elliptic-curve+dsa-2"} { MimeCertificate::PubkeyAlgo::EdDsa, "elliptic-curve+dsa-2"}
}}; }};
constexpr Option<std::string_view> to_string_view(MimeCertificate::PubkeyAlgo algo) { constexpr Option<std::string_view> to_string_view_opt(MimeCertificate::PubkeyAlgo algo) {
return to_string_view(AllPubkeyAlgos, algo); return to_string_view_opt(AllPubkeyAlgos, algo);
}; };
@ -365,8 +365,8 @@ AllDigestAlgos = {{
{ MimeCertificate::DigestAlgo::Crc32Rfc2440, "crc32-rfc2440"}, { MimeCertificate::DigestAlgo::Crc32Rfc2440, "crc32-rfc2440"},
}}; }};
constexpr Option<std::string_view> to_string_view(MimeCertificate::DigestAlgo algo) { constexpr Option<std::string_view> to_string_view_opt(MimeCertificate::DigestAlgo algo) {
return to_string_view(AllDigestAlgos, algo); return to_string_view_opt(AllDigestAlgos, algo);
}; };
constexpr std::array<std::pair<MimeCertificate::Trust, std::string_view>, 6> constexpr std::array<std::pair<MimeCertificate::Trust, std::string_view>, 6>
@ -379,8 +379,8 @@ AllTrusts = {{
{ MimeCertificate::Trust::TrustUltimate,"trust-ultimate" }, { MimeCertificate::Trust::TrustUltimate,"trust-ultimate" },
}}; }};
constexpr Option<std::string_view> to_string_view(MimeCertificate::Trust trust) { constexpr Option<std::string_view> to_string_view_opt(MimeCertificate::Trust trust) {
return to_string_view(AllTrusts, trust); return to_string_view_opt(AllTrusts, trust);
}; };
constexpr std::array<std::pair<MimeCertificate::Validity, std::string_view>, 6> constexpr std::array<std::pair<MimeCertificate::Validity, std::string_view>, 6>
@ -393,8 +393,8 @@ AllValidities = {{
{ MimeCertificate::Validity::Ultimate, "ultimate" }, { MimeCertificate::Validity::Ultimate, "ultimate" },
}}; }};
constexpr Option<std::string_view> to_string_view(MimeCertificate::Validity val) { constexpr Option<std::string_view> to_string_view_opt(MimeCertificate::Validity val) {
return to_string_view(AllValidities, val); return to_string_view_opt(AllValidities, val);
}; };
@ -520,7 +520,7 @@ struct MimeDecryptResult: public Object {
} }
Option<std::string> session_key() const noexcept { Option<std::string> session_key() const noexcept {
return to_option_string(g_mime_decrypt_result_get_session_key(self())); return to_string_opt(g_mime_decrypt_result_get_session_key(self()));
} }
@ -532,22 +532,22 @@ private:
constexpr std::array<std::pair<MimeDecryptResult::CipherAlgo, std::string_view>, 12> constexpr std::array<std::pair<MimeDecryptResult::CipherAlgo, std::string_view>, 12>
AllCipherAlgos= {{ AllCipherAlgos= {{
{ MimeDecryptResult::CipherAlgo::Default , "default"}, {MimeDecryptResult::CipherAlgo::Default, "default"},
{ MimeDecryptResult::CipherAlgo::Idea , "idea"}, {MimeDecryptResult::CipherAlgo::Idea, "idea"},
{ MimeDecryptResult::CipherAlgo::Des3 , "3des"}, {MimeDecryptResult::CipherAlgo::Des3, "3des"},
{ MimeDecryptResult::CipherAlgo::Cast5 , "cast5"}, {MimeDecryptResult::CipherAlgo::Cast5, "cast5"},
{ MimeDecryptResult::CipherAlgo::Blowfish , "blowfish"}, {MimeDecryptResult::CipherAlgo::Blowfish, "blowfish"},
{ MimeDecryptResult::CipherAlgo::Aes , "aes"}, {MimeDecryptResult::CipherAlgo::Aes, "aes"},
{ MimeDecryptResult::CipherAlgo::Aes192 , "aes192"}, {MimeDecryptResult::CipherAlgo::Aes192, "aes192"},
{ MimeDecryptResult::CipherAlgo::Aes256 , "aes256"}, {MimeDecryptResult::CipherAlgo::Aes256, "aes256"},
{ MimeDecryptResult::CipherAlgo::TwoFish , "twofish"}, {MimeDecryptResult::CipherAlgo::TwoFish, "twofish"},
{ MimeDecryptResult::CipherAlgo::Camellia128, "camellia128"}, {MimeDecryptResult::CipherAlgo::Camellia128, "camellia128"},
{ MimeDecryptResult::CipherAlgo::Camellia192, "camellia192"}, {MimeDecryptResult::CipherAlgo::Camellia192, "camellia192"},
{ MimeDecryptResult::CipherAlgo::Camellia256, "camellia256"}, {MimeDecryptResult::CipherAlgo::Camellia256, "camellia256"},
}}; }};
constexpr Option<std::string_view> to_string_view(MimeDecryptResult::CipherAlgo algo) { constexpr Option<std::string_view> to_string_view_opt(MimeDecryptResult::CipherAlgo algo) {
return to_string_view(AllCipherAlgos, algo); return to_string_view_opt(AllCipherAlgos, algo);
}; };
@ -581,13 +581,13 @@ struct MimeCryptoContext : public Object {
Option<std::string> encryption_protocol() { Option<std::string> encryption_protocol() {
return to_option_string(g_mime_crypto_context_get_encryption_protocol(self())); return to_string_opt(g_mime_crypto_context_get_encryption_protocol(self()));
} }
Option<std::string> signature_protocol() { Option<std::string> signature_protocol() {
return to_option_string(g_mime_crypto_context_get_signature_protocol(self())); return to_string_opt(g_mime_crypto_context_get_signature_protocol(self()));
} }
Option<std::string> key_exchange_protocol() { Option<std::string> key_exchange_protocol() {
return to_option_string(g_mime_crypto_context_get_key_exchange_protocol(self())); return to_string_opt(g_mime_crypto_context_get_key_exchange_protocol(self()));
} }
/** /**
@ -685,7 +685,7 @@ public:
} }
Option<std::string> content_type_parameter(const std::string& param) const noexcept { Option<std::string> content_type_parameter(const std::string& param) const noexcept {
return to_option_string( return to_string_opt(
g_mime_object_get_content_type_parameter(self(), param.c_str())); g_mime_object_get_content_type_parameter(self(), param.c_str()));
} }
@ -832,7 +832,7 @@ public:
* @return string or nullopt * @return string or nullopt
*/ */
Option<std::string> message_id() const noexcept { Option<std::string> message_id() const noexcept {
return to_option_string(g_mime_message_get_message_id(self())); return to_string_opt(g_mime_message_get_message_id(self()));
} }
/** /**
@ -841,7 +841,7 @@ public:
* @return string or nullopt * @return string or nullopt
*/ */
Option<std::string> subject() const noexcept { Option<std::string> subject() const noexcept {
return to_option_string(g_mime_message_get_subject(self())); return to_string_opt(g_mime_message_get_subject(self()));
} }
/** /**
@ -914,7 +914,7 @@ public:
* @return string or nullopt * @return string or nullopt
*/ */
Option<std::string> content_description() const noexcept { Option<std::string> content_description() const noexcept {
return to_option_string(g_mime_part_get_content_description(self())); return to_string_opt(g_mime_part_get_content_description(self()));
} }
/** /**
@ -924,7 +924,7 @@ public:
* @return string or nullopt * @return string or nullopt
*/ */
Option<std::string> content_id() const noexcept { Option<std::string> content_id() const noexcept {
return to_option_string(g_mime_part_get_content_id(self())); return to_string_opt(g_mime_part_get_content_id(self()));
} }
/** /**
@ -934,7 +934,7 @@ public:
* @return string or nullopt * @return string or nullopt
*/ */
Option<std::string> content_md5() const noexcept { Option<std::string> content_md5() const noexcept {
return to_option_string(g_mime_part_get_content_md5(self())); return to_string_opt(g_mime_part_get_content_md5(self()));
} }
@ -955,7 +955,7 @@ public:
* @return string or nullopt * @return string or nullopt
*/ */
Option<std::string> content_location() const noexcept { Option<std::string> content_location() const noexcept {
return to_option_string(g_mime_part_get_content_location(self())); return to_string_opt(g_mime_part_get_content_location(self()));
} }
/** /**
@ -965,7 +965,7 @@ public:
* @return string or nullopt * @return string or nullopt
*/ */
Option<std::string> filename() const noexcept { Option<std::string> filename() const noexcept {
return to_option_string(g_mime_part_get_filename(self())); return to_string_opt(g_mime_part_get_filename(self()));
} }
/** /**

View File

@ -58,6 +58,7 @@ libmu_utils_la_SOURCES= \
mu-logger.cc \ mu-logger.cc \
mu-logger.hh \ mu-logger.hh \
mu-option.hh \ mu-option.hh \
mu-option.cc \
mu-readline.cc \ mu-readline.cc \
mu-readline.hh \ mu-readline.hh \
mu-result.hh \ mu-result.hh \

View File

@ -23,6 +23,7 @@ lib_mu_utils=static_library('mu-utils', [
'mu-logger.cc', 'mu-logger.cc',
'mu-logger.hh', 'mu-logger.hh',
'mu-option.hh', 'mu-option.hh',
'mu-option.cc',
'mu-readline.cc', 'mu-readline.cc',
'mu-readline.hh', 'mu-readline.hh',
'mu-result.hh', 'mu-result.hh',

View File

@ -15,6 +15,7 @@
#define MU_OPTION__ #define MU_OPTION__
#include "optional.hpp" #include "optional.hpp"
#include <string>
namespace Mu { namespace Mu {
@ -29,5 +30,31 @@ Some(T&& t)
} }
constexpr auto Nothing = tl::nullopt; // 'None' is take already constexpr auto Nothing = tl::nullopt; // 'None' is take already
/**
* Maybe create a string from a const char pointer.
*
* @param str a char pointer or NULL
*
* @return option with either the string or nothing if str was NULL.
*/
Option<std::string>
static inline to_string_opt(const char* str) {
if (str)
return std::string{str};
else
return Nothing;
}
/**
* Like maybe_string that takes a const char*, but additionally,
* g_free() the string.
*
* @param str char pointer or NULL (consumed)
*
* @return option with either the string or nothing if str was NULL.
*/
Option<std::string> to_string_opt_gchar(char*&& str);
} // namespace Mu } // namespace Mu
#endif /*MU_OPTION__*/ #endif /*MU_OPTION__*/

View File

@ -43,6 +43,7 @@
#include "mu-util.h" #include "mu-util.h"
#include "mu-str.h" #include "mu-str.h"
#include "mu-error.hh" #include "mu-error.hh"
#include "mu-option.hh"
using namespace Mu; using namespace Mu;
@ -352,14 +353,12 @@ Mu::time_to_string(const std::string& frm, time_t t, bool utc)
return {}; return {};
} }
char* str = g_date_time_format(dt, frm.c_str()); /* always utf8 */ auto datestr{to_string_opt_gchar(g_date_time_format(dt, frm.c_str()))};
g_date_time_unref(dt); g_date_time_unref(dt);
if (!str) { if (!datestr)
g_warning("failed to format time with format '%s'", frm.c_str()); g_warning("failed to format time with format '%s'", frm.c_str());
return {};
}
return from_gchars(std::move(str)/*consumed*/); return datestr.value_or("");
} }
static std::string static std::string

View File

@ -174,22 +174,22 @@ std::string date_to_time_t_string(int64_t t);
std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G_GNUC_CONST; std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G_GNUC_CONST;
/** // /**
* Create a std::string by consuming a gchar* array; this takes ownership // * Create a std::string by consuming a gchar* array; this takes ownership
* of str which should no longer be used. // * of str which should no longer be used.
* // *
* @param str a gchar* or NULL (latter taken as "") // * @param str a gchar* or NULL (latter taken as "")
* // *
* @return a std::string // * @return a std::string
*/ // */
static inline std::string // static inline std::string
from_gchars(gchar*&& str) // from_gchars(gchar*&& str)
{ // {
std::string s{str ? str : ""}; // std::string s{str ? str : ""};
g_free(str); // g_free(str);
return s; // return s;
} // }
// https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member // https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member
template <auto fn> template <auto fn>

View File

@ -366,13 +366,13 @@ print_signature(const Mu::MimeSignature& sig, const MuConfig *opts)
const auto cert{sig.certificate()}; const auto cert{sig.certificate()};
key_val(col, "public-key algo", key_val(col, "public-key algo",
to_string_view(cert.pubkey_algo()).value_or("unknown")); to_string_view_opt(cert.pubkey_algo()).value_or("unknown"));
key_val(col, "digest algo", key_val(col, "digest algo",
to_string_view(cert.digest_algo()).value_or("unknown")); to_string_view_opt(cert.digest_algo()).value_or("unknown"));
key_val(col, "id-validity", key_val(col, "id-validity",
to_string_view(cert.id_validity()).value_or("unknown")); to_string_view_opt(cert.id_validity()).value_or("unknown"));
key_val(col, "trust", key_val(col, "trust",
to_string_view(cert.trust()).value_or("unknown")); to_string_view_opt(cert.trust()).value_or("unknown"));
key_val(col, "issuer-serial", cert.issuer_serial().value_or("unknown")); key_val(col, "issuer-serial", cert.issuer_serial().value_or("unknown"));
key_val(col, "issuer-name", cert.issuer_name().value_or("unknown")); key_val(col, "issuer-name", cert.issuer_name().value_or("unknown"));
key_val(col, "finger-print", cert.fingerprint().value_or("unknown")); key_val(col, "finger-print", cert.fingerprint().value_or("unknown"));