lib: clean up mu_msg_to_sexp API

move out the QueryMatch
This commit is contained in:
Dirk-Jan C. Binnema
2021-01-29 22:39:21 +02:00
parent e5a52e45fd
commit 72fdfec3eb
5 changed files with 89 additions and 68 deletions

View File

@ -283,13 +283,6 @@ make_part_types (MuMsgPartType ptype)
return Sexp::make_list(std::move(list));
}
static Sexp
symbol_t()
{
return Sexp::make_symbol("t");
}
static void
each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo)
{
@ -316,7 +309,7 @@ each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo)
g_free (fname);
if (mu_msg_part_maybe_attachment (part))
partlist.add_prop(":attachment", symbol_t());
partlist.add_prop(":attachment", Sexp::make_symbol("t"));
const auto cid{ mu_msg_part_get_content_id(part)};
if (cid)
partlist.add_prop(":cid", Sexp::make_string(cid));
@ -341,32 +334,6 @@ add_parts (Sexp::List& items, MuMsg *msg, MuMsgOptions opts)
items.add_prop(":parts", Sexp::make_list(std::move(pinfo.parts)));
}
static void
add_thread_info (Sexp::List& items, const QueryMatch& qmatch)
{
Sexp::List info;
info.add_prop(":path", Sexp::make_string(qmatch.thread_path));
info.add_prop(":level", Sexp::make_number(qmatch.thread_level));
if (any_of(qmatch.flags & QueryMatch::Flags::Root))
info.add_prop( ":root", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Related))
info.add_prop( ":related", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::First))
info.add_prop( ":first-child", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Last))
info.add_prop( ":last-child", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Orphan))
info.add_prop( ":empty-parent", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Duplicate))
info.add_prop( ":duplicate", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::HasChild))
info.add_prop( ":has-child", symbol_t());
items.add_prop(":thread", Sexp::make_list(std::move(info)));
}
static void
add_message_file_parts (Sexp::List& items, MuMsg *msg, MuMsgOptions opts)
@ -429,22 +396,18 @@ add_tags (Sexp::List& items, MuMsg *msg)
}
Mu::Sexp
Mu::msg_to_sexp (MuMsg *msg, unsigned docid,
const Option<QueryMatch&> qm, MuMsgOptions opts)
Mu::Sexp::List
Mu::msg_to_sexp_list (MuMsg *msg, unsigned docid, MuMsgOptions opts)
{
g_return_val_if_fail (msg, Sexp::make_symbol("error"));
g_return_val_if_fail (msg, Sexp::List());
g_return_val_if_fail (!((opts & MU_MSG_OPTION_HEADERS_ONLY) &&
(opts & MU_MSG_OPTION_EXTRACT_IMAGES)),
Sexp::make_symbol("error"));
Sexp::List());
Sexp::List items;
if (docid != 0)
items.add_prop(":docid", Sexp::make_number(docid));
if (qm)
add_thread_info (items, *qm);
add_prop_nonempty (items, ":subject", mu_msg_get_subject (msg));
add_prop_nonempty (items, ":message-id", mu_msg_get_msgid (msg));
add_prop_nonempty (items, ":mailing-list", mu_msg_get_mailing_list (msg));
@ -471,5 +434,12 @@ Mu::msg_to_sexp (MuMsg *msg, unsigned docid,
if (!(opts & MU_MSG_OPTION_HEADERS_ONLY))
add_message_file_parts (items, msg, opts);
return Sexp::make_list(std::move(items));
return items;
}
Mu::Sexp
Mu::msg_to_sexp (MuMsg *msg, unsigned docid, MuMsgOptions opts)
{
return Sexp::make_list(msg_to_sexp_list(msg, docid, opts));
}

View File

@ -601,7 +601,6 @@ struct QueryMatch;
*
* @param msg a valid message
* @param docid the docid for this message, or 0
* @param qm information about this match
* @param opts, bitwise OR'ed;
* - MU_MSG_OPTION_HEADERS_ONLY: only include message fields which can be
* obtained from the database (this is much faster if the MuMsg is
@ -614,11 +613,10 @@ struct QueryMatch;
* MU_MSG_OPTION_USE_AGENT: attempt to use GPG-agent
* MU_MSG_OPTION_USE_PKCS7: attempt to use PKCS (instead of gpg)
*
* @return a Sexp::Node representing the message
* @return a Mu::Sexp or a Mu::Sexp::List representing the message.
*/
Mu::Sexp msg_to_sexp (MuMsg *msg, unsigned docid,
const Option<QueryMatch&> qm,
MuMsgOptions ops);
Mu::Sexp::List msg_to_sexp_list(MuMsg *msg, unsigned docid, MuMsgOptions ops);
Mu::Sexp msg_to_sexp(MuMsg *msg, unsigned docid, MuMsgOptions ops);
}
#endif /*MU_MSG_HH__*/

View File

@ -109,7 +109,19 @@ struct Server::Private {
void sent_handler (const Parameters& params);
void view_handler (const Parameters& params);
private:
// helpers
Sexp build_message_sexp(MuMsg *msg, unsigned docid,
const Option<QueryMatch&> qm,
MuMsgOptions opts);
Sexp::List move_docid (DocId docid, const std::string& flagstr,
bool new_name, bool no_view);
Sexp::List perform_move (DocId docid, MuMsg *msg,
const std::string& maildirarg,
MuFlags flags, bool new_name, bool no_view);
Store& store_;
Server::Output output_;
const CommandMap command_map_;
@ -118,7 +130,48 @@ private:
std::atomic<bool> keep_going_{};
};
static void
add_thread_info (Sexp::List& items, const QueryMatch& qmatch)
{
Sexp::List info;
auto symbol_t = []{return Sexp::make_symbol("t");};
info.add_prop(":path", Sexp::make_string(qmatch.thread_path));
info.add_prop(":level", Sexp::make_number(qmatch.thread_level));
if (qmatch.has_flag(QueryMatch::Flags::Root))
info.add_prop( ":root", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Related))
info.add_prop( ":related", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::First))
info.add_prop( ":first-child", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Last))
info.add_prop( ":last-child", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Orphan))
info.add_prop( ":empty-parent", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Duplicate))
info.add_prop( ":duplicate", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::HasChild))
info.add_prop( ":has-child", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::ThreadSubject))
info.add_prop( ":thread_subject", symbol_t());
items.add_prop(":thread", Sexp::make_list(std::move(info)));
}
Sexp
Server::Private::build_message_sexp (MuMsg *msg, unsigned docid,
const Option<QueryMatch&> qm,
MuMsgOptions opts)
{
auto lst{Mu::msg_to_sexp_list(msg, docid, opts)};
if (qm)
add_thread_info(lst, *qm);
return Sexp::make_list(std::move(lst));
}
CommandMap
Server::Private::make_command_map ()
{
@ -358,7 +411,7 @@ Server::Private::add_handler (const Parameters& params)
path.c_str(), docid);
Sexp::List update;
update.add_prop(":update", Mu::msg_to_sexp(msg, docid, {}, MU_MSG_OPTION_VERIFY));
update.add_prop(":update", build_message_sexp(msg, docid, {}, MU_MSG_OPTION_VERIFY));
output_sexp(Sexp::make_list(std::move(update)));
mu_msg_unref(msg);
}
@ -420,7 +473,7 @@ Server::Private::compose_handler (const Parameters& params)
throw Error{Error::Code::Store, &gerr, "failed to get message %u", docid};
const auto opts{message_options(params)};
comp_lst.add_prop(":original", Mu::msg_to_sexp(msg, docid, {}, opts));
comp_lst.add_prop(":original", build_message_sexp(msg, docid, {}, opts));
if (ctype == "forward") {
PartInfo pinfo{};
@ -686,7 +739,7 @@ Server::Private::output_sexp (const QueryResults& qres, unsigned maxnum)
continue;
auto qm{mi.query_match()};
output_sexp(Mu::msg_to_sexp(msg, mi.doc_id(),
output_sexp(build_message_sexp(msg, mi.doc_id(),
qm, MU_MSG_OPTION_HEADERS_ONLY));
}
@ -856,8 +909,8 @@ get_flags (const std::string& path, const std::string& flagstr)
}
}
static Sexp::List
perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildirarg,
Sexp::List
Server::Private::perform_move (DocId docid, MuMsg *msg, const std::string& maildirarg,
MuFlags flags, bool new_name, bool no_view)
{
bool different_mdir{};
@ -874,11 +927,11 @@ perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildira
/* after mu_msg_move_to_maildir, path will be the *new* path, and flags and maildir fields
* will be updated as wel */
if (!store.update_message (msg, docid))
if (!store_.update_message (msg, docid))
throw Error{Error::Code::Store, "failed to store updated message"};
Sexp::List seq;
seq.add_prop(":update", msg_to_sexp (msg, docid, {}, MU_MSG_OPTION_VERIFY));
seq.add_prop(":update", build_message_sexp (msg, docid, {}, MU_MSG_OPTION_VERIFY));
/* note, the :move t thing is a hint to the frontend that it
* could remove the particular header */
if (different_mdir)
@ -889,14 +942,14 @@ perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildira
return seq;
}
static Sexp::List
move_docid (Store& store, DocId docid, const std::string& flagstr,
Sexp::List
Server::Private::move_docid (DocId docid, const std::string& flagstr,
bool new_name, bool no_view)
{
if (docid == MU_STORE_INVALID_DOCID)
throw Error{Error::Code::InvalidArgument, "invalid docid"};
auto msg{store.find_message(docid)};
auto msg{store_.find_message(docid)};
try {
if (!msg)
throw Error{Error::Code::Store, "failed to get message from store"};
@ -907,7 +960,7 @@ move_docid (Store& store, DocId docid, const std::string& flagstr,
throw Error{Error::Code::InvalidArgument, "invalid flags '%s'",
flagstr.c_str()};
auto lst = perform_move(store, docid, msg, "", flags, new_name, no_view);
auto lst = perform_move(docid, msg, "", flags, new_name, no_view);
mu_msg_unref (msg);
return lst;
@ -942,7 +995,7 @@ Server::Private::move_handler (const Parameters& params)
"can't move multiple messages at the same time"};
// multi.
for (auto&& docid: docids)
output_sexp(move_docid(store(), docid, flagstr, rename, no_view));
output_sexp(move_docid(docid, flagstr, rename, no_view));
return;
}
auto docid{docids.at(0)};
@ -972,7 +1025,7 @@ Server::Private::move_handler (const Parameters& params)
}
try {
output_sexp(perform_move(store(), docid, msg, maildir, flags,
output_sexp(perform_move(docid, msg, maildir, flags,
rename, no_view));
} catch (...) {
mu_msg_unref(msg);
@ -1128,7 +1181,7 @@ Server::Private::view_handler (const Parameters& params)
maybe_mark_as_read (store(), msg, docid);
Sexp::List seq;
seq.add_prop(":view", msg_to_sexp(msg, docid, {}, message_options(params)));
seq.add_prop(":view", build_message_sexp(msg, docid, {}, message_options(params)));
mu_msg_unref(msg);

View File

@ -496,7 +496,7 @@ output_sexp (MuMsg *msg, const OutputInfo& info, const MuConfig *opts, GError **
if (!msg)
return true;
fputs(msg_to_sexp(msg, 0, {}, MU_MSG_OPTION_HEADERS_ONLY)
fputs(msg_to_sexp(msg, 0, MU_MSG_OPTION_HEADERS_ONLY)
.to_sexp_string().c_str(), stdout);
fputs ("\n", stdout);
@ -516,7 +516,7 @@ output_json (MuMsg *msg, const OutputInfo& info, const MuConfig *opts, GError **
return true;
}
g_print("%s\n", msg_to_sexp(msg, info.docid, {}, MU_MSG_OPTION_HEADERS_ONLY)
g_print("%s\n", msg_to_sexp(msg, info.docid, MU_MSG_OPTION_HEADERS_ONLY)
.to_sexp_string().c_str());
return true;

View File

@ -50,7 +50,7 @@ using namespace Mu;
static gboolean
view_msg_sexp (MuMsg *msg, const MuConfig *opts)
{
::fputs(msg_to_sexp(msg,0,{}, mu_config_get_msg_options(opts))
::fputs(msg_to_sexp(msg,0, mu_config_get_msg_options(opts))
.to_sexp_string(). c_str(), stdout);
return TRUE;
}