Skip to content

Commit

Permalink
remove generic error
Browse files Browse the repository at this point in the history
  • Loading branch information
morganchen12 committed Jan 25, 2024
1 parent 4b0a290 commit a68afb4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
28 changes: 14 additions & 14 deletions Sources/GoogleAI/PartsRepresentable+Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,52 +41,52 @@ public enum ImageConversionError: Error {
#if canImport(UIKit)
/// Enables images to be representable as ``PartsRepresentable``.
extension UIImage: PartsRepresentable {
public func toModelContentParts() -> Result<[ModelContent.Part], ImageConversionError> {
public func tryPartsValue() throws -> [ModelContent.Part] {
guard let data = jpegData(compressionQuality: imageCompressionQuality) else {
return .failure(.couldNotConvertToJPEG(self))
throw ImageConversionError.couldNotConvertToJPEG(self)
}
return .success([ModelContent.Part.data(mimetype: "image/jpeg", data)])
return [ModelContent.Part.data(mimetype: "image/jpeg", data)]
}
}

#elseif canImport(AppKit)
/// Enables images to be representable as ``PartsRepresentable``.
extension NSImage: PartsRepresentable {
public func toModelContentParts() -> Result<[ModelContent.Part], ImageConversionError> {
public func tryPartsValue() throws -> [ModelContent.Part] {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else {
return .failure(.invalidUnderlyingImage)
throw ImageConversionError.invalidUnderlyingImage
}
let bmp = NSBitmapImageRep(cgImage: cgImage)
guard let data = bmp.representation(using: .jpeg, properties: [.compressionFactor: 0.8])
else {
return .failure(.couldNotConvertToJPEG(bmp))
throw ImageConversionError.couldNotConvertToJPEG(bmp)
}
return .success([ModelContent.Part.data(mimetype: "image/jpeg", data)])
return [ModelContent.Part.data(mimetype: "image/jpeg", data)]
}
}
#endif

extension CGImage: PartsRepresentable {
public func toModelContentParts() -> Result<[ModelContent.Part], ImageConversionError> {
public func tryPartsValue() throws -> [ModelContent.Part] {
let output = NSMutableData()
guard let imageDestination = CGImageDestinationCreateWithData(
output, UTType.jpeg.identifier as CFString, 1, nil
) else {
return .failure(.couldNotAllocateDestination)
throw ImageConversionError.couldNotAllocateDestination
}
CGImageDestinationAddImage(imageDestination, self, nil)
CGImageDestinationSetProperties(imageDestination, [
kCGImageDestinationLossyCompressionQuality: imageCompressionQuality,
] as CFDictionary)
if CGImageDestinationFinalize(imageDestination) {
return .success([.data(mimetype: "image/jpeg", output as Data)])
return [.data(mimetype: "image/jpeg", output as Data)]
}
return .failure(.couldNotConvertToJPEG(self))
throw ImageConversionError.couldNotConvertToJPEG(self)
}
}

extension CIImage: PartsRepresentable {
public func toModelContentParts() -> Result<[ModelContent.Part], ImageConversionError> {
public func tryPartsValue() throws -> [ModelContent.Part] {
let context = CIContext()
let jpegData = (colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB))
.flatMap {
Expand All @@ -96,8 +96,8 @@ extension CIImage: PartsRepresentable {
context.jpegRepresentation(of: self, colorSpace: $0, options: [:])
}
if let jpegData = jpegData {
return .success([.data(mimetype: "image/jpeg", jpegData)])
return [.data(mimetype: "image/jpeg", jpegData)]
}
return .failure(.couldNotConvertToJPEG(self))
throw ImageConversionError.couldNotConvertToJPEG(self)
}
}
44 changes: 10 additions & 34 deletions Sources/GoogleAI/PartsRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,31 @@ import Foundation
/// A protocol describing any data that could be serialized to model-interpretable input data,
/// where the serialization process might fail with an error.
public protocol PartsRepresentable {
associatedtype ErrorType where ErrorType: Error
func toModelContentParts() -> Result<[ModelContent.Part], ErrorType>
}

public extension PartsRepresentable {
func tryPartsValue() throws -> [ModelContent.Part] {
return try toModelContentParts().get()
}
}

public extension PartsRepresentable where ErrorType == Never {
var partsValue: [ModelContent.Part] {
let content = toModelContentParts()
switch content {
case let .success(success):
return success
}
}
func tryPartsValue() throws -> [ModelContent.Part]
}

/// Enables a ``ModelContent.Part`` to be passed in as ``PartsRepresentable``.
extension ModelContent.Part: PartsRepresentable {
public typealias ErrorType = Never
public func toModelContentParts() -> Result<[ModelContent.Part], Never> {
return .success([self])
public func tryPartsValue() throws -> [ModelContent.Part] {
return [self]
}
}

/// Enable an `Array` of ``PartsRepresentable`` values to be passed in as a single
/// ``PartsRepresentable``.
extension [any PartsRepresentable]: PartsRepresentable {
public typealias ErrorType = Error

public func toModelContentParts() -> Result<[ModelContent.Part], Error> {
let result = { () throws -> [ModelContent.Part] in
try compactMap { element in
try element.tryPartsValue()
}
.flatMap { $0 }
extension [PartsRepresentable]: PartsRepresentable {
public func tryPartsValue() throws -> [ModelContent.Part] {
return try compactMap { element in
try element.tryPartsValue()
}
return Result(catching: result)
.flatMap { $0 }
}
}

/// Enables a `String` to be passed in as ``PartsRepresentable``.
extension String: PartsRepresentable {
public typealias ErrorType = Never

public func toModelContentParts() -> Result<[ModelContent.Part], Never> {
return .success([.text(self)])
public func tryPartsValue() throws -> [ModelContent.Part] {
return [.text(self)]
}
}

0 comments on commit a68afb4

Please sign in to comment.