Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if SWT_TARGET_OS_APPLE && canImport(CoreGraphics)
public import UniformTypeIdentifiers

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
@available(_uttypesAPI, *)
extension AttachableImageFormat {
/// The content type corresponding to this image format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ private import CoreGraphics

private import UniformTypeIdentifiers

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
@available(_uttypesAPI, *)
extension _AttachableImageWrapper: Attachable, AttachableWrapper where Image: AttachableAsImage {
/// Get the image format to use when encoding an image, substituting a
Expand Down Expand Up @@ -47,11 +50,17 @@ extension _AttachableImageWrapper: Attachable, AttachableWrapper where Image: At
return encodingQuality < 1.0 ? .jpeg : .png
}

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<_AttachableImageWrapper>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
let imageFormat = _imageFormat(forPreferredName: attachment.preferredName)
return try wrappedValue.withUnsafeBytes(as: imageFormat, body)
}

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
public borrowing func preferredName(for attachment: borrowing Attachment<_AttachableImageWrapper>, basedOn suggestedName: String) -> String {
let imageFormat = _imageFormat(forPreferredName: suggestedName)
return (suggestedName as NSString).appendingPathExtension(for: imageFormat.contentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if os(Windows)
public import WinSDK

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
extension AttachableImageFormat {
private static let _encoderPathExtensionsByCLSID = Result {
var result = [CLSID.Wrapper: [String]]()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if os(Windows)
private import WinSDK

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
extension _AttachableImageWrapper: Attachable, AttachableWrapper where Image: AttachableAsImage {
/// Get the image format to use when encoding an image.
///
Expand Down Expand Up @@ -38,11 +41,17 @@ extension _AttachableImageWrapper: Attachable, AttachableWrapper where Image: At
return encodingQuality < 1.0 ? .jpeg : .png
}

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<_AttachableImageWrapper>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
let imageFormat = _imageFormat(forPreferredName: attachment.preferredName)
return try wrappedValue.withUnsafeBytes(as: imageFormat, body)
}

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
public borrowing func preferredName(for attachment: borrowing Attachment<_AttachableImageWrapper>, basedOn suggestedName: String) -> String {
let imageFormat = _imageFormat(forPreferredName: suggestedName)
return AttachableImageFormat.appendPathExtension(for: imageFormat.encoderCLSID, to: suggestedName)
Expand Down
4 changes: 4 additions & 0 deletions Sources/Testing/Attachments/Images/AttachableAsImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public protocol AttachableAsImage {
/// The testing library uses this function when saving an image as an
/// attachment. The implementation should use `imageFormat` to determine what
/// encoder to use.
///
/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
borrowing func withUnsafeBytes<R>(as imageFormat: AttachableImageFormat, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R

/// Make a copy of this instance to pass to an attachment.
Expand Down
15 changes: 15 additions & 0 deletions Sources/Testing/Attachments/Images/AttachableImageFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public struct AttachableImageFormat: Sendable {

// MARK: - Equatable, Hashable

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
#if SWT_NO_IMAGE_ATTACHMENTS
@_unavailableInEmbedded
@available(*, unavailable, message: "Image attachments are not available on this platform.")
Expand Down Expand Up @@ -120,12 +123,18 @@ extension AttachableImageFormat.Kind: Equatable, Hashable {

// MARK: - CustomStringConvertible, CustomDebugStringConvertible

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
#if SWT_NO_IMAGE_ATTACHMENTS
@_unavailableInEmbedded
@available(*, unavailable, message: "Image attachments are not available on this platform.")
#endif
@available(_uttypesAPI, *)
extension AttachableImageFormat: CustomStringConvertible, CustomDebugStringConvertible {
/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
public var description: String {
let kindDescription = String(describing: kind)
if encodingQuality < 1.0 {
Expand All @@ -134,6 +143,9 @@ extension AttachableImageFormat: CustomStringConvertible, CustomDebugStringConve
return kindDescription
}

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
public var debugDescription: String {
let kindDescription = String(reflecting: kind)
return "\(kindDescription) at quality \(encodingQuality)"
Expand All @@ -142,6 +154,9 @@ extension AttachableImageFormat: CustomStringConvertible, CustomDebugStringConve

// MARK: -

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
#if SWT_NO_IMAGE_ATTACHMENTS
@_unavailableInEmbedded
@available(*, unavailable, message: "Image attachments are not available on this platform.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

/// @Metadata {
/// @Available(Swift, introduced: 6.3)
/// }
#if SWT_NO_IMAGE_ATTACHMENTS
@_unavailableInEmbedded
@available(*, unavailable, message: "Image attachments are not available on this platform.")
Expand Down