-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSuffixGenerator.mjs
executable file
·157 lines (145 loc) · 4.43 KB
/
SuffixGenerator.mjs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
import {readFileSync, writeFileSync} from 'fs';
import {toFormattedSwitchCase} from './commonUtility.mjs';
/** @type {Array<string>} */
const suffixes = JSON.parse(readFileSync('./Suffixes.json').toString());
/** @type {{caseName:string,value:string}} */
const records = suffixes
.map(ext => ({
'caseName': ext
.slice(1)
.replace(/-(.)/g, (_, m) => m.toUpperCase()),
'value': ext
}));
const code = `import Foundation
/// Represents the media type [suffix](https://en.wikipedia.org/wiki/Media_type#Suffix)es.
///
/// The library allows all \`\`MediaType\`\`s to have a suffix. Keep in mind though, that not all such combinations are
/// registered (see the [official site](https://www.iana.org/assignments/media-types/media-types.xhtml) for details).
///
/// It is also possible to create completely custom suffixes by using either the \`\`Suffix/other(_:)\`\` case directly or a
/// string literal.
///
/// \`\`\`swift
/// MediaType.application(.jose(.json)) // Creates: application/jose+json
/// MediaType.application(.jose(.other("custom"))) // Creates: application/jose+custom
/// MediaType.image(.svg("zip")) // Creates: image/svg+zip
/// \`\`\`
///
/// You can access a media type's suffix by matching using a \`switch\` statement:
///
/// \`\`\`swift
/// let mediaType = MediaType.application(.calendar(.xml))
/// switch mediaType {
/// case .application(.calendar(let suffix, _)):
/// guard let suffix = suffix else { break }
/// print("Suffix: \\(suffix)", "Suffix: +xml")
/// default:
/// print("Unsupported media type: \\(mediaType)")
/// }
/// \`\`\`
public enum Suffix {
${
records
.map(({caseName, value}) => ` /// Represents the \`${value.substring(1)}\` suffix.
case ${caseName}`)
.join('\n')
}
/// Represents a custom suffix.
case other(CustomStringConvertible)
}
extension Suffix: RawRepresentable {
public init(rawValue: String) {
let rawValue = rawValue.starts(with: "+")
? String(rawValue.split(separator: "+", maxSplits: 1).last!)
: rawValue
switch (rawValue) {
${
records
.map(({caseName, value}) => [
` case "${value.replace(/^\+/, '')}": `,
`self = .${caseName}`
])
.sort(([a], [b]) => a.charCodeAt(0) - b.charCodeAt(0))
.concat([[
` default: `,
`self = .other(rawValue)`
]])
.map(toFormattedSwitchCase)
.join('\n')
}
}
}
public var rawValue: String {
switch self {
${
records
.map(({caseName, value}) => [
` case .${caseName}:`,
`return "${value}"`
])
.sort(([a], [b]) => a.charCodeAt(0) - b.charCodeAt(0))
.concat([[
" case .other(let value): ",
`return value.description.starts(with: "+") ? value.description : "+\\(value)"`
]])
.map(toFormattedSwitchCase)
.join('\n')
}
}
}
}
extension Suffix: CustomStringConvertible { public var description: String { rawValue } }
extension Optional: CustomStringConvertible where Wrapped == Suffix {
public var description: String {
switch self {
case .none: return ""
case .some(let value): return value.description
}
}
}
extension Suffix: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(rawValue: value)
}
}
extension Suffix: Hashable {
public static func ==(lhs: Self, rhs: Self) -> Bool {
switch lhs {
${
records
.map(({caseName}) => [
` case .${caseName}: `,
`if case .${caseName} = rhs { return true } else { return false }`
])
.sort(([a], [b]) => a.charCodeAt(0) - b.charCodeAt(0))
.concat([[
` case .other(let lhsSuffix): `,
`if case let .other(rhsSuffix) = rhs { return lhsSuffix.description == rhsSuffix.description } else { return false }`
]])
.map(toFormattedSwitchCase)
.join('\n')
}
}
}
public func hash(into hasher: inout Hasher) {
switch self {
${
records
.map(({caseName}, i) => [
` case .${caseName}: `,
`hasher.combine(${i})`
])
.sort(([a], [b]) => a.charCodeAt(0) - b.charCodeAt(0))
.concat([[
` case .other(let other): `,
`hasher.combine(other.description)`
]])
.map(toFormattedSwitchCase)
.join('\n')
}
}
}
}
`
writeFileSync('Suffix.swift', code);