Skip to content

Commit 08a8e60

Browse files
committed
A little bit of refactoring
...
1 parent da13ba9 commit 08a8e60

File tree

4 files changed

+117
-38
lines changed

4 files changed

+117
-38
lines changed

Sources/MacroXmlRpc/RouteKeeper.swift

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,3 @@ public extension RouteKeeper {
2121
post(xmlrpc.synchronousCall(methodName, execute: execute))
2222
}
2323
}
24-
25-
public extension RouteKeeper {
26-
27-
@inlinable
28-
@discardableResult
29-
func rpc<A1, R>(_ methodName: String, execute: @escaping ( A1 ) throws -> R)
30-
-> Self
31-
where A1 : XmlRpcValueRepresentable,
32-
R : XmlRpcValueRepresentable
33-
{
34-
rpc(methodName) { call in
35-
guard call.parameters.count == 1,
36-
let a1 = A1(xmlRpcValue: call.parameters[0])
37-
else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") }
38-
return try execute(a1)
39-
}
40-
}
41-
42-
@inlinable
43-
@discardableResult
44-
func rpc<A1, A2, R>(_ methodName: String,
45-
execute: @escaping ( A1, A2 ) throws -> R)
46-
-> Self
47-
where A1 : XmlRpcValueRepresentable,
48-
A2 : XmlRpcValueRepresentable,
49-
R : XmlRpcValueRepresentable
50-
{
51-
rpc(methodName) { call in
52-
guard call.parameters.count == 2,
53-
let a1 = A1(xmlRpcValue: call.parameters[0]),
54-
let a2 = A2(xmlRpcValue: call.parameters[1])
55-
else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") }
56-
return try execute(a1, a2)
57-
}
58-
}
59-
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//
2+
// TypedRoutes.swift
3+
// MacroXmlRpc
4+
//
5+
// Created by Helge Hess.
6+
// Copyright © 2020 ZeeZide GmbH. All rights reserved.
7+
//
8+
9+
import MacroExpress
10+
import XmlRpc
11+
12+
public protocol IntrospectibleXmlRpcValue: XmlRpcValueRepresentable {
13+
static var xmlRpcValueType : XmlRpc.Value.ValueType { get }
14+
}
15+
16+
public extension RouteKeeper {
17+
18+
// TODO: To finish up introspection, this needs to be decoupled from
19+
// `synchronousCall`.
20+
21+
@inlinable
22+
@discardableResult
23+
func rpc<A1, R>(_ methodName: String, execute: @escaping ( A1 ) throws -> R)
24+
-> Self
25+
where A1 : IntrospectibleXmlRpcValue,
26+
R : IntrospectibleXmlRpcValue
27+
{
28+
post(xmlrpc.synchronousCall(methodName) { call in
29+
guard call.parameters.count == 1,
30+
let a1 = A1(xmlRpcValue: call.parameters[0])
31+
else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") }
32+
return try execute(a1)
33+
})
34+
}
35+
36+
@inlinable
37+
@discardableResult
38+
func rpc<A1, A2, R>(_ methodName: String,
39+
execute: @escaping ( A1, A2 ) throws -> R)
40+
-> Self
41+
where A1 : IntrospectibleXmlRpcValue,
42+
A2 : IntrospectibleXmlRpcValue,
43+
R : IntrospectibleXmlRpcValue
44+
{
45+
post(xmlrpc.synchronousCall(methodName) { call in
46+
guard call.parameters.count == 2,
47+
let a1 = A1(xmlRpcValue: call.parameters[0]),
48+
let a2 = A2(xmlRpcValue: call.parameters[1])
49+
else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") }
50+
return try execute(a1, a2)
51+
})
52+
}
53+
}
54+
55+
56+
// MARK: - IntrospectibleXmlRpcValue types
57+
58+
extension String: IntrospectibleXmlRpcValue {
59+
@inlinable
60+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .string }
61+
}
62+
63+
extension Int: IntrospectibleXmlRpcValue {
64+
@inlinable
65+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .int }
66+
}
67+
68+
extension Double: IntrospectibleXmlRpcValue {
69+
@inlinable
70+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .double }
71+
}
72+
73+
extension Bool: IntrospectibleXmlRpcValue {
74+
@inlinable
75+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .bool }
76+
}
77+
78+
extension Collection where Element : IntrospectibleXmlRpcValue {
79+
@inlinable
80+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .array }
81+
}
82+
83+
extension Array : IntrospectibleXmlRpcValue
84+
where Element : IntrospectibleXmlRpcValue
85+
{
86+
}
87+
extension Set : IntrospectibleXmlRpcValue
88+
where Element : IntrospectibleXmlRpcValue
89+
{
90+
}
91+
92+
extension Dictionary : IntrospectibleXmlRpcValue
93+
where Key == String,
94+
Value : IntrospectibleXmlRpcValue
95+
{
96+
@inlinable
97+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .dictionary }
98+
}
99+
100+
#if canImport(Foundation)
101+
import struct Foundation.URL
102+
import struct Foundation.DateComponents
103+
104+
extension DateComponents: IntrospectibleXmlRpcValue {
105+
@inlinable
106+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .dateTime }
107+
}
108+
109+
extension URL: IntrospectibleXmlRpcValue {
110+
@inlinable
111+
public static var xmlRpcValueType : XmlRpc.Value.ValueType { .string }
112+
}
113+
#endif // canImport(Foundation)

Sources/MacroXmlRpc/ValueType.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ import protocol XmlRpc.XmlRpcValueRepresentable
1111
public extension XmlRpc.Value {
1212

1313
/**
14-
* The various possible XML-RPC value types.
14+
* The various possible XML-RPC value types used in XML-RPC introspection.
15+
* Note that those are flat, i.e. arrays and dictionaries are not further
16+
* described.
1517
*/
1618
@frozen
1719
enum ValueType: Hashable {
1820
case null
1921
case string, bool, int, double, dateTime, data
2022
case array, dictionary
2123
}
22-
24+
2325
@inlinable
2426
var xmlRpcValueType: ValueType {
2527
switch self {

TODO.md

Whitespace-only changes.

0 commit comments

Comments
 (0)