From 59fbdd2fed7e56afb9ca5b50bab50229427cba1b Mon Sep 17 00:00:00 2001 From: Peter Feigl Date: Fri, 13 Jul 2012 10:11:35 +0200 Subject: [PATCH] [PATCH v5 1/3] Add support for structured output formatters. --- 50/b26bf2286466745f9de94d2ceb9ac16f027a63 | 187 ++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 50/b26bf2286466745f9de94d2ceb9ac16f027a63 diff --git a/50/b26bf2286466745f9de94d2ceb9ac16f027a63 b/50/b26bf2286466745f9de94d2ceb9ac16f027a63 new file mode 100644 index 000000000..e38a8c327 --- /dev/null +++ b/50/b26bf2286466745f9de94d2ceb9ac16f027a63 @@ -0,0 +1,187 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 6F71D42119C + for ; Fri, 13 Jul 2012 01:13:42 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0.001 +X-Spam-Level: +X-Spam-Status: No, score=0.001 tagged_above=-999 required=5 + tests=[FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001] + autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 6rWCsySVhxs0 for ; + Fri, 13 Jul 2012 01:13:38 -0700 (PDT) +Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.23]) + by olra.theworths.org (Postfix) with SMTP id 5182D431FAE + for ; Fri, 13 Jul 2012 01:13:38 -0700 (PDT) +Received: (qmail invoked by alias); 13 Jul 2012 08:13:37 -0000 +Received: from cm56-137-203.liwest.at (EHLO nexork.localdomain) + [86.56.137.203] by mail.gmx.net (mp035) with SMTP; 13 Jul 2012 10:13:37 +0200 +X-Authenticated: #201305 +X-Provags-ID: V01U2FsdGVkX193oLhdBFl3aBKpTndLiBGE+TLr/5HmDGbu4KAsHe + eh9Ap1g+Hv+kSZ +Received: by nexork.localdomain (Postfix, from userid 1000) + id 9BC3D4527802; Fri, 13 Jul 2012 10:11:42 +0200 (CEST) +From: Peter Feigl +To: notmuch@notmuchmail.org +Subject: [PATCH v5 1/3] Add support for structured output formatters. +Date: Fri, 13 Jul 2012 10:11:35 +0200 +Message-Id: <1342167097-25012-2-git-send-email-craven@gmx.net> +X-Mailer: git-send-email 1.7.11.1 +In-Reply-To: <1342167097-25012-1-git-send-email-craven@gmx.net> +References: <20120710191331.GE7332@mit.edu> + <1342167097-25012-1-git-send-email-craven@gmx.net> +X-Y-GMX-Trusted: 0 +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Fri, 13 Jul 2012 08:13:42 -0000 + +From: + +This patch adds a new type sprinter_t, which is used for structured +formatting, e.g. JSON or S-Expressions. The structure printer is the +code from Austin Clements (id:87d34hsdx8.fsf@awakening.csail.mit.edu) +with minor modifications. + +The structure printer contains the following function pointers: + +/* Start a new map/dictionary structure. This should be followed by + * a sequence of alternating calls to map_key and one of the + * value-printing functions until the map is ended by end. + */ +void (*begin_map)(struct sprinter *); + +/* Start a new list/array structure. + */ +void (*begin_list)(struct sprinter *); + +/* End the last opened list or map structure. + */ +void (*end)(struct sprinter *); + +/* Set the prefix of the next component. This is purely for + * debugging purposes and for the unstructured text formatter. + */ +void (*set_prefix)(struct sprinter *, const char *); + +/* Print one string/integer/boolean/null element (possibly inside a + * list or map, followed or preceded by separators). + * For string, the char * must be UTF-8 encoded. + */ +void (*string)(struct sprinter *, const char *); +void (*integer)(struct sprinter *, int); +void (*boolean)(struct sprinter *, notmuch_bool_t); +void (*null)(struct sprinter *); + +/* Print the key of a map's key/value pair. The char * must be UTF-8 + * encoded. + */ +void (*map_key)(struct sprinter *, const char *); + +/* Insert a separator (usually extra whitespace) for improved + * readability without affecting the abstract syntax of the + * structure being printed. + * For JSON, this could simply be a line break. + */ +void (*separator)(struct sprinter *); + +The printer can (and should) use internal state to insert delimiters and +syntax at the correct places. + +Example: + +format->begin_map(format); +format->map_key(format, "foo"); +format->begin_list(format); +format->integer(format, 1); +format->integer(format, 2); +format->integer(format, 3); +format->end(format); +format->map_key(format, "bar"); +format->begin_map(format); +format->map_key(format, "baaz"); +format->string(format, "hello world"); +format->end(format); +format->end(format); + +would output JSON as follows: + +{"foo": [1, 2, 3], "bar": { "baaz": "hello world"}} +--- + sprinter.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ + 1 file changed, 50 insertions(+) + create mode 100644 sprinter.h + +diff --git a/sprinter.h b/sprinter.h +new file mode 100644 +index 0000000..c9cd6a6 +--- /dev/null ++++ b/sprinter.h +@@ -0,0 +1,50 @@ ++#ifndef NOTMUCH_SPRINTER_H ++#define NOTMUCH_SPRINTER_H ++ ++/* Necessary for notmuch_bool_t */ ++#include "notmuch-client.h" ++ ++/* Structure printer interface */ ++typedef struct sprinter { ++ /* Start a new map/dictionary structure. This should be followed by ++ * a sequence of alternating calls to map_key and one of the ++ * value-printing functions until the map is ended by end. ++ */ ++ void (*begin_map)(struct sprinter *); ++ ++ /* Start a new list/array structure. ++ */ ++ void (*begin_list)(struct sprinter *); ++ ++ /* End the last opened list or map structure. ++ */ ++ void (*end)(struct sprinter *); ++ ++ /* Set the prefix of the next component. This is purely for ++ * debugging purposes and for the unstructured text formatter. ++ */ ++ void (*set_prefix)(struct sprinter *, const char *); ++ ++ /* Print one string/integer/boolean/null element (possibly inside a ++ * list or map, followed or preceded by separators). ++ * For string, the char * must be UTF-8 encoded. ++ */ ++ void (*string)(struct sprinter *, const char *); ++ void (*integer)(struct sprinter *, int); ++ void (*boolean)(struct sprinter *, notmuch_bool_t); ++ void (*null)(struct sprinter *); ++ ++ /* Print the key of a map's key/value pair. The char * must be UTF-8 ++ * encoded. ++ */ ++ void (*map_key)(struct sprinter *, const char *); ++ ++ /* Insert a separator (usually extra whitespace) for improved ++ * readability without affecting the abstract syntax of the ++ * structure being printed. ++ * For JSON, this could simply be a line break. ++ */ ++ void (*separator)(struct sprinter *); ++} sprinter_t; ++ ++#endif // NOTMUCH_SPRINTER_H +-- +1.7.11.1 + -- 2.26.2