diff --git a/guile/mu-guile-message.cc b/guile/mu-guile-message.cc index 640d643b..3918b2b0 100644 --- a/guile/mu-guile-message.cc +++ b/guile/mu-guile-message.cc @@ -213,7 +213,7 @@ SCM_DEFINE(get_field, #undef FUNC_NAME static SCM -contacts_to_list(MuMsg *msg, std::optional field_id) +contacts_to_list(MuMsg *msg, Option field_id) { SCM list{SCM_EOL}; @@ -252,7 +252,7 @@ SCM_DEFINE(get_contacts, if (CONTACT_TYPE == SCM_BOOL_F) return SCM_UNSPECIFIED; /* nothing to do */ - std::optional field_id; + Option field_id; if (CONTACT_TYPE == SCM_BOOL_T) field_id = {}; /* get all */ else { diff --git a/lib/message/mu-flags.hh b/lib/message/mu-flags.hh index f424b5a1..651ede73 100644 --- a/lib/message/mu-flags.hh +++ b/lib/message/mu-flags.hh @@ -24,7 +24,8 @@ #include #include #include -#include "utils/mu-utils.hh" +#include +#include namespace Mu { @@ -146,16 +147,16 @@ constexpr void flag_infos_for_each(Func&& func) * * @param flag a singular flag * - * @return the MessageFlagInfo, or std::nullopt in case of error. + * @return the MessageFlagInfo, or Nothing in case of error. */ -constexpr const std::optional +constexpr const Option flag_info(Flags flag) { constexpr auto upper = static_cast(Flags::_final_); const auto val = static_cast(flag); if (__builtin_popcount(val) != 1 || val >= upper) - return std::nullopt; + return Nothing; return AllMessageFlagInfos[static_cast(__builtin_ctz(val))]; } @@ -167,14 +168,14 @@ flag_info(Flags flag) * * @return the MessageFlagInfo */ -constexpr const std::optional +constexpr const Option flag_info(char shortcut) { for (auto&& info : AllMessageFlagInfos) if (info.shortcut == shortcut) return info; - return std::nullopt; + return Nothing; } /** @@ -184,14 +185,14 @@ flag_info(char shortcut) * * @return the MessageFlagInfo */ -constexpr const std::optional +constexpr const Option flag_info(std::string_view name) { for (auto&& info : AllMessageFlagInfos) if (info.name == name) return info; - return std::nullopt; + return Nothing; } /** @@ -209,7 +210,7 @@ flag_info(std::string_view name) * * @return the (OR'ed) flags or Flags::None */ -constexpr std::optional +constexpr Option flags_from_absolute_expr(std::string_view expr, bool ignore_invalid = false) { Flags flags{Flags::None}; @@ -217,7 +218,7 @@ flags_from_absolute_expr(std::string_view expr, bool ignore_invalid = false) for (auto&& kar : expr) { if (const auto& info{flag_info(kar)}; !info) { if (!ignore_invalid) - return std::nullopt; + return Nothing; } else flags |= info->flag; } @@ -242,24 +243,24 @@ flags_from_absolute_expr(std::string_view expr, bool ignore_invalid = false) * * @return new flags, or nullopt in case of error */ -constexpr std::optional +constexpr Option flags_from_delta_expr(std::string_view expr, Flags flags, - bool ignore_invalid = false) + bool ignore_invalid = false) { if (expr.size() % 2 != 0) - return std::nullopt; + return Nothing; for (auto u = 0U; u != expr.size(); u += 2) { if (const auto& info{flag_info(expr[u + 1])}; !info) { if (!ignore_invalid) - return std::nullopt; + return Nothing; } else { switch (expr[u]) { case '+': flags |= info->flag; break; case '-': flags &= ~info->flag; break; default: if (!ignore_invalid) - return std::nullopt; + return Nothing; break; } } @@ -274,14 +275,14 @@ flags_from_delta_expr(std::string_view expr, Flags flags, * @param expr a flag expression, either 'delta' or 'absolute' * @param flags optional: existing flags or none. Required for delta. * - * @return either messages flags or std::nullopt in case of error. + * @return either messages flags or Nothing in case of error. */ -constexpr std::optional +constexpr Option flags_from_expr(std::string_view expr, - std::optional flags = std::nullopt) + Option flags = Nothing) { if (expr.empty()) - return std::nullopt; + return Nothing; if (expr[0] == '+' || expr[0] == '-') return flags_from_delta_expr( diff --git a/lib/mu-msg.cc b/lib/mu-msg.cc index 41f80746..1ce4d896 100644 --- a/lib/mu-msg.cc +++ b/lib/mu-msg.cc @@ -636,7 +636,7 @@ get_all_contacts(MuMsg *self) } Mu::Contacts -Mu::mu_msg_get_contacts(MuMsg *self, std::optional field_id) +Mu::mu_msg_get_contacts(MuMsg *self, Option field_id) { typedef const char*(*AddressFunc)(MuMsg*); using AddressInfo = std::pair; diff --git a/lib/mu-msg.hh b/lib/mu-msg.hh index 7acd9917..f0962106 100644 --- a/lib/mu-msg.hh +++ b/lib/mu-msg.hh @@ -446,7 +446,7 @@ bool mu_msg_move_to_maildir(MuMsg* msg, * @return a sequence */ Mu::Contacts mu_msg_get_contacts (MuMsg *self, - std::optional field_id={}); + Option field_id={}); /** * create a 'display contact' from an email header To/Cc/Bcc/From-type address * ie., turn diff --git a/lib/mu-query.cc b/lib/mu-query.cc index d968d820..a6218c01 100644 --- a/lib/mu-query.cc +++ b/lib/mu-query.cc @@ -42,23 +42,23 @@ struct Query::Private { // bool calculate_threads (Xapian::Enquire& enq, size maxnum); Xapian::Enquire make_enquire(const std::string& expr, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags) const; Xapian::Enquire make_related_enquire(const StringSet& thread_ids, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags) const; Option run_threaded(QueryResults&& qres, Xapian::Enquire& enq, QueryFlags qflags, size_t max_size) const; Option run_singular(const std::string& expr, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags, size_t maxnum) const; Option run_related(const std::string& expr, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags, size_t maxnum) const; Option run(const std::string& expr, - std::optional sortfield_id, QueryFlags qflags, + Option sortfield_id, QueryFlags qflags, size_t maxnum) const; size_t store_size() const { return store_.database().get_doccount(); } @@ -84,7 +84,7 @@ sort_enquire(Xapian::Enquire& enq, Field::Id sortfield_id, QueryFlags qflags) Xapian::Enquire Query::Private::make_enquire(const std::string& expr, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags) const { Xapian::Enquire enq{store_.database()}; @@ -108,7 +108,7 @@ Query::Private::make_enquire(const std::string& expr, Xapian::Enquire Query::Private::make_related_enquire(const StringSet& thread_ids, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags) const { Xapian::Enquire enq{store_.database()}; @@ -156,7 +156,7 @@ Query::Private::run_threaded(QueryResults&& qres, Xapian::Enquire& enq, QueryFla Option Query::Private::run_singular(const std::string& expr, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags, size_t maxnum) const { // i.e. a query _without_ related messages, but still possibly @@ -197,7 +197,7 @@ opt_string(const Xapian::Document& doc, Field::Id id) noexcept Option Query::Private::run_related(const std::string& expr, - std::optional sortfield_id, + Option sortfield_id, QueryFlags qflags, size_t maxnum) const { // i.e. a query _with_ related messages and possibly with threading. @@ -231,7 +231,7 @@ Query::Private::run_related(const std::string& expr, // is unlimited and the sorting happens during threading. auto r_enq = std::invoke([&]{ if (threading) - return make_related_enquire(minfo.thread_ids, std::nullopt, qflags); + return make_related_enquire(minfo.thread_ids, Nothing, qflags); else return make_related_enquire(minfo.thread_ids, sortfield_id, qflags); }); @@ -244,7 +244,7 @@ Query::Private::run_related(const std::string& expr, Option Query::Private::run(const std::string& expr, - std::optional sortfield_id, QueryFlags qflags, + Option sortfield_id, QueryFlags qflags, size_t maxnum) const { const auto eff_maxnum{maxnum == 0 ? store_size() : maxnum}; @@ -259,7 +259,7 @@ Query::Private::run(const std::string& expr, } Option -Query::run(const std::string& expr, std::optional sortfield_id, +Query::run(const std::string& expr, Option sortfield_id, QueryFlags qflags, size_t maxnum) const try { // some flags are for internal use only. diff --git a/lib/mu-query.hh b/lib/mu-query.hh index 9101c876..92653969 100644 --- a/lib/mu-query.hh +++ b/lib/mu-query.hh @@ -21,12 +21,12 @@ #define __MU_QUERY_HH__ #include -#include #include #include #include #include +#include #include namespace Mu { @@ -44,10 +44,10 @@ public: * @return the query-results, or Nothing in case of error. */ - Option run(const std::string& expr = "", - std::optional sortfield_id = {}, - QueryFlags flags = QueryFlags::None, - size_t maxnum = 0) const; + Option run(const std::string& expr = "", + Option sortfield_id = {}, + QueryFlags flags = QueryFlags::None, + size_t maxnum = 0) const; /** * run a Xapian query to count the number of matches; for the syntax, please diff --git a/lib/mu-server.cc b/lib/mu-server.cc index a4bdc00b..1303bc63 100644 --- a/lib/mu-server.cc +++ b/lib/mu-server.cc @@ -44,6 +44,7 @@ #include "utils/mu-str.h" #include "utils/mu-utils.hh" +#include "utils/mu-option.hh" #include "utils/mu-command-parser.hh" #include "utils/mu-readline.hh" @@ -119,7 +120,7 @@ private: const Option qm, MuMsgOptions opts) const; - Sexp::List move_docid(Store::Id docid, std::optional flagstr, + Sexp::List move_docid(Store::Id docid, Option flagstr, bool new_name, bool no_view); Sexp::List perform_move(Store::Id docid, @@ -814,7 +815,7 @@ Sexp::List Server::Private::perform_move(Store::Id docid, MuMsg* msg, const std::string& maildirarg, - Flags flags, + Flags flags, bool new_name, bool no_view) { @@ -852,9 +853,9 @@ Server::Private::perform_move(Store::Id docid, static Flags -calculate_message_flags(MuMsg* msg, std::optional flagopt) +calculate_message_flags(MuMsg* msg, Option flagopt) { - const auto flags = std::invoke([&]()->std::optional{ + const auto flags = std::invoke([&]()->Option{ auto msgflags{mu_msg_get_flags(msg)}; if (!flagopt) return mu_msg_get_flags(msg); @@ -870,10 +871,10 @@ calculate_message_flags(MuMsg* msg, std::optional flagopt) } Sexp::List -Server::Private::move_docid(Store::Id docid, - std::optional flagopt, - bool new_name, - bool no_view) +Server::Private::move_docid(Store::Id docid, + Option flagopt, + bool new_name, + bool no_view) { if (docid == Store::InvalidId) throw Error{Error::Code::InvalidArgument, "invalid docid"}; diff --git a/lib/mu-store.cc b/lib/mu-store.cc index 01c62e93..043892a2 100644 --- a/lib/mu-store.cc +++ b/lib/mu-store.cc @@ -631,7 +631,7 @@ Store::lock() const Option Store::run_query(const std::string& expr, - std::optional sortfield_id, + Option sortfield_id, QueryFlags flags, size_t maxnum) const { return xapian_try([&] { diff --git a/lib/mu-store.hh b/lib/mu-store.hh index 77241325..5fd2507f 100644 --- a/lib/mu-store.hh +++ b/lib/mu-store.hh @@ -34,6 +34,8 @@ #include #include #include +#include + #include namespace Mu { @@ -155,10 +157,10 @@ public: * @return the query-results, or Nothing in case of error. */ std::mutex& lock() const; - Option run_query(const std::string& expr = "", - std::optional sortfield_id = {}, - QueryFlags flags = QueryFlags::None, - size_t maxnum = 0) const; + Option run_query(const std::string& expr = "", + Option sortfield_id = {}, + QueryFlags flags = QueryFlags::None, + size_t maxnum = 0) const; /** * run a Xapian query merely to count the number of matches; for the diff --git a/lib/utils/mu-command-parser.cc b/lib/utils/mu-command-parser.cc index 05f1e02d..40be1e9c 100644 --- a/lib/utils/mu-command-parser.cc +++ b/lib/utils/mu-command-parser.cc @@ -32,16 +32,16 @@ Command::invoke(const Command::CommandMap& cmap, const Sexp& call) { if (!call.is_call()) { throw Mu::Error{Error::Code::Command, - "expected call-sexpr but got %s", - call.to_sexp_string().c_str()}; + "expected call-sexpr but got %s", + call.to_sexp_string().c_str()}; } const auto& params{call.list()}; const auto cmd_it = cmap.find(params.at(0).value()); if (cmd_it == cmap.end()) throw Mu::Error{Error::Code::Command, - "unknown command in call %s", - call.to_sexp_string().c_str()}; + "unknown command in call %s", + call.to_sexp_string().c_str()}; const auto& cinfo{cmd_it->second}; @@ -65,9 +65,9 @@ Command::invoke(const Command::CommandMap& cmap, const Sexp& call) if (param_it == params.end()) { if (arginfo.required) throw Mu::Error{Error::Code::Command, - "missing required parameter %s in call %s", - argname.c_str(), - call.to_sexp_string().c_str()}; + "missing required parameter %s in call %s", + argname.c_str(), + call.to_sexp_string().c_str()}; continue; // not required } @@ -75,11 +75,11 @@ Command::invoke(const Command::CommandMap& cmap, const Sexp& call) // "no value" if (param_it->type() != arginfo.type && !(param_it->is_nil())) throw Mu::Error{Error::Code::Command, - "parameter %s expects type %s, but got %s in call %s", - argname.c_str(), - to_string(arginfo.type).c_str(), - to_string(param_it->type()).c_str(), - call.to_sexp_string().c_str()}; + "parameter %s expects type %s, but got %s in call %s", + argname.c_str(), + to_string(arginfo.type).c_str(), + to_string(param_it->type()).c_str(), + call.to_sexp_string().c_str()}; } // all passed parameters must be known @@ -88,9 +88,9 @@ Command::invoke(const Command::CommandMap& cmap, const Sexp& call) return params.at(i).value() == arg.first; })) throw Mu::Error{Error::Code::Command, - "unknown parameter %s in call %s", - params.at(i).value().c_str(), - call.to_sexp_string().c_str()}; + "unknown parameter %s in call %s", + params.at(i).value().c_str(), + call.to_sexp_string().c_str()}; } if (cinfo.handler) @@ -105,8 +105,8 @@ find_param_node(const Parameters& params, const std::string& argname) if (argname.empty() || argname.at(0) != ':') throw Error(Error::Code::InvalidArgument, - "property key must start with ':' but got '%s')", - argname.c_str()); + "property key must start with ':' but got '%s')", + argname.c_str()); for (size_t i = 1; i < params.size(); i += 2) { if (i + 1 != params.size() && params.at(i).is_symbol() && @@ -121,63 +121,63 @@ static Error wrong_type(Sexp::Type expected, Sexp::Type got) { return Error(Error::Code::InvalidArgument, - "expected <%s> but got <%s>", - to_string(expected).c_str(), - to_string(got).c_str()); + "expected <%s> but got <%s>", + to_string(expected).c_str(), + to_string(got).c_str()); } -std::optional +Option Command::get_string(const Parameters& params, const std::string& argname) { const auto it = find_param_node(params, argname); if (it == params.end() || it->is_nil()) - return std::nullopt; + return Nothing; else if (!it->is_string()) throw wrong_type(Sexp::Type::String, it->type()); else return it->value(); } -std::optional +Option Command::get_symbol(const Parameters& params, const std::string& argname) { const auto it = find_param_node(params, argname); if (it == params.end() || it->is_nil()) - return std::nullopt; + return Nothing; else if (!it->is_symbol()) throw wrong_type(Sexp::Type::Symbol, it->type()); else return it->value(); } -std::optional +Option Command::get_int(const Parameters& params, const std::string& argname) { const auto it = find_param_node(params, argname); if (it == params.end() || it->is_nil()) - return std::nullopt; + return Nothing; else if (!it->is_number()) throw wrong_type(Sexp::Type::Number, it->type()); else return ::atoi(it->value().c_str()); } -std::optional +Option Command::get_unsigned(const Parameters& params, const std::string& argname) { if (auto val = get_int(params, argname); val && *val >= 0) return val; else - return std::nullopt; + return Nothing; } -std::optional +Option Command::get_bool(const Parameters& params, const std::string& argname) { const auto it = find_param_node(params, argname); if (it == params.end()) - return std::nullopt; + return Nothing; else if (!it->is_symbol()) throw wrong_type(Sexp::Type::Symbol, it->type()); else diff --git a/lib/utils/mu-command-parser.hh b/lib/utils/mu-command-parser.hh index b7a47763..751e5d8e 100644 --- a/lib/utils/mu-command-parser.hh +++ b/lib/utils/mu-command-parser.hh @@ -26,10 +26,10 @@ #include #include #include -#include #include "utils/mu-error.hh" #include "utils/mu-sexp.hh" +#include "utils/mu-option.hh" namespace Mu { namespace Command { @@ -62,11 +62,11 @@ using ArgMap = std::unordered_map; // The parameters to a Handler. using Parameters = Sexp::Seq; -std::optional get_int(const Parameters& parms, const std::string& argname); -std::optional get_unsigned(const Parameters& parms, const std::string& argname); -std::optional get_bool(const Parameters& parms, const std::string& argname); -std::optional get_string(const Parameters& parms, const std::string& argname); -std::optional get_symbol(const Parameters& parms, const std::string& argname); +Option get_int(const Parameters& parms, const std::string& argname); +Option get_unsigned(const Parameters& parms, const std::string& argname); +Option get_bool(const Parameters& parms, const std::string& argname); +Option get_string(const Parameters& parms, const std::string& argname); +Option get_symbol(const Parameters& parms, const std::string& argname); std::vector get_string_vec(const Parameters& params, const std::string& argname); @@ -86,7 +86,7 @@ static inline std::string get_string_or(const Parameters& parms, const std::string& arg, const std::string& alt = ""){ return get_string(parms, arg).value_or(alt); } - + static inline std::string get_symbol_or(const Parameters& parms, const std::string& arg, const std::string& alt = "nil") { return get_symbol(parms, arg).value_or(alt);