Skip to content

Commit dfc845f

Browse files
committed
Throw an error if there are too few words
1 parent 0f84997 commit dfc845f

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

Diff for: Sources/D2Commands/Fun/BuzzwordPhraseCommand.swift

+41-14
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,51 @@ public class BuzzwordPhraseCommand: StringCommand {
3939
private struct Generator {
4040
var corpus: Corpus
4141

42-
mutating func phrase(adjectives: Int = 1, nouns: Int = 2) -> String {
43-
((0..<adjectives).map { _ in adjective() } + (0..<nouns).map { _ in noun() }).joined(separator: " ")
42+
private enum GenerationError: Error {
43+
case noMoreNouns
44+
case noMoreCompoundPrefixes
45+
case noMoreCompoundSuffixes
46+
case noMoreAdjectives
4447
}
4548

46-
private mutating func noun() -> String {
47-
corpus.nouns.removeRandomElementBySwap() ?? ""
49+
mutating func phrase(adjectives: Int = 1, nouns: Int = 2) throws -> String {
50+
try ((0..<adjectives).map { _ in try adjective() } + (0..<nouns).map { _ in try noun() }).joined(separator: " ")
4851
}
4952

50-
private mutating func compoundPrefix() -> String {
51-
corpus.compoundPrefixes.removeRandomElementBySwap() ?? ""
53+
private mutating func noun() throws -> String {
54+
guard let noun = corpus.nouns.removeRandomElementBySwap() else {
55+
throw GenerationError.noMoreNouns
56+
}
57+
return noun
5258
}
5359

54-
private mutating func compoundSuffix() -> String {
55-
corpus.compoundSuffixes.removeRandomElementBySwap() ?? ""
60+
private mutating func compoundPrefix() throws -> String {
61+
guard let compoundPrefix = corpus.compoundPrefixes.removeRandomElementBySwap() else {
62+
throw GenerationError.noMoreCompoundPrefixes
63+
}
64+
return compoundPrefix
5665
}
5766

58-
private mutating func compoundAdjective() -> String {
59-
"\(Bool.random() ? noun() : compoundPrefix())-\(compoundSuffix())"
67+
private mutating func compoundSuffix() throws -> String {
68+
guard let compoundSuffix = corpus.compoundSuffixes.removeRandomElementBySwap() else {
69+
throw GenerationError.noMoreCompoundSuffixes
70+
}
71+
return compoundSuffix
6072
}
6173

62-
private mutating func adjective() -> String {
63-
Bool.random() ? compoundAdjective() : corpus.adjectives.removeRandomElementBySwap()!
74+
private mutating func primitiveAdjective() throws -> String {
75+
guard let adjective = corpus.adjectives.removeRandomElementBySwap() else {
76+
throw GenerationError.noMoreAdjectives
77+
}
78+
return adjective
79+
}
80+
81+
private mutating func compoundAdjective() throws -> String {
82+
try "\(Bool.random() ? noun() : compoundPrefix())-\(compoundSuffix())"
83+
}
84+
85+
private mutating func adjective() throws -> String {
86+
try Bool.random() ? compoundAdjective() : primitiveAdjective()
6487
}
6588
}
6689

@@ -159,8 +182,12 @@ public class BuzzwordPhraseCommand: StringCommand {
159182
let nouns = parsedArgs.output.nouns.flatMap { Int($0) } ?? 2
160183

161184
var generator = Generator(corpus: corpus)
162-
let phrase = generator.phrase(adjectives: adjectives, nouns: nouns)
163185

164-
await output.append(phrase)
186+
do {
187+
let phrase = try generator.phrase(adjectives: adjectives, nouns: nouns)
188+
await output.append(phrase)
189+
} catch {
190+
await output.append(error, errorText: "Could not generate phrase: \(error)")
191+
}
165192
}
166193
}

0 commit comments

Comments
 (0)