From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id sK6SDsqU82BhDgAAgWs5BA (envelope-from ) for ; Sun, 18 Jul 2021 04:41:14 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id cKVTCsqU82ChXgAA1q6Kng (envelope-from ) for ; Sun, 18 Jul 2021 02:41:14 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id B243222907 for ; Sun, 18 Jul 2021 04:41:13 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id A3C302934C; Sat, 17 Jul 2021 22:40:53 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 7220629081 for ; Sat, 17 Jul 2021 22:40:35 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id 35A185FD21; Sat, 17 Jul 2021 22:40:35 -0400 (EDT) Received: (nullmailer pid 3853943 invoked by uid 1000); Sun, 18 Jul 2021 02:40:27 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 11/25] lib: factor out date to query conversion Date: Sat, 17 Jul 2021 23:40:07 -0300 Message-Id: <20210718024021.3850340-12-david@tethera.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210718024021.3850340-1-david@tethera.net> References: <20210718024021.3850340-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: WGQPMWRB3YB5DUAN5WUEFG2HXBNPX43J X-Message-ID-Hash: WGQPMWRB3YB5DUAN5WUEFG2HXBNPX43J X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1626576073; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Z/03vKF3WhOldGShL8lV4NW2DpIw8xV/tGa1n9r9OuY=; b=mc2B/17588s9lb8EzTTUlJtLlMNZ8DWZ9iWf8ANUC9gbOW4syA3RoV7rXqqe8ooYWw31lH spDulIrCPXUnmyUsN85ktQp/posI2E/QBUOm0qx79IU1wvyQX9Nj0VJZk7o/mfXT58ky46 ejJhe+bBRaLnNSCvhIMW8faV59mf6RbWQDFceRVcBsGABiaEy1UcjpOFA6AnQsBRX66lBI cTzglAny6Bxan5TcOKFNKEJJSc7jG/NldQkhGR+0HBDkQOibW3ApDbU2BDTmvPgK1Cz9x5 fmiDmcLmzTvSP0J5esTByzPQj1uuqnjxhjzUXGwk3AibB6psfBBNJ2fZ0jOI2Q== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1626576073; a=rsa-sha256; cv=none; b=uyOAM3rTmt1LTa31X/nWJQOMBCsIntsyy8KpNWU0tl7xjWLi2ymxOE9fvxPrJnJVbi+rFx k+MalTL6FfJXqlMwxrNArMZnF/30mU8EXXatDucsdY5LNhznDCLCSexAMRs0exa2WTXuMe gLYy9/3gYPLjCAzsDq2F/yiFA4nzXfGya/8EpCVHP0A7vyZ54BfYHJInyZv9uuQVu42tED QhpVPr+OKS02UYG1F6jC62n88il8fjRXUQRdWdc3RWB382u+d/6X6pVwQz0jPNE63UWAna Z26eoWoV5h62qVQrR4CprfzJhqPUfwA8Cy1GtPzxoNbIY9C/KmtI1dwS1ALxbQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.05 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: B243222907 X-Spam-Score: -1.05 X-Migadu-Scanner: scn0.migadu.com X-TUID: onrQyUriC7JX This is a bit messy, but throwing and catching Xapian::QueryParserError exceptions outside of the Xapian query parser seems worse. --- lib/parse-time-vrp.cc | 97 +++++++++++++++++++++++++++++++------------ lib/parse-time-vrp.h | 8 ++++ 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/lib/parse-time-vrp.cc b/lib/parse-time-vrp.cc index 22bf2ab5..75c67797 100644 --- a/lib/parse-time-vrp.cc +++ b/lib/parse-time-vrp.cc @@ -24,21 +24,26 @@ #include "parse-time-vrp.h" #include "parse-time-string.h" -Xapian::Query -ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string &end) +notmuch_status_t +_notmuch_time_range_to_query (Xapian::valueno slot, const std::string &begin, const std::string &end, + std::string &msg, Xapian::Query &output) { double from = DBL_MIN, to = DBL_MAX; time_t parsed_time, now; std::string str; /* Use the same 'now' for begin and end. */ - if (time (&now) == (time_t) -1) - throw Xapian::QueryParserError ("unable to get current time"); + if (time (&now) == (time_t) -1) { + msg = "unable to get current time"; + /* XXX Give a better status value */ + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } if (! begin.empty ()) { - if (parse_time_string (begin.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN)) - throw Xapian::QueryParserError ("Didn't understand date specification '" + begin + "'"); - else + if (parse_time_string (begin.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN)) { + msg = "Didn't understand date specification '" + begin + "'"; + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } else from = (double) parsed_time; } @@ -48,39 +53,79 @@ ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string else str = end; - if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE)) - throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'"); - else + if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE)) { + msg = "Didn't understand date specification '" + str + "'"; + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } else to = (double) parsed_time; } - return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot, - Xapian::sortable_serialise (from), - Xapian::sortable_serialise (to)); + output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot, + Xapian::sortable_serialise (from), + Xapian::sortable_serialise (to)); + + return NOTMUCH_STATUS_SUCCESS; } -/* XXX TODO: is throwing an exception the right thing to do here? */ Xapian::Query -DateFieldProcessor::operator() (const std::string & str) +ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string &end) +{ + Xapian::Query output; + notmuch_status_t status; + std::string msg; + + status = _notmuch_time_range_to_query (slot, begin, end, msg, output); + if (status) + throw Xapian::QueryParserError (msg); + + return output; +} + +notmuch_status_t +_notmuch_time_string_to_query (Xapian::valueno slot, const std::string &str, + std::string &msg, Xapian::Query &output) { double from = DBL_MIN, to = DBL_MAX; time_t parsed_time, now; /* Use the same 'now' for begin and end. */ - if (time (&now) == (time_t) -1) - throw Xapian::QueryParserError ("Unable to get current time"); + if (time (&now) == (time_t) -1) { + msg = "Unable to get current time"; + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } - if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN)) - throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'"); - else + if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN)) { + msg = "Didn't understand date specification '" + str + "'"; + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } else from = (double) parsed_time; - if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE)) - throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'"); - else + if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE)) { + msg = "Didn't understand date specification '" + str + "'"; + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } else to = (double) parsed_time; - return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot, - Xapian::sortable_serialise (from), - Xapian::sortable_serialise (to)); + output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot, + Xapian::sortable_serialise (from), + Xapian::sortable_serialise (to)); + + return NOTMUCH_STATUS_SUCCESS; + +} + +/* XXX TODO: is throwing an exception the right thing to do here? */ +Xapian::Query +DateFieldProcessor::operator() (const std::string & str) +{ + Xapian::Query output; + notmuch_status_t status; + std::string msg; + + status = _notmuch_time_string_to_query (slot, str, msg, output); + if (status) + throw Xapian::QueryParserError (msg); + + return output; + } diff --git a/lib/parse-time-vrp.h b/lib/parse-time-vrp.h index f495e716..76d16eb2 100644 --- a/lib/parse-time-vrp.h +++ b/lib/parse-time-vrp.h @@ -25,6 +25,14 @@ #include +/* for use outside the Xapian query parser */ +notmuch_status_t +_notmuch_time_range_to_query (Xapian::valueno slot, const std::string &begin, const std::string &end, + std::string &msg, Xapian::Query &output); +notmuch_status_t +_notmuch_time_string_to_query (Xapian::valueno slot, const std::string &str, + std::string &msg, Xapian::Query &output); + /* see *ValueRangeProcessor in xapian-core/include/xapian/queryparser.h */ class ParseTimeRangeProcessor : public Xapian::RangeProcessor { -- 2.30.2