From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id AHe3NV9l6WHoVAEAgWs5BA (envelope-from ) for ; Thu, 20 Jan 2022 14:36:31 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id SAZgLl9l6WFjfQAAG6o9tA (envelope-from ) for ; Thu, 20 Jan 2022 14:36:31 +0100 Received: from mail.notmuchmail.org (yantan.tethera.net [IPv6:2a01:4f9:c011:7a79::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 48E272FDB1 for ; Thu, 20 Jan 2022 14:36:31 +0100 (CET) Received: from yantan.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 072225F71D; Thu, 20 Jan 2022 13:36:17 +0000 (UTC) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 38EDB5F6EC for ; Thu, 20 Jan 2022 13:36:13 +0000 (UTC) Received: by fethera.tethera.net (Postfix, from userid 1001) id B9A455FC01; Thu, 20 Jan 2022 08:36:12 -0500 (EST) Received: (nullmailer pid 31637 invoked by uid 1000); Thu, 20 Jan 2022 13:36:08 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [PATCH v2 3/6] lib/date: factor out date range parsing. Date: Thu, 20 Jan 2022 09:36:02 -0400 Message-Id: <20220120133605.31401-4-david@tethera.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220120133605.31401-1-david@tethera.net> References: <20220120133605.31401-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: LCA5IBIWJDXBDYDPEFEKG2PZKZK4DKAN X-Message-ID-Hash: LCA5IBIWJDXBDYDPEFEKG2PZKZK4DKAN 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; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Country: DE ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1642685791; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to: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-owner:list-unsubscribe:list-subscribe:list-post; bh=Z8MFvYGGhf6n11vbuqewJ38PbpC16oSkWbV1BWwxKUE=; b=bUa20aLB7dB1eDWfSk5JOExwcsKOkHM5XNJrrlDi5OEyPGwMj32+5Pa3oZn8Y1aAeiaJYl WL5IHhPlhgYxXcRPvVEMAJ8dZSiaz76vJIEkQHHAWutJGGKweZKLNv1fPCkP41zgRcqkYL gj0tMCIS0+PwLt+GKH6wgUQMDmptiZ8mFDMjTHXWcEmrxExl0DAuzBmsDn1l7w6wiF/qCA J3qbbzuSBecx0YeEjQ/A4yDWTMg4APNBJe67iTkhBF+oNn8JS7Ltj4niGvPhSSFfHoIfzG l/BQGuG2bPyQ273DVN/dqoNzi7qJLEDaMqs82sB8PKKeU5iZbjxkgTFzdDTU6A== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1642685791; a=rsa-sha256; cv=none; b=GWxOpHfPRT0e9FtwsC4afXf716+4cRJOE9/8z7yywKbFjI6xXOKaYWcrwgzsk2J6MRXA1X cdOVHDNKjnW7CsOogKJiIEVEjVrXQTQSAJ8xk7QYEn5IweEcKdo5eFv5yqi+YchMaoxWsr CpSjgsztkD1k7lsoUzFGrK1ZDxwtBduch+3Y0XgrGstzN6Qc33lUBGVVWFYGlMBFsvcolE mlFYEbS9uN88i5CzJq2hnecVUF2Y1oL2MD8Eyxa/mQuEYf4WhC9XoSnpHbB1vlwIDWJH0A cYcUs20ToY36ffQB8bRUKKqG8mePWWPvrS1cK32AMtSkd69hhaoGiwL0yL8hBw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.82 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 48E272FDB1 X-Spam-Score: -1.82 X-Migadu-Scanner: scn0.migadu.com X-TUID: NwMNHU5mr7VO This will allow re-using the same logic in the s-expression parser. --- lib/database-private.h | 5 +++++ lib/parse-time-vrp.cc | 51 +++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/lib/database-private.h b/lib/database-private.h index 5db8882f..0c08fa15 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -379,5 +379,10 @@ notmuch_status_t _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr, Xapian::Query &output); #endif + +/* parse-time-vrp.h */ +notmuch_status_t +_notmuch_date_strings_to_query (Xapian::valueno slot, const std::string &from, const std::string &to, + Xapian::Query &output, std::string &msg); #endif #endif diff --git a/lib/parse-time-vrp.cc b/lib/parse-time-vrp.cc index 22bf2ab5..6b07970b 100644 --- a/lib/parse-time-vrp.cc +++ b/lib/parse-time-vrp.cc @@ -24,22 +24,28 @@ #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_date_strings_to_query (Xapian::valueno slot, + const std::string &begin, const std::string &end, + Xapian::Query &output, std::string &msg) { 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"; + 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 - from = (double) parsed_time; + if (parse_time_string (begin.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN)) { + msg = "Didn't understand date specification '" + begin + "'"; + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + + from = (double) parsed_time; } if (! end.empty ()) { @@ -48,15 +54,30 @@ 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 - to = (double) parsed_time; + 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_BAD_QUERY_SYNTAX; + } + 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; +} + +Xapian::Query +ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string &end) +{ + + Xapian::Query output; + std::string msg; + + if (_notmuch_date_strings_to_query (slot, begin, end, output, msg)) + throw Xapian::QueryParserError (msg); + + return output; } /* XXX TODO: is throwing an exception the right thing to do here? */ -- 2.34.1