Re: [PATCH 01/11] parse_tag_line: use enum for return value.
[notmuch-archives.git] / f4 / 2a5d31fcffc224ac739ed1006d8d2b6732a94a
1 Return-Path: <craven@gmx.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id B887D431FB6\r
6         for <notmuch@notmuchmail.org>; Mon, 23 Jul 2012 03:38:18 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.001\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.001 tagged_above=-999 required=5\r
12         tests=[FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001]\r
13         autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id toMiaExP9dnR for <notmuch@notmuchmail.org>;\r
17         Mon, 23 Jul 2012 03:38:17 -0700 (PDT)\r
18 Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.22])\r
19         by olra.theworths.org (Postfix) with SMTP id 35BDD431FAF\r
20         for <notmuch@notmuchmail.org>; Mon, 23 Jul 2012 03:38:17 -0700 (PDT)\r
21 Received: (qmail invoked by alias); 23 Jul 2012 10:38:15 -0000\r
22 Received: from gw.arelion.cust.net.lagis.at (EHLO dodekanex.arelion.at)\r
23         [83.164.197.182]\r
24         by mail.gmx.net (mp030) with SMTP; 23 Jul 2012 12:38:15 +0200\r
25 X-Authenticated: #201305\r
26 X-Provags-ID: V01U2FsdGVkX1+YUQ8honOYuWCeL42Obr4DGJgqN+GyHAr6k0icdC\r
27         4TVSVY4y4htGHU\r
28 Received: by dodekanex.arelion.at (Postfix, from userid 1000)\r
29         id B5B3F3034D2; Mon, 23 Jul 2012 12:39:50 +0200 (CEST)\r
30 From: craven@gmx.net\r
31 To: notmuch@notmuchmail.org\r
32 Subject: [PATCH v8 1/3] Add support for structured output formatters.\r
33 Date: Mon, 23 Jul 2012 12:39:44 +0200\r
34 Message-Id: <1343039986-2732-2-git-send-email-craven@gmx.net>\r
35 X-Mailer: git-send-email 1.7.11.2\r
36 In-Reply-To: <1343039986-2732-1-git-send-email-craven@gmx.net>\r
37 References: <1343039986-2732-1-git-send-email-craven@gmx.net>\r
38 X-Y-GMX-Trusted: 0\r
39 X-BeenThere: notmuch@notmuchmail.org\r
40 X-Mailman-Version: 2.1.13\r
41 Precedence: list\r
42 List-Id: "Use and development of the notmuch mail system."\r
43         <notmuch.notmuchmail.org>\r
44 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
45         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
46 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
47 List-Post: <mailto:notmuch@notmuchmail.org>\r
48 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
49 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
50         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
51 X-List-Received-Date: Mon, 23 Jul 2012 10:38:18 -0000\r
52 \r
53 From: <craven@gmx.net>\r
54 \r
55 This patch adds a new struct type sprinter_t, which is used for\r
56 structured formatting, e.g. JSON or S-Expressions. The structure printer\r
57 is heavily based on code from Austin Clements\r
58 (id:87d34hsdx8.fsf@awakening.csail.mit.edu).\r
59 \r
60 It includes the following functions:\r
61 \r
62     /* Start a new map/dictionary structure. This should be followed by\r
63      * a sequence of alternating calls to map_key and one of the\r
64      * value-printing functions until the map is ended by end.\r
65      */\r
66     void (*begin_map) (struct sprinter *);\r
67 \r
68     /* Start a new list/array structure.\r
69      */\r
70     void (*begin_list) (struct sprinter *);\r
71 \r
72     /* End the last opened list or map structure.\r
73      */\r
74     void (*end) (struct sprinter *);\r
75 \r
76     /* Print one string/integer/boolean/null element (possibly inside a\r
77      * list or map, followed or preceded by separators).\r
78      * For string, the char * must be UTF-8 encoded.\r
79      */\r
80     void (*string) (struct sprinter *, const char *);\r
81     void (*integer) (struct sprinter *, int);\r
82     void (*boolean) (struct sprinter *, notmuch_bool_t);\r
83     void (*null) (struct sprinter *);\r
84 \r
85     /* Print the key of a map's key/value pair. The char * must be UTF-8\r
86      * encoded.\r
87      */\r
88     void (*map_key) (struct sprinter *, const char *);\r
89 \r
90     /* Insert a separator (usually extra whitespace) for improved\r
91      * readability without affecting the abstract syntax of the\r
92      * structure being printed.\r
93      * For JSON, this could simply be a line break.\r
94      */\r
95     void (*separator) (struct sprinter *);\r
96 \r
97     /* Set the current string prefix. This only affects the text\r
98      * printer, which will print this string, followed by a colon,\r
99      * before any string. For other printers, this does nothing.\r
100      */\r
101     void (*set_prefix) (struct sprinter *, const char *);\r
102 \r
103 To support the plain text format properly, the following additional\r
104 function must also be implemented:\r
105 \r
106     /* Set the current string prefix. This only affects the text\r
107      * printer, which will print this string, followed by a colon,\r
108      * before any string. For other printers, this does nothing.\r
109      */\r
110     void (*set_prefix) (struct sprinter *, const char *);\r
111 \r
112 The structure also contains a flag that should be set to FALSE in all\r
113 custom printers and to TRUE in the plain text formatter.\r
114 \r
115     /* True if this is the special-cased plain text printer.\r
116      */\r
117     notmuch_bool_t is_text_printer;\r
118 \r
119 The printer can (and should) use internal state to insert delimiters\r
120 and syntax at the correct places.\r
121 \r
122 Example:\r
123 \r
124 format->begin_map(format);\r
125 format->map_key(format, "foo");\r
126 format->begin_list(format);\r
127 format->integer(format, 1);\r
128 format->integer(format, 2);\r
129 format->integer(format, 3);\r
130 format->end(format);\r
131 format->map_key(format, "bar");\r
132 format->begin_map(format);\r
133 format->map_key(format, "baaz");\r
134 format->string(format, "hello world");\r
135 format->end(format);\r
136 format->end(format);\r
137 \r
138 would output JSON as follows:\r
139 \r
140 {"foo": [1, 2, 3], "bar": { "baaz": "hello world"}}\r
141 ---\r
142  sprinter.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
143  1 file changed, 58 insertions(+)\r
144 \r
145 diff --git a/sprinter.h b/sprinter.h\r
146 new file mode 100644\r
147 index 0000000..77dc26f\r
148 --- /dev/null\r
149 +++ b/sprinter.h\r
150 @@ -0,0 +1,58 @@\r
151 +#ifndef NOTMUCH_SPRINTER_H\r
152 +#define NOTMUCH_SPRINTER_H\r
153 +\r
154 +/* Necessary for notmuch_bool_t */\r
155 +#include "notmuch-client.h"\r
156 +\r
157 +/* Structure printer interface. This is used to create output\r
158 + * structured as maps (with key/value pairs), lists and primitives\r
159 + * (strings, integers and booleans).\r
160 + */\r
161 +typedef struct sprinter {\r
162 +    /* Start a new map/dictionary structure. This should be followed by\r
163 +     * a sequence of alternating calls to map_key and one of the\r
164 +     * value-printing functions until the map is ended by end.\r
165 +     */\r
166 +    void (*begin_map) (struct sprinter *);\r
167 +\r
168 +    /* Start a new list/array structure.\r
169 +     */\r
170 +    void (*begin_list) (struct sprinter *);\r
171 +\r
172 +    /* End the last opened list or map structure.\r
173 +     */\r
174 +    void (*end) (struct sprinter *);\r
175 +\r
176 +    /* Print one string/integer/boolean/null element (possibly inside a\r
177 +     * list or map, followed or preceded by separators).\r
178 +     * For string, the char * must be UTF-8 encoded.\r
179 +     */\r
180 +    void (*string) (struct sprinter *, const char *);\r
181 +    void (*integer) (struct sprinter *, int);\r
182 +    void (*boolean) (struct sprinter *, notmuch_bool_t);\r
183 +    void (*null) (struct sprinter *);\r
184 +\r
185 +    /* Print the key of a map's key/value pair. The char * must be UTF-8\r
186 +     * encoded.\r
187 +     */\r
188 +    void (*map_key) (struct sprinter *, const char *);\r
189 +\r
190 +    /* Insert a separator (usually extra whitespace) for improved\r
191 +     * readability without affecting the abstract syntax of the\r
192 +     * structure being printed.\r
193 +     * For JSON, this could simply be a line break.\r
194 +     */\r
195 +    void (*separator) (struct sprinter *);\r
196 +\r
197 +    /* Set the current string prefix. This only affects the text\r
198 +     * printer, which will print this string, followed by a colon,\r
199 +     * before any string. For other printers, this does nothing.\r
200 +     */\r
201 +    void (*set_prefix) (struct sprinter *, const char *);\r
202 +\r
203 +    /* True if this is the special-cased plain text printer.\r
204 +     */\r
205 +    notmuch_bool_t is_text_printer;\r
206 +} sprinter_t;\r
207 +\r
208 +#endif // NOTMUCH_SPRINTER_H\r
209 -- \r
210 1.7.11.2\r
211 \r