-
Notifications
You must be signed in to change notification settings - Fork 104
/
BoolBox.swift
52 lines (44 loc) · 1.14 KB
/
BoolBox.swift
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
//
// BoolBox.swift
// XMLCoder
//
// Created by Vincent Esche on 12/17/18.
//
struct BoolBox: Equatable {
typealias Unboxed = Bool
let unboxed: Unboxed
init(_ unboxed: Unboxed) {
self.unboxed = unboxed
}
init?(xmlString: String) {
switch xmlString.lowercased() {
case "false", "0", "n", "no": self.init(false)
case "true", "1", "y", "yes": self.init(true)
case _: return nil
}
}
}
extension BoolBox: Box {
var isNull: Bool {
return false
}
/// # Lexical representation
/// Boolean has a lexical representation consisting of the following
/// legal literals {`true`, `false`, `1`, `0`}.
///
/// # Canonical representation
/// The canonical representation for boolean is the set of literals {`true`, `false`}.
///
/// ---
///
/// [Schema definition](https://www.w3.org/TR/xmlschema-2/#boolean)
func xmlString() -> String? {
return (unboxed) ? "true" : "false"
}
}
extension BoolBox: SimpleBox {}
extension BoolBox: CustomStringConvertible {
var description: String {
return unboxed.description
}
}