Skip to content

Commit

Permalink
Reorganize Swift files more hierarchically (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Sep 5, 2024
1 parent c0de718 commit c228356
Show file tree
Hide file tree
Showing 48 changed files with 58 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let package = Package(
path: "Support/Sources/WindowsRuntime",
exclude: [
"CMakeLists.txt",
"ProjectedTypes/Core/Readme.md",
"ProjectedTypes/Readme.md",
"ProjectedTypes/WindowsFoundation/Readme.md",
]),
.testTarget(
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions Support/Sources/COM/COMError+createErrorInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import COM_ABI

extension COMError {
/// Creates an instance of `ICreateErrorInfo`.
public static func createErrorInfo() throws -> ICreateErrorInfo {
var createErrorInfo = ICreateErrorInfoProjection.abiDefaultValue
defer { ICreateErrorInfoProjection.release(&createErrorInfo) }
try COMError.fromABI(SWRT_CreateErrorInfo(&createErrorInfo))
return try NullResult.unwrap(ICreateErrorInfoProjection.toSwift(createErrorInfo))
}

/// Creates an instance of `IErrorInfo` with prepopulated fields.
public static func createErrorInfo(guid: GUID? = nil, source: String? = nil, description: String?,
helpFile: String? = nil, helpContext: UInt32? = nil) throws -> IErrorInfo {
let errorInfo = try createErrorInfo()
if let guid { try errorInfo.setGUID(guid) }
if let source { try errorInfo.setSource(source) }
if let description { try errorInfo.setDescription(description) }
if let helpFile { try errorInfo.setHelpFile(helpFile) }
if let helpContext { try errorInfo.setHelpContext(helpContext) }
return try errorInfo.queryInterface(IErrorInfoProjection.self)
}
}
2 changes: 1 addition & 1 deletion Support/Sources/COM/COMError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct COMError: COMErrorProtocol, CustomStringConvertible {
}

public init(hresult: HResult, description: String?) {
self.init(hresult: hresult, errorInfo: description.flatMap { try? createErrorInfo(description: $0) })
self.init(hresult: hresult, errorInfo: description.flatMap { try? Self.createErrorInfo(description: $0) })
}

public var description: String {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 0 additions & 21 deletions Support/Sources/COM/createErrorInfo.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import COM

/// Projects a WinRT array to a Swift array.
public enum ArrayProjection<ElementProjection: ABIProjection>: ABIProjection {
// WinRT does not have a distinct representation for null and empty
public typealias SwiftValue = [ElementProjection.SwiftValue]
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import COM
public typealias IRestrictedErrorInfo = any IRestrictedErrorInfoProtocol
public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol {
func getErrorDetails(
description: inout String?,
error: inout HResult,
restrictedDescription: inout String?,
capabilitySid: inout String?) throws
_ description: inout String?,
_ error: inout HResult,
_ restrictedDescription: inout String?,
_ capabilitySid: inout String?) throws
var reference: String? { get throws }
}

extension IRestrictedErrorInfoProtocol {
var errorDetails: (description: String?, error: HResult, restrictedDescription: String?, capabilitySid: String?) {
get throws {
var description: String? = nil
var error: HResult = .fail
var restrictedDescription: String? = nil
var capabilitySid: String? = nil
try getErrorDetails(&description, &error, &restrictedDescription, &capabilitySid)
return (description, error, restrictedDescription, capabilitySid)
}
}
}

import WindowsRuntime_ABI

public enum IRestrictedErrorInfoProjection: COMProjection {
Expand All @@ -28,10 +41,10 @@ public enum IRestrictedErrorInfoProjection: COMProjection {

private final class Import: COMImport<IRestrictedErrorInfoProjection>, IRestrictedErrorInfoProtocol {
func getErrorDetails(
description: inout String?,
error: inout HResult,
restrictedDescription: inout String?,
capabilitySid: inout String?) throws {
_ description: inout String?,
_ error: inout HResult,
_ restrictedDescription: inout String?,
_ capabilitySid: inout String?) throws {
try _interop.getErrorDetails(&description, &error, &restrictedDescription, &capabilitySid)
}

Expand Down
1 change: 1 addition & 0 deletions Support/Sources/WindowsRuntime/ProjectedTypes/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Defines Swift projections for core WinRT types, and COM types that support the Windows Runtime. Those types are defined in native idl or C++ headers rather than Windows Metadata files and cannot be generated.
24 changes: 10 additions & 14 deletions Support/Sources/WindowsRuntime/WinRTError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public struct WinRTError: COMErrorProtocol, CustomStringConvertible {
}

public var description: String {
let details = (try? restrictedErrorInfo?.details) ?? RestrictedErrorInfoDetails()
// RestrictedDescription contains the value reported in RoOriginateError
return details.restrictedDescription ?? details.description ?? hresult.description
if let restrictedErrorInfo {
let errorDetails = try? restrictedErrorInfo.errorDetails
if let restrictedDescription = errorDetails?.restrictedDescription { return restrictedDescription }
if let description = errorDetails?.description { return description }
}

return hresult.description
}

public func toABI(setErrorInfo: Bool = true) -> HResult.Value {
Expand All @@ -41,20 +45,12 @@ public struct WinRTError: COMErrorProtocol, CustomStringConvertible {
guard hresult.isFailure else { return hresult }

// Check for an associated IRestrictedErrorInfo
guard captureErrorInfo, let restrictedErrorInfo = try? Self.getRestrictedErrorInfo(matching: hresult) else {
guard captureErrorInfo,
let restrictedErrorInfo = try? Self.getRestrictedErrorInfo(matching: hresult),
(try? restrictedErrorInfo.errorDetails.error) == hresult else {
throw WinRTError(hresult: hresult)
}

// Ensure we didn't get a stale IRestrictedErrorInfo
var description: String? = nil
var error: HResult = .ok
var restrictedDescription: String? = nil
var capabilitySid: String? = nil
try? restrictedErrorInfo.getErrorDetails(
description: &description, error: &error,
restrictedDescription: &restrictedDescription, capabilitySid: &capabilitySid)
guard error == hresult else { throw WinRTError(hresult: hresult) }

// Append to the propagation context, if available.
// See https://learn.microsoft.com/en-us/windows/win32/api/restrictederrorinfo/nf-restrictederrorinfo-ilanguageexceptionerrorinfo2-capturepropagationcontext
if let languageExceptionErrorInfo = try? restrictedErrorInfo.queryInterface(ILanguageExceptionErrorInfoProjection.self) {
Expand Down

0 comments on commit c228356

Please sign in to comment.