Default DeviceInfoProvider

This commit is contained in:
Kabir Oberai
2024-12-31 01:03:24 +05:30
Unverified
parent 23e7b77080
commit f721741b23
3 changed files with 18 additions and 18 deletions
@@ -83,7 +83,7 @@ extension DeviceInfo {
}
}
#elseif os(Linux) || os(Windows)
#else
extension DeviceInfo {
public static func current() -> DeviceInfo? {
return DeviceInfo(
@@ -97,5 +97,5 @@ extension DeviceInfo {
}
#endif
// FIXME: Figure out DeviceInfo.current() on Windows/Linux
// FIXME: Figure out DeviceInfo.current() outside macOS
// (see above)
@@ -92,14 +92,28 @@ public struct DeviceInfo: Codable, Sendable {
}
public struct DeviceInfoProvider: TestDependencyKey, Sendable {
extension DeviceInfo {
public enum FetchError: Error {
case couldNotFetch
}
fileprivate static func fetch() throws -> Self {
guard let deviceInfo = DeviceInfo.current() else {
throw FetchError.couldNotFetch
}
return deviceInfo
}
}
public struct DeviceInfoProvider: DependencyKey, Sendable {
public var fetch: @Sendable () throws -> DeviceInfo
public init(fetch: @escaping @Sendable () throws -> DeviceInfo) {
self.fetch = fetch
}
public static let testValue = DeviceInfoProvider(fetch: unimplemented())
private static let current = Result { try DeviceInfo.fetch() }
public static let liveValue = DeviceInfoProvider { try current.get() }
}
extension DependencyValues {
@@ -201,17 +201,3 @@ struct AuthCommand: AsyncParsableCommand {
defaultSubcommand: AuthLoginCommand.self
)
}
extension DeviceInfo {
static func fetch() throws -> Self {
guard let deviceInfo = DeviceInfo.current() else {
throw Console.Error("Could not fetch client info.")
}
return deviceInfo
}
}
extension DeviceInfoProvider: DependencyKey {
private static let current = Result { try DeviceInfo.fetch() }
public static let liveValue = DeviceInfoProvider { try current.get() }
}