Skip to content

Instantly share code, notes, and snippets.

@blixt
Last active May 19, 2018 10:42
Show Gist options
  • Save blixt/cedd25e91dcf5a45a8d0 to your computer and use it in GitHub Desktop.
Save blixt/cedd25e91dcf5a45a8d0 to your computer and use it in GitHub Desktop.
Using indirect enums to create a simplified XML generator in just a few lines of code
indirect enum Node {
case tag(String, [Node])
case text(String)
}
extension Node: CustomStringConvertible {
var description: String {
switch self {
case let .tag(name, children):
if children.count == 1, case let .some(.text(text)) = children.first {
return "<\(name)>\(text)</\(name)>"
}
let content = children
.flatMap { $0.description.split(separator: "\n") }
.map { " \($0)" }
return "<\(name)>\n\(content.joined(separator: "\n"))\n</\(name)>"
case let .text(text):
return text
}
}
}
let document: Node = .tag("html", [
.tag("head", [
.tag("title", [.text("Example")]),
]),
.tag("body", [
.tag("h1", [.text("Hello World!")]),
.tag("p", [.text("Welcome to this site. Here are my favorite things:")]),
.tag("ul", [
.tag("li", [.text("Burgers")]),
.tag("li", [.text("Computers")]),
.tag("li", [.text("Ice cream")]),
]),
]),
])
print(document)
/* Prints:
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>Hello World!</h1>
<p>Welcome to this site. Here are my favorite things:</p>
<ul>
<li>Burgers</li>
<li>Computers</li>
<li>Ice cream</li>
</ul>
</body>
</html>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment