diff --git a/Mlem/Extensions/View - Handle Lemmy Links.swift b/Mlem/Extensions/View - Handle Lemmy Links.swift index e78639dbb..4c0fee29e 100644 --- a/Mlem/Extensions/View - Handle Lemmy Links.swift +++ b/Mlem/Extensions/View - Handle Lemmy Links.swift @@ -153,29 +153,21 @@ struct HandleLemmyLinkResolution: ViewModifier { } return await MainActor.run { - switch resolution { - case let .post(object): - if let route = Path.makeRoute(object) { - navigationPath.wrappedValue.append(route) + do { + switch resolution { + case let .post(object): + navigationPath.wrappedValue.append(try Path.makeRoute(object)) return true - } else { - return false - } - case let .person(object): - if let route = Path.makeRoute(object.person) { - navigationPath.wrappedValue.append(route) + case let .person(object): + navigationPath.wrappedValue.append(try Path.makeRoute(object.person)) return true - } else { - return false - } - case let .community(object): - if let route = Path.makeRoute(object) { - navigationPath.wrappedValue.append(route) + case let .community(object): + navigationPath.wrappedValue.append(try Path.makeRoute(object)) return true - } else { + case .comment: return false } - case .comment: + } catch { return false } } diff --git a/Mlem/Navigation/AnyNavigationPath.swift b/Mlem/Navigation/AnyNavigationPath.swift index ffc158311..3c5d9fe60 100644 --- a/Mlem/Navigation/AnyNavigationPath.swift +++ b/Mlem/Navigation/AnyNavigationPath.swift @@ -13,7 +13,7 @@ protocol AnyNavigationPath { associatedtype Route: Routable /// Implementation should make a route that makes sense for the passed-in data value and can be appended to the navigation path. - static func makeRoute(_ value: V) -> Route? where V: Hashable + static func makeRoute(_ value: V) throws -> Route where V: Hashable /// The number of elements in this path. var count: Int { get } diff --git a/Mlem/Navigation/Routable.swift b/Mlem/Navigation/Routable.swift index 4d41ed383..2cea9de47 100644 --- a/Mlem/Navigation/Routable.swift +++ b/Mlem/Navigation/Routable.swift @@ -12,22 +12,25 @@ protocol Routable: Hashable { /// - Parameter value: A data type for a given navigation destination. /// - Returns: `nil` if data value cannot be mapped to a navigation route. - static func makeRoute(_ value: V) -> Self? where V: Hashable + static func makeRoute(_ value: V) throws -> Self where V: Hashable /// Generic error string static var makeRouteErrorString: String { get } } +enum RoutableError: LocalizedError { + case routeNotConfigured(value: V) +} + extension Routable { /// Default implementation. - static func makeRoute(_ value: V) -> Self? where V: Hashable { + static func makeRoute(_ value: V) throws -> Self where V: Hashable { switch value { case let value as Self: return value default: - print(Self.makeRouteErrorString) - return nil + throw RoutableError.routeNotConfigured(value: value) } } diff --git a/Mlem/Navigation/Route/SettingsRoutes.swift b/Mlem/Navigation/Route/SettingsRoutes.swift index 95fac5afb..538974525 100644 --- a/Mlem/Navigation/Route/SettingsRoutes.swift +++ b/Mlem/Navigation/Route/SettingsRoutes.swift @@ -22,47 +22,24 @@ enum SettingsRoute: Routable { case postPage(PostSettingsRoute) case licensesPage(LicensesSettingsRoute) - // swiftlint:disable cyclomatic_complexity - static func makeRoute(_ value: V) -> SettingsRoute? where V: Hashable { + static func makeRoute(_ value: V) throws -> SettingsRoute where V: Hashable { switch value { case let value as Self: return value case let value as AboutSettingsRoute: - if let route = AboutSettingsRoute.makeRoute(value) { - return .aboutPage(route) - } else { - return nil - } + return try .aboutPage(AboutSettingsRoute.makeRoute(value)) case let value as AppearanceSettingsRoute: - if let route = AppearanceSettingsRoute.makeRoute(value) { - return .appearancePage(route) - } else { - return nil - } + return try .appearancePage(AppearanceSettingsRoute.makeRoute(value)) case let value as CommentSettingsRoute: - if let route = CommentSettingsRoute.makeRoute(value) { - return .commentPage(route) - } else { - return nil - } + return try .commentPage(CommentSettingsRoute.makeRoute(value)) case let value as PostSettingsRoute: - if let route = PostSettingsRoute.makeRoute(value) { - return .postPage(route) - } else { - return nil - } + return try .postPage(PostSettingsRoute.makeRoute(value)) case let value as LicensesSettingsRoute: - if let route = LicensesSettingsRoute.makeRoute(value) { - return .licensesPage(route) - } else { - return nil - } + return try .licensesPage(LicensesSettingsRoute.makeRoute(value)) default: - print(Self.makeRouteErrorString) - return nil + throw RoutableError.routeNotConfigured(value: value) } } - // swiftlint:enable cyclomatic_complexity } enum AppearanceSettingsRoute: Routable, Codable { @@ -89,7 +66,7 @@ enum AboutSettingsRoute: Routable { case eula(Document) case licenses - static func makeRoute(_ value: V) -> AboutSettingsRoute? where V: Hashable { + static func makeRoute(_ value: V) throws -> AboutSettingsRoute where V: Hashable { switch value { case let value as Self: return value @@ -97,8 +74,7 @@ enum AboutSettingsRoute: Routable { // return .privacyPolicy(value) return .eula(value) default: - print(Self.makeRouteErrorString) - return nil + throw RoutableError.routeNotConfigured(value: value) } } } @@ -106,13 +82,12 @@ enum AboutSettingsRoute: Routable { enum LicensesSettingsRoute: Routable { case licenseDocument(Document) - static func makeRoute(_ value: V) -> LicensesSettingsRoute? where V: Hashable { + static func makeRoute(_ value: V) throws -> LicensesSettingsRoute where V: Hashable { switch value { case let value as Document: return .licenseDocument(value) default: - print(Self.makeRouteErrorString) - return nil + throw RoutableError.routeNotConfigured(value: value) } } } diff --git a/Mlem/Navigation/Router/NavigationRouter.swift b/Mlem/Navigation/Router/NavigationRouter.swift index dbdf1e2f5..a25b066d0 100644 --- a/Mlem/Navigation/Router/NavigationRouter.swift +++ b/Mlem/Navigation/Router/NavigationRouter.swift @@ -18,8 +18,8 @@ extension NavigationRouter: AnyNavigationPath { typealias Route = RouteValue - static func makeRoute(_ value: V) -> Route? where V: Hashable { - RouteValue.makeRoute(value) ?? nil + static func makeRoute(_ value: V) throws -> Route where V: Hashable { + try RouteValue.makeRoute(value) } var count: Int {