-
Notifications
You must be signed in to change notification settings - Fork 104
/
DecimalBox.swift
61 lines (52 loc) · 1.74 KB
/
DecimalBox.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
53
54
55
56
57
58
59
60
61
//
// DecimalBox.swift
// XMLCoder
//
// Created by Vincent Esche on 12/17/18.
//
import Foundation
struct DecimalBox: Equatable {
typealias Unboxed = Decimal
let unboxed: Unboxed
init(_ unboxed: Unboxed) {
self.unboxed = unboxed
}
init?(xmlString: String) {
guard let unboxed = Unboxed(string: xmlString) else {
return nil
}
self.init(unboxed)
}
}
extension DecimalBox: Box {
var isNull: Bool {
return false
}
/// # Lexical representation
/// Decimal has a lexical representation consisting of a finite-length sequence of
/// decimal digits separated by a period as a decimal indicator.
/// An optional leading sign is allowed. If the sign is omitted, `"+"` is assumed.
/// Leading and trailing zeroes are optional. If the fractional part is zero,
/// the period and following zero(es) can be omitted.
/// For example: `-1.23`, `12678967.543233`, `+100000.00`, `210`.
///
/// # Canonical representation
/// The canonical representation for decimal is defined by prohibiting certain
/// options from the Lexical representation. Specifically, the preceding optional
/// `"+"` sign is prohibited. The decimal point is required. Leading and trailing
/// zeroes are prohibited subject to the following: there must be at least one
/// digit to the right and to the left of the decimal point which may be a zero.
///
/// ---
///
/// [Schema definition](https://www.w3.org/TR/xmlschema-2/#decimal)
func xmlString() -> String? {
return "\(unboxed)"
}
}
extension DecimalBox: SimpleBox {}
extension DecimalBox: CustomStringConvertible {
var description: String {
return unboxed.description
}
}