-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStringFormat.swift
More file actions
96 lines (77 loc) · 2.8 KB
/
StringFormat.swift
File metadata and controls
96 lines (77 loc) · 2.8 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public enum StringFormat: RawRepresentable, Codable {
public typealias RawValue = String
/// Base64 encoded characters.
case byte
/// Any sequence of octets.
case binary
/// "full-date" representation, see RFC 3339, section 5.6.
case date
/// "date-time" representation, see RFC 3339, section 5.6.
case dateTime
/// Internet email address, see RFC 5322, section 3.4.1.
case email
/// Internet host name, see RFC 1034, section 3.1.
case hostname
/// IPv4 address, according to dotted-quad ABNF syntax as defined in RFC 2673, section 3.2.
case ipv4
/// IPv6 address, as defined in RFC 2373, section 2.2.
case ipv6
/// Used to hint UIs the input needs to be obscured.
case password
/// A custom format
case other(String)
/// A universal resource identifier (URI), according to RFC3986.
case uri
public init(from decoder: Decoder) throws {
let value = try decoder.singleValueContainer()
if let rawFormat = try? value.decode(RawStringFormat.self) {
self = rawFormat.stringFormat
return
}
self = .other(try value.decode(String.self))
}
public init(rawValue: String) {
self = RawStringFormat(rawValue: rawValue)?.stringFormat ?? .other(rawValue)
}
public var rawValue: String {
switch self {
case .byte: return RawStringFormat.byte.rawValue
case .binary: return RawStringFormat.binary.rawValue
case .date: return RawStringFormat.date.rawValue
case .dateTime: return RawStringFormat.dateTime.rawValue
case .email: return RawStringFormat.email.rawValue
case .hostname: return RawStringFormat.hostname.rawValue
case .ipv4: return RawStringFormat.ipv4.rawValue
case .ipv6: return RawStringFormat.ipv6.rawValue
case .password: return RawStringFormat.password.rawValue
case .uri: return RawStringFormat.uri.rawValue
case .other(let other): return other
}
}
private enum RawStringFormat: String, Codable {
case byte
case binary
case date
case dateTime = "date-time"
case email
case hostname
case ipv4
case ipv6
case password
case uri
var stringFormat: StringFormat {
switch self {
case .byte: return .byte
case .binary: return .binary
case .date: return.date
case .dateTime: return .dateTime
case .email: return .email
case .hostname: return .hostname
case .ipv4: return .ipv4
case .ipv6: return .ipv6
case .password: return .password
case .uri: return .uri
}
}
}
}