diff --git a/Sources/DatabaseClient/TrunkSizes.swift b/Sources/DatabaseClient/TrunkSizes.swift index be4f850..80867c4 100644 --- a/Sources/DatabaseClient/TrunkSizes.swift +++ b/Sources/DatabaseClient/TrunkSizes.swift @@ -41,8 +41,9 @@ extension DatabaseClient.TrunkSizes: TestDependencyKey { type: request.type ) try await model.save(on: database) - // TODO: This `model.toDTO()` may not work now, need to check. - try await roomProxies.append(model.toDTO()) + roomProxies.append( + .init(room: try room.toDTO(), registers: registers) + ) } return try .init( @@ -77,7 +78,7 @@ extension DatabaseClient.TrunkSizes: TestDependencyKey { else { return nil } - return try await model.toDTO() + return try model.toDTO() }, update: { id, updates in guard diff --git a/Sources/Styleguide/ResultView.swift b/Sources/Styleguide/ResultView.swift index 5067fb6..b538182 100644 --- a/Sources/Styleguide/ResultView.swift +++ b/Sources/Styleguide/ResultView.swift @@ -1,102 +1,76 @@ import Elementary import Foundation -public struct ResultView< - V: Sendable, - E: Error, - ValueView: HTML, - ErrorView: HTML ->: HTML { +public struct ResultView: HTML where ValueView: HTML, ErrorView: HTML { - let onSuccess: @Sendable (V) -> ValueView - let onError: @Sendable (E) -> ErrorView - let result: Result + let result: Result + let errorView: @Sendable (any Error) -> ErrorView public init( - result: Result, - @HTMLBuilder onSuccess: @escaping @Sendable (V) -> ValueView, - @HTMLBuilder onError: @escaping @Sendable (E) -> ErrorView - ) { - self.result = result - self.onError = onError - self.onSuccess = onSuccess + _ content: @escaping @Sendable () async throws -> ValueView, + onError: @escaping @Sendable (any Error) -> ErrorView + ) async { + self.result = await Result(catching: content) + self.errorView = onError } public var body: some HTML { switch result { - case .success(let value): - onSuccess(value) + case .success(let view): + view case .failure(let error): - onError(error) + errorView(error) } } } -extension ResultView { +extension ResultView where ErrorView == Styleguide.ErrorView { public init( - result: Result, - @HTMLBuilder onSuccess: @escaping @Sendable (V) -> ValueView - ) where ErrorView == Styleguide.ErrorView { - self.init(result: result, onSuccess: onSuccess) { error in - Styleguide.ErrorView(error: error) - } + _ content: @escaping @Sendable () async throws -> ValueView + ) async { + await self.init( + content, + onError: { Styleguide.ErrorView(error: $0) } + ) + } + + public init( + catching: @escaping @Sendable () async throws -> V, + onSuccess content: @escaping @Sendable (V) -> ValueView + ) async where ValueView: Sendable { + await self.init( + { + try await content(catching()) + } + ) } public init( - catching: @escaping @Sendable () async throws(E) -> V, - @HTMLBuilder onSuccess: @escaping @Sendable (V) -> ValueView - ) async where ErrorView == Styleguide.ErrorView { + catching: @escaping @Sendable () async throws -> Void + ) async where ValueView == EmptyHTML { await self.init( - result: .init(catching: catching), - onSuccess: onSuccess - ) { error in - Styleguide.ErrorView(error: error) - } - } - - public init( - catching: @escaping @Sendable () async throws(E) -> V, - ) async where ErrorView == Styleguide.ErrorView, V == Void, ValueView == EmptyHTML { - await self.init( - result: .init(catching: catching), + catching: catching, onSuccess: { EmptyHTML() } - ) { error in - Styleguide.ErrorView(error: error) - } + ) } } -extension ResultView where V == ValueView { +extension ResultView: Sendable where ValueView: Sendable, ErrorView: Sendable {} - public init( - catching: @escaping @Sendable () async throws(E) -> V - ) async where ErrorView == Styleguide.ErrorView { - await self.init(result: .init(catching: catching)) { - $0 - } onError: { error in - Styleguide.ErrorView(error: error) - } - } -} +public struct ErrorView: HTML, Sendable { + let error: any Error -extension ResultView: Sendable where Error: Sendable, ValueView: Sendable, ErrorView: Sendable {} - -public struct ErrorView: HTML, Sendable where Error: Sendable { - - let error: E - - public init(error: E) { + public init(error: any Error) { self.error = error } public var body: some HTML { div { - h1(.class("text-2xl font-bold text-error")) { "Oops: Error" } + h1(.class("text-xl font-bold text-error")) { "Oops: Error" } p { "\(error)" } } } - } diff --git a/Sources/ViewController/Live.swift b/Sources/ViewController/Live.swift index 60ef3aa..dc1d10f 100644 --- a/Sources/ViewController/Live.swift +++ b/Sources/ViewController/Live.swift @@ -193,9 +193,14 @@ extension SiteRoute.View.ProjectRoute { case .frictionRate(let route): return await route.renderView(on: request, projectID: projectID) case .pdf: - return await ResultView { - try await projectClient.toHTML(projectID) - } + // return await ResultView2 { + // try await projectClient.toHTML(projectID) + // } onError: { + // ErrorView2(error: $0) + // } + // return await ResultView { + return try! await projectClient.toHTML(projectID) + // } // fatalError() case .rooms(let route): return await route.renderView(on: request, projectID: projectID)