-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml_builder.gleam
355 lines (314 loc) · 10.2 KB
/
xml_builder.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
////Goals for this module:
//// 1. make xml like the string_builder
//// ie. xml_builder.new()
//// |> xml_builder.block_tag("name", {
//// xml_builder.new()
//// |> xml_builder.tag("hello", "world")
//// |> xml_builder.tag("maybe", "here")})
//// |> xml_builder.option_tag("link", Opt.("href", "https://example.com"))
//// To:
//// <?xml version="1.0" encoding="UTF-8" xml?>
//// <name>
//// <hello> world </hello>
//// </name>
import gleam/bool
import gleam/list
import gleam/result
import gleam/string
import gleam/string_builder.{append, append_builder}
pub type BuilderError {
ContentsEmpty
LabelEmpty
OptionsEmpty
VersionEmpty
EncodingEmpty
TagPlacedBeforeNew
InnerEmpty
EmptyDocument
NOTAPPLICABLE
}
pub type Option {
Opt(label: String, value: String)
}
pub type XmlBuilder =
Result(string_builder.StringBuilder, BuilderError)
/// starts the builder and assumes version 1.0 and encoding UTF-8,
/// if you need specific verisons or encoding
/// use new_advanced_document(version, encoding)
pub fn new_document() -> XmlBuilder {
string_builder.new()
|> string_builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|> Ok
}
/// starts the builder and
/// allows you to put in your own version and encoding
pub fn new_advanced_document(version: String, encoding: String) -> XmlBuilder {
let version_empty = string.is_empty(version)
use <- bool.guard(when: version_empty, return: Error(VersionEmpty))
let encoding_empty = string.is_empty(encoding)
use <- bool.guard(when: encoding_empty, return: Error(EncodingEmpty))
string_builder.new()
|> string_builder.append("<?xml version=\"")
|> string_builder.append(version)
|> string_builder.append("\" encoding=\"")
|> string_builder.append(encoding)
|> string_builder.append("\"?> \n")
|> Ok
}
/// starts the blocks inside of tags because of the requirement
/// of document and not having be optional
pub fn new() -> XmlBuilder {
string_builder.new()
|> Ok
}
/// Basic tag that takes in a label and contents and a
/// document in the form of an XmlBuilder
/// this is intended to be used in a pipe chain
/// ie. new_document()
/// |> tag("hello", "world")
/// Throws an error if anything is left blank
pub fn tag(document: XmlBuilder, label: String, contents: String) -> XmlBuilder {
let label_empty = string.is_empty(label)
use <- bool.guard(when: label_empty, return: Error(LabelEmpty))
let contents_empty = string.is_empty(contents)
use <- bool.guard(when: contents_empty, return: Error(ContentsEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<")
|> append(label)
|> append("> ")
|> append(contents)
|> append(" </")
|> append(label)
|> append("> \n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
pub fn cdata_tag(document: XmlBuilder, label: String, contents: String) {
let label_empty = string.is_empty(label)
use <- bool.guard(when: label_empty, return: Error(LabelEmpty))
let contents_empty = string.is_empty(contents)
use <- bool.guard(when: contents_empty, return: Error(ContentsEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<")
|> append(label)
|> append("> \n")
|> append("<![CDATA[\n \t")
|> append(contents)
|> append("\n]]> \n")
|> append("</")
|> append(label)
|> append("> \n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
/// Tag with options and content
/// ie. <hello world="hi"> ?? <hello>
pub fn option_content_tag(
document: XmlBuilder,
label: String,
options: List(Option),
contents: String,
) {
let label_empty = string.is_empty(label)
use <- bool.guard(when: label_empty, return: Error(LabelEmpty))
let contents_empty = string.is_empty(contents)
use <- bool.guard(when: contents_empty, return: Error(ContentsEmpty))
let options_empty = list.is_empty(options)
use <- bool.guard(when: options_empty, return: Error(OptionsEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<")
|> append(label)
|> append_builder(string_options(options))
|> append("> ")
|> append(contents)
|> append(" </")
|> append(label)
|> append("> \n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
///Tag with options that self-closes
/// ie. <link href="https://example.com" />
pub fn option_tag(document: XmlBuilder, label: String, options: List(Option)) {
let label_empty = string.is_empty(label)
use <- bool.guard(when: label_empty, return: Error(LabelEmpty))
let options_empty = list.is_empty(options)
use <- bool.guard(when: options_empty, return: Error(OptionsEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<")
|> append(label)
|> append_builder(string_options(options))
|> append(" />\n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
fn string_options(options: List(Option)) {
list.map(options, option_to_string)
|> string_builder.concat
}
fn option_to_string(option: Option) {
string_builder.from_strings([" ", option.label, "=\"", option.value, "\""])
}
/// Starts a block which is a tag with other tags inside of it
/// ie. <owner>
/// <email>example@example.com</email>
/// </owner>
///
/// Usage: |>block_tag("owner", {
/// new()
/// |> tag("email", "example@example.com")
/// })
pub fn block_tag(document: XmlBuilder, label: String, inner: XmlBuilder) {
let label_empty = string.is_empty(label)
use <- bool.guard(when: label_empty, return: Error(LabelEmpty))
let inner_empty =
string_builder.is_empty(result.unwrap(inner, string_builder.new()))
use <- bool.guard(when: inner_empty, return: Error(InnerEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
case result.is_error(inner) {
True -> Error(result.unwrap_error(inner, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<")
|> append(label)
|> append("> \n")
|> append_builder(result.unwrap(inner, string_builder.new()))
|> append("</")
|> append(label)
|> append("> \n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
}
/// block tag that also has options
pub fn option_block_tag(
document: XmlBuilder,
label: String,
options: List(Option),
inner: XmlBuilder,
) {
let label_empty = string.is_empty(label)
use <- bool.guard(when: label_empty, return: Error(LabelEmpty))
let inner_empty =
string_builder.is_empty(result.unwrap(inner, string_builder.new()))
use <- bool.guard(when: inner_empty, return: Error(InnerEmpty))
let options_empty = list.is_empty(options)
use <- bool.guard(when: options_empty, return: Error(OptionsEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
case result.is_error(inner) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<")
|> append(label)
|> append_builder(string_options(options))
|> append("> \n")
|> append_builder(result.unwrap(inner, string_builder.new()))
|> append(" </")
|> append(label)
|> append("> \n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
}
///This a comment function, works the same way as any other tag
/// But makes whatever you put in as a string a comment
pub fn comment(document: XmlBuilder, comment: String) -> XmlBuilder {
let comments_empty = string.is_empty(comment)
use <- bool.guard(when: comments_empty, return: Error(ContentsEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<!-- ")
|> append(comment)
|> append(" --> \n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
///This is a funciton for a block comment, works like block_tag
/// but does not take a label
pub fn block_comment(document: XmlBuilder, inner: XmlBuilder) {
let inner_empty =
string_builder.is_empty(result.unwrap(inner, string_builder.new()))
use <- bool.guard(when: inner_empty, return: Error(InnerEmpty))
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
case result.is_error(inner) {
True -> Error(result.unwrap_error(inner, NOTAPPLICABLE))
False ->
string_builder.new()
|> append("<!-- \n")
|> append_builder(result.unwrap(inner, string_builder.new()))
|> append("--> \n")
|> append_builder(
to: result.unwrap(document, string_builder.new()),
suffix: _,
)
|> Ok
}
}
}
/// Ends the XML document
/// takes in the XML Document and outputs
/// a Result(String, BuilderError)
pub fn end_xml(document: XmlBuilder) -> Result(String, BuilderError) {
case result.is_error(document) {
True -> Error(result.unwrap_error(document, NOTAPPLICABLE))
False ->
case
string_builder.is_empty(result.unwrap(document, string_builder.new()))
{
True -> Error(EmptyDocument)
False ->
result.unwrap(document, string_builder.new())
|> string_builder.to_string
|> Ok
}
}
}