@@ -97,21 +97,44 @@ public struct SafetySetting {
9797}
9898
9999/// Categories describing the potential harm a piece of content may pose.
100- public enum HarmCategory : String , Sendable {
100+ public struct HarmCategory : Sendable , Equatable , Hashable {
101101 /// Unknown. A new server value that isn't recognized by the SDK.
102- case unknown = " HARM_CATEGORY_UNKNOWN "
102+ public static var unknown : HarmCategory {
103+ return self . init ( category: " HARM_CATEGORY_UNKNOWN " )
104+ }
103105
104106 /// Harassment content.
105- case harassment = " HARM_CATEGORY_HARASSMENT "
107+ public static var harassment : HarmCategory {
108+ return self . init ( category: " HARM_CATEGORY_HARASSMENT " )
109+ }
106110
107111 /// Negative or harmful comments targeting identity and/or protected attributes.
108- case hateSpeech = " HARM_CATEGORY_HATE_SPEECH "
112+ public static var hateSpeech : HarmCategory {
113+ return self . init ( category: " HARM_CATEGORY_HATE_SPEECH " )
114+ }
109115
110116 /// Contains references to sexual acts or other lewd content.
111- case sexuallyExplicit = " HARM_CATEGORY_SEXUALLY_EXPLICIT "
117+ public static var sexuallyExplicit : HarmCategory {
118+ return self . init ( category: " HARM_CATEGORY_SEXUALLY_EXPLICIT " )
119+ }
112120
113121 /// Promotes or enables access to harmful goods, services, or activities.
114- case dangerousContent = " HARM_CATEGORY_DANGEROUS_CONTENT "
122+ public static var dangerousContent : HarmCategory {
123+ return self . init ( category: " HARM_CATEGORY_DANGEROUS_CONTENT " )
124+ }
125+
126+ static let allCategories = [
127+ HarmCategory . harassment. category,
128+ HarmCategory . hateSpeech. category,
129+ HarmCategory . sexuallyExplicit. category,
130+ HarmCategory . dangerousContent. category,
131+ ]
132+
133+ let category : String
134+
135+ init ( category: String ) {
136+ self . category = category
137+ }
115138}
116139
117140// MARK: - Codable Conformances
@@ -139,17 +162,17 @@ extension SafetyRating: Decodable {}
139162@available ( iOS 15 . 0 , macOS 11 . 0 , macCatalyst 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
140163extension HarmCategory : Codable {
141164 public init ( from decoder: Decoder ) throws {
142- let value = try decoder. singleValueContainer ( ) . decode ( String . self)
143- guard let decodedCategory = HarmCategory ( rawValue : value ) else {
165+ let category = try decoder. singleValueContainer ( ) . decode ( String . self)
166+ guard HarmCategory . allCategories . contains ( category ) else {
144167 VertexLog . error (
145168 code: . generateContentResponseUnrecognizedHarmCategory,
146- " Unrecognized HarmCategory with value \" \( value ) \" . "
169+ " Unrecognized HarmCategory with value \" \( category ) \" . "
147170 )
148171 self = . unknown
149172 return
150173 }
151174
152- self = decodedCategory
175+ self . init ( category : category )
153176 }
154177}
155178
0 commit comments