11import Foundation
22
33extension String : XMLValueCodable {
4- public var xmlStringValue : String { self }
5- public init ( xmlStringValue: String ) throws { self = xmlStringValue }
4+ public func xmlStringValue( for node : Node ) -> String { self }
5+ public init ( xmlStringValue: String , for node : Node ) throws { self = xmlStringValue }
66}
77
88extension Bool : XMLValueCodable {
9- public var xmlStringValue : String { self ? " true " : " false " }
9+ public func xmlStringValue( for node : Node ) -> String { self ? " true " : " false " }
1010
11- public init ( xmlStringValue: String ) throws {
11+ public init ( xmlStringValue: String , for node : Node ) throws {
1212 switch xmlStringValue {
1313 case " true " , " 1 " : self = true
1414 case " false " , " 0 " : self = false
@@ -18,9 +18,9 @@ extension Bool: XMLValueCodable {
1818}
1919
2020extension Double : XMLValueCodable {
21- public var xmlStringValue : String { String ( self ) }
21+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
2222
23- public init ( xmlStringValue: String ) throws {
23+ public init ( xmlStringValue: String , for node : Node ) throws {
2424 guard let double = Double ( xmlStringValue) else {
2525 throw XMLValueError . invalidFormat ( expected: " Double " , found: xmlStringValue)
2626 }
@@ -29,9 +29,9 @@ extension Double: XMLValueCodable {
2929}
3030
3131extension Float : XMLValueCodable {
32- public var xmlStringValue : String { String ( self ) }
32+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
3333
34- public init ( xmlStringValue: String ) throws {
34+ public init ( xmlStringValue: String , for node : Node ) throws {
3535 guard let float = Float ( xmlStringValue) else {
3636 throw XMLValueError . invalidFormat ( expected: " Float " , found: xmlStringValue)
3737 }
@@ -40,9 +40,9 @@ extension Float: XMLValueCodable {
4040}
4141
4242extension Int : XMLValueCodable {
43- public var xmlStringValue : String { String ( self ) }
43+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
4444
45- public init ( xmlStringValue: String ) throws {
45+ public init ( xmlStringValue: String , for node : Node ) throws {
4646 guard let int = Int ( xmlStringValue) else {
4747 throw XMLValueError . invalidFormat ( expected: " Int " , found: xmlStringValue)
4848 }
@@ -51,9 +51,9 @@ extension Int: XMLValueCodable {
5151}
5252
5353extension Int8 : XMLValueCodable {
54- public var xmlStringValue : String { String ( self ) }
54+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
5555
56- public init ( xmlStringValue: String ) throws {
56+ public init ( xmlStringValue: String , for node : Node ) throws {
5757 guard let value = Int8 ( xmlStringValue) else {
5858 throw XMLValueError . invalidFormat ( expected: " Int8 " , found: xmlStringValue)
5959 }
@@ -62,9 +62,9 @@ extension Int8: XMLValueCodable {
6262}
6363
6464extension Int16 : XMLValueCodable {
65- public var xmlStringValue : String { String ( self ) }
65+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
6666
67- public init ( xmlStringValue: String ) throws {
67+ public init ( xmlStringValue: String , for node : Node ) throws {
6868 guard let value = Int16 ( xmlStringValue) else {
6969 throw XMLValueError . invalidFormat ( expected: " Int16 " , found: xmlStringValue)
7070 }
@@ -73,9 +73,9 @@ extension Int16: XMLValueCodable {
7373}
7474
7575extension Int32 : XMLValueCodable {
76- public var xmlStringValue : String { String ( self ) }
76+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
7777
78- public init ( xmlStringValue: String ) throws {
78+ public init ( xmlStringValue: String , for node : Node ) throws {
7979 guard let value = Int32 ( xmlStringValue) else {
8080 throw XMLValueError . invalidFormat ( expected: " Int32 " , found: xmlStringValue)
8181 }
@@ -84,9 +84,9 @@ extension Int32: XMLValueCodable {
8484}
8585
8686extension Int64 : XMLValueCodable {
87- public var xmlStringValue : String { String ( self ) }
87+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
8888
89- public init ( xmlStringValue: String ) throws {
89+ public init ( xmlStringValue: String , for node : Node ) throws {
9090 guard let value = Int64 ( xmlStringValue) else {
9191 throw XMLValueError . invalidFormat ( expected: " Int64 " , found: xmlStringValue)
9292 }
@@ -95,9 +95,9 @@ extension Int64: XMLValueCodable {
9595}
9696
9797extension UInt : XMLValueCodable {
98- public var xmlStringValue : String { String ( self ) }
98+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
9999
100- public init ( xmlStringValue: String ) throws {
100+ public init ( xmlStringValue: String , for node : Node ) throws {
101101 guard let value = UInt ( xmlStringValue) else {
102102 throw XMLValueError . invalidFormat ( expected: " UInt " , found: xmlStringValue)
103103 }
@@ -106,9 +106,9 @@ extension UInt: XMLValueCodable {
106106}
107107
108108extension UInt8 : XMLValueCodable {
109- public var xmlStringValue : String { String ( self ) }
109+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
110110
111- public init ( xmlStringValue: String ) throws {
111+ public init ( xmlStringValue: String , for node : Node ) throws {
112112 guard let value = UInt8 ( xmlStringValue) else {
113113 throw XMLValueError . invalidFormat ( expected: " UInt8 " , found: xmlStringValue)
114114 }
@@ -117,9 +117,9 @@ extension UInt8: XMLValueCodable {
117117}
118118
119119extension UInt16 : XMLValueCodable {
120- public var xmlStringValue : String { String ( self ) }
120+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
121121
122- public init ( xmlStringValue: String ) throws {
122+ public init ( xmlStringValue: String , for node : Node ) throws {
123123 guard let value = UInt16 ( xmlStringValue) else {
124124 throw XMLValueError . invalidFormat ( expected: " UInt16 " , found: xmlStringValue)
125125 }
@@ -128,9 +128,9 @@ extension UInt16: XMLValueCodable {
128128}
129129
130130extension UInt32 : XMLValueCodable {
131- public var xmlStringValue : String { String ( self ) }
131+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
132132
133- public init ( xmlStringValue: String ) throws {
133+ public init ( xmlStringValue: String , for node : Node ) throws {
134134 guard let value = UInt32 ( xmlStringValue) else {
135135 throw XMLValueError . invalidFormat ( expected: " UInt32 " , found: xmlStringValue)
136136 }
@@ -139,9 +139,9 @@ extension UInt32: XMLValueCodable {
139139}
140140
141141extension UInt64 : XMLValueCodable {
142- public var xmlStringValue : String { String ( self ) }
142+ public func xmlStringValue( for node : Node ) -> String { String ( self ) }
143143
144- public init ( xmlStringValue: String ) throws {
144+ public init ( xmlStringValue: String , for node : Node ) throws {
145145 guard let value = UInt64 ( xmlStringValue) else {
146146 throw XMLValueError . invalidFormat ( expected: " UInt64 " , found: xmlStringValue)
147147 }
@@ -150,25 +150,56 @@ extension UInt64: XMLValueCodable {
150150}
151151
152152extension UUID : XMLValueCodable {
153- public var xmlStringValue : String { uuidString }
153+ public func xmlStringValue( for node : Node ) -> String { uuidString }
154154
155- public init ( xmlStringValue: String ) throws {
155+ public init ( xmlStringValue: String , for node : Node ) throws {
156156 guard let value = UUID ( uuidString: xmlStringValue) else {
157157 throw XMLValueError . invalidFormat ( expected: " UUID " , found: xmlStringValue)
158158 }
159159 self = value
160160 }
161161}
162162
163+ extension URL : XMLValueCodable {
164+ public func xmlStringValue( for node: Node ) -> String { absoluteString }
165+
166+ public init ( xmlStringValue: String , for node: Node ) throws {
167+ guard let url = URL ( string: xmlStringValue) else {
168+ throw XMLValueError . invalidFormat ( expected: " URL " , found: xmlStringValue)
169+ }
170+ self = url
171+ }
172+ }
173+
174+ extension Date : XMLValueCodable {
175+ public func xmlStringValue( for node: Node ) -> String {
176+ node. document. dateFormat. encode ( self )
177+ }
178+
179+ public init ( xmlStringValue: String , for node: Node ) throws {
180+ self = try node. document. dateFormat. decode ( xmlStringValue)
181+ }
182+ }
183+
184+ extension Data : XMLValueCodable {
185+ public func xmlStringValue( for node: Node ) -> String {
186+ node. document. dataFormat. encode ( self )
187+ }
188+
189+ public init ( xmlStringValue: String , for node: Node ) throws {
190+ self = try node. document. dataFormat. decode ( xmlStringValue)
191+ }
192+ }
193+
163194extension RawRepresentable where RawValue: XMLValueEncodable {
164- public var xmlStringValue : String {
165- rawValue. xmlStringValue
195+ public func xmlStringValue( for node : Node ) -> String {
196+ rawValue. xmlStringValue ( for : node )
166197 }
167198}
168199
169200extension RawRepresentable where RawValue: XMLValueDecodable {
170- public init ( xmlStringValue string: String ) throws {
171- let raw = try RawValue ( xmlStringValue: string)
201+ public init ( xmlStringValue string: String , for node : Node ) throws {
202+ let raw = try RawValue ( xmlStringValue: string, for : node )
172203 guard let value = Self ( rawValue: raw) else {
173204 throw XMLValueError . invalidFormat ( expected: " \( String ( describing: Self . self) ) " , found: string)
174205 }
0 commit comments