From e83082d417b1cbccd9614d52c6d20f71ab6e8d3b Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 12 Nov 2025 08:46:15 -0800 Subject: [PATCH] Convert RSParser to approachable concurrency. --- Modules/RSParser/Package.swift | 4 ++- Modules/RSParser/Sources/ObjC/ParserData.h | 1 + .../RSParser/Sources/ObjC/RSHTMLMetadata.h | 1 + .../Sources/Swift/Feeds/FeedParser.swift | 4 +-- .../Sources/Swift/Feeds/FeedParserError.swift | 25 ++++++------------- .../Sources/Swift/Feeds/FeedType.swift | 6 ++--- .../Swift/Feeds/JSON/JSONFeedParser.swift | 12 ++++----- .../Swift/Feeds/JSON/RSSInJSONParser.swift | 13 +++++----- .../Swift/Feeds/ParsedAttachment.swift | 2 +- .../Sources/Swift/Feeds/ParsedAuthor.swift | 2 +- .../Sources/Swift/Feeds/ParsedFeed.swift | 2 +- .../Sources/Swift/Feeds/ParsedHub.swift | 2 +- .../Sources/Swift/Feeds/ParsedItem.swift | 2 +- .../Sources/Swift/Feeds/XML/AtomParser.swift | 2 +- .../Feeds/XML/RSParsedFeedTransformer.swift | 4 +-- .../Sources/Swift/Feeds/XML/RSSParser.swift | 2 +- .../Sources/Swift/JSON/JSONUtilities.swift | 2 +- .../Swift/Utilities/String+RSParser.swift | 3 +-- 18 files changed, 40 insertions(+), 49 deletions(-) diff --git a/Modules/RSParser/Package.swift b/Modules/RSParser/Package.swift index c53d865de..4b59b856f 100644 --- a/Modules/RSParser/Package.swift +++ b/Modules/RSParser/Package.swift @@ -23,7 +23,9 @@ let package = Package( dependencies: ["RSParserObjC", "RSMarkdown"], path: "Sources/Swift", swiftSettings: [ - .swiftLanguageMode(.v5) + .enableUpcomingFeature("NonisolatedNonsendingByDefault"), + .enableUpcomingFeature("InferIsolatedConformances"), + .defaultIsolation(MainActor.self) ]), .target( name: "RSParserObjC", diff --git a/Modules/RSParser/Sources/ObjC/ParserData.h b/Modules/RSParser/Sources/ObjC/ParserData.h index 30517d98d..8d75db073 100644 --- a/Modules/RSParser/Sources/ObjC/ParserData.h +++ b/Modules/RSParser/Sources/ObjC/ParserData.h @@ -10,6 +10,7 @@ NS_ASSUME_NONNULL_BEGIN +NS_SWIFT_SENDABLE @interface ParserData : NSObject @property (nonatomic, readonly) NSString *url; diff --git a/Modules/RSParser/Sources/ObjC/RSHTMLMetadata.h b/Modules/RSParser/Sources/ObjC/RSHTMLMetadata.h index 63df7a1db..36664c3a7 100755 --- a/Modules/RSParser/Sources/ObjC/RSHTMLMetadata.h +++ b/Modules/RSParser/Sources/ObjC/RSHTMLMetadata.h @@ -19,6 +19,7 @@ NS_ASSUME_NONNULL_BEGIN +NS_SWIFT_SENDABLE @interface RSHTMLMetadata : NSObject - (instancetype)initWithURLString:(NSString *)urlString tags:(NSArray *)tags; diff --git a/Modules/RSParser/Sources/Swift/Feeds/FeedParser.swift b/Modules/RSParser/Sources/Swift/Feeds/FeedParser.swift index d7f2f95c7..71551e2a0 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/FeedParser.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/FeedParser.swift @@ -12,9 +12,9 @@ import RSParserObjC // FeedParser handles RSS, Atom, JSON Feed, and RSS-in-JSON. // You don’t need to know the type of feed. -public typealias FeedParserCallback = (_ parsedFeed: ParsedFeed?, _ error: Error?) -> Void +public typealias FeedParserCallback = @Sendable (_ parsedFeed: ParsedFeed?, _ error: Error?) -> Void -public struct FeedParser { +nonisolated public struct FeedParser { private static let parseQueue = DispatchQueue(label: "FeedParser parse queue") diff --git a/Modules/RSParser/Sources/Swift/Feeds/FeedParserError.swift b/Modules/RSParser/Sources/Swift/Feeds/FeedParserError.swift index 83ccbd059..16ab67b68 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/FeedParserError.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/FeedParserError.swift @@ -8,22 +8,11 @@ import Foundation -public struct FeedParserError: Error { - - public enum FeedParserErrorType { - - case rssChannelNotFound - case rssItemsNotFound - case jsonFeedVersionNotFound - case jsonFeedItemsNotFound - case jsonFeedTitleNotFound - case invalidJSON - } - - public let errorType: FeedParserErrorType - - public init(_ errorType: FeedParserErrorType) { - - self.errorType = errorType - } +public enum FeedParserError: Error, Sendable { + case rssChannelNotFound + case rssItemsNotFound + case jsonFeedVersionNotFound + case jsonFeedItemsNotFound + case jsonFeedTitleNotFound + case invalidJSON } diff --git a/Modules/RSParser/Sources/Swift/Feeds/FeedType.swift b/Modules/RSParser/Sources/Swift/Feeds/FeedType.swift index cbd16b1ab..c7857398f 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/FeedType.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/FeedType.swift @@ -11,7 +11,7 @@ import Foundation import RSParserObjC #endif -public enum FeedType: Sendable { +nonisolated public enum FeedType: Sendable { case rss case atom case jsonFeed @@ -21,9 +21,9 @@ public enum FeedType: Sendable { } -private let minNumberOfBytesRequired = 128 +nonisolated private let minNumberOfBytesRequired = 128 -public func feedType(_ parserData: ParserData, isPartialData: Bool = false) -> FeedType { +nonisolated public func feedType(_ parserData: ParserData, isPartialData: Bool = false) -> FeedType { // Can call with partial data — while still downloading, for instance. // If there’s not enough data, return .unknown. Ask again when there’s more data. diff --git a/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift b/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift index ab168dc57..8aaa7bff2 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift @@ -13,7 +13,7 @@ import RSParserObjC // See https://jsonfeed.org/version/1.1 -public struct JSONFeedParser { +nonisolated public struct JSONFeedParser { struct Key { static let version = "version" @@ -55,17 +55,17 @@ public struct JSONFeedParser { public static func parse(_ parserData: ParserData) throws -> ParsedFeed? { guard let d = JSONUtilities.dictionary(with: parserData.data) else { - throw FeedParserError(.invalidJSON) + throw FeedParserError.invalidJSON } guard let version = d[Key.version] as? String, let _ = version.range(of: JSONFeedParser.jsonFeedVersionMarker) else { - throw FeedParserError(.jsonFeedVersionNotFound) + throw FeedParserError.jsonFeedVersionNotFound } guard let itemsArray = d[Key.items] as? JSONArray else { - throw FeedParserError(.jsonFeedItemsNotFound) + throw FeedParserError.jsonFeedItemsNotFound } guard let title = d[Key.title] as? String else { - throw FeedParserError(.jsonFeedTitleNotFound) + throw FeedParserError.jsonFeedTitleNotFound } let authors = parseAuthors(d) @@ -85,7 +85,7 @@ public struct JSONFeedParser { } } -private extension JSONFeedParser { +nonisolated private extension JSONFeedParser { static func parseAuthors(_ dictionary: JSONDictionary) -> Set? { diff --git a/Modules/RSParser/Sources/Swift/Feeds/JSON/RSSInJSONParser.swift b/Modules/RSParser/Sources/Swift/Feeds/JSON/RSSInJSONParser.swift index 2f31422b6..612a5ece3 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/JSON/RSSInJSONParser.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/JSON/RSSInJSONParser.swift @@ -14,19 +14,18 @@ import RSParserObjC // See https://github.com/scripting/Scripting-News/blob/master/rss-in-json/README.md // Also: http://cyber.harvard.edu/rss/rss.html -public struct RSSInJSONParser { +nonisolated public struct RSSInJSONParser { public static func parse(_ parserData: ParserData) throws -> ParsedFeed? { - do { guard let parsedObject = try JSONSerialization.jsonObject(with: parserData.data) as? JSONDictionary else { - throw FeedParserError(.invalidJSON) + throw FeedParserError.invalidJSON } guard let rssObject = parsedObject["rss"] as? JSONDictionary else { - throw FeedParserError(.rssChannelNotFound) + throw FeedParserError.rssChannelNotFound } guard let channelObject = rssObject["channel"] as? JSONDictionary else { - throw FeedParserError(.rssChannelNotFound) + throw FeedParserError.rssChannelNotFound } // I’d bet money that in practice the items array won’t always appear correctly inside the channel object. @@ -42,7 +41,7 @@ public struct RSSInJSONParser { itemsObject = parsedObject["items"] as? JSONArray } if itemsObject == nil { - throw FeedParserError(.rssItemsNotFound) + throw FeedParserError.rssItemsNotFound } let title = channelObject["title"] as? String @@ -60,7 +59,7 @@ public struct RSSInJSONParser { } } -private extension RSSInJSONParser { +nonisolated private extension RSSInJSONParser { static func parseItems(_ itemsObject: JSONArray, _ feedURL: String) -> Set { diff --git a/Modules/RSParser/Sources/Swift/Feeds/ParsedAttachment.swift b/Modules/RSParser/Sources/Swift/Feeds/ParsedAttachment.swift index f1f0eda33..aa782c634 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/ParsedAttachment.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/ParsedAttachment.swift @@ -8,7 +8,7 @@ import Foundation -public struct ParsedAttachment: Hashable, Sendable { +nonisolated public struct ParsedAttachment: Hashable, Sendable { public let url: String public let mimeType: String? public let title: String? diff --git a/Modules/RSParser/Sources/Swift/Feeds/ParsedAuthor.swift b/Modules/RSParser/Sources/Swift/Feeds/ParsedAuthor.swift index d28f02fdb..8d85d6899 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/ParsedAuthor.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/ParsedAuthor.swift @@ -8,7 +8,7 @@ import Foundation -public struct ParsedAuthor: Hashable, Codable, Sendable { +nonisolated public struct ParsedAuthor: Hashable, Codable, Sendable { public let name: String? public let url: String? diff --git a/Modules/RSParser/Sources/Swift/Feeds/ParsedFeed.swift b/Modules/RSParser/Sources/Swift/Feeds/ParsedFeed.swift index 105b3d885..8a8442301 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/ParsedFeed.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/ParsedFeed.swift @@ -8,7 +8,7 @@ import Foundation -public struct ParsedFeed: Sendable { +nonisolated public struct ParsedFeed: Sendable { public let type: FeedType public let title: String? public let homePageURL: String? diff --git a/Modules/RSParser/Sources/Swift/Feeds/ParsedHub.swift b/Modules/RSParser/Sources/Swift/Feeds/ParsedHub.swift index a2d17dc71..d4c8317a1 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/ParsedHub.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/ParsedHub.swift @@ -8,7 +8,7 @@ import Foundation -public struct ParsedHub: Hashable, Sendable { +nonisolated public struct ParsedHub: Hashable, Sendable { public let type: String public let url: String } diff --git a/Modules/RSParser/Sources/Swift/Feeds/ParsedItem.swift b/Modules/RSParser/Sources/Swift/Feeds/ParsedItem.swift index 062b5cbc8..75c37cf63 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/ParsedItem.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/ParsedItem.swift @@ -9,7 +9,7 @@ import Foundation import RSMarkdown -public struct ParsedItem: Hashable, Sendable { +nonisolated public struct ParsedItem: Hashable, Sendable { public let syncServiceID: String? //Nil when not syncing public let uniqueID: String //RSS guid, for instance; may be calculated diff --git a/Modules/RSParser/Sources/Swift/Feeds/XML/AtomParser.swift b/Modules/RSParser/Sources/Swift/Feeds/XML/AtomParser.swift index ad6b6648f..9a060f5ca 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/XML/AtomParser.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/XML/AtomParser.swift @@ -20,7 +20,7 @@ import RSParserObjC // // In general, you should see FeedParser.swift for all your feed-parsing needs. -public struct AtomParser { +nonisolated public struct AtomParser { public static func parse(_ parserData: ParserData) -> ParsedFeed? { diff --git a/Modules/RSParser/Sources/Swift/Feeds/XML/RSParsedFeedTransformer.swift b/Modules/RSParser/Sources/Swift/Feeds/XML/RSParsedFeedTransformer.swift index e03537449..1f73bb195 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/XML/RSParsedFeedTransformer.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/XML/RSParsedFeedTransformer.swift @@ -16,7 +16,7 @@ import RSParserObjC // These functions take an RSParsedFeed and return a Swift-y ParsedFeed, // which is part of providing a single API for feed parsing. -struct RSParsedFeedTransformer { +nonisolated struct RSParsedFeedTransformer { static func parsedFeed(_ rsParsedFeed: RSParsedFeed) -> ParsedFeed { @@ -25,7 +25,7 @@ struct RSParsedFeedTransformer { } } -private extension RSParsedFeedTransformer { +nonisolated private extension RSParsedFeedTransformer { static func parsedItems(_ parsedArticles: Set) -> Set { diff --git a/Modules/RSParser/Sources/Swift/Feeds/XML/RSSParser.swift b/Modules/RSParser/Sources/Swift/Feeds/XML/RSSParser.swift index f14d9c75d..c99447fdb 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/XML/RSSParser.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/XML/RSSParser.swift @@ -17,7 +17,7 @@ import RSParserObjC // // In general, you should see FeedParser.swift for all your feed-parsing needs. -public struct RSSParser { +nonisolated public struct RSSParser { public static func parse(_ parserData: ParserData) -> ParsedFeed? { diff --git a/Modules/RSParser/Sources/Swift/JSON/JSONUtilities.swift b/Modules/RSParser/Sources/Swift/JSON/JSONUtilities.swift index 2319e0389..c2bc621a5 100644 --- a/Modules/RSParser/Sources/Swift/JSON/JSONUtilities.swift +++ b/Modules/RSParser/Sources/Swift/JSON/JSONUtilities.swift @@ -8,7 +8,7 @@ import Foundation -public struct JSONUtilities { +nonisolated public struct JSONUtilities { public static func object(with data: Data) -> Any? { diff --git a/Modules/RSParser/Sources/Swift/Utilities/String+RSParser.swift b/Modules/RSParser/Sources/Swift/Utilities/String+RSParser.swift index 9922be93a..b9b1d045a 100644 --- a/Modules/RSParser/Sources/Swift/Utilities/String+RSParser.swift +++ b/Modules/RSParser/Sources/Swift/Utilities/String+RSParser.swift @@ -8,10 +8,9 @@ import Foundation -extension String { +nonisolated extension String { var nilIfEmptyOrWhitespace: String? { return self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? nil : self } - }