Skip to content

Commit

Permalink
support Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
nixzhu committed Jun 21, 2024
1 parent d1222fc commit 2b6973d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 24 deletions.
62 changes: 42 additions & 20 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@

import PackageDescription

#if os(Linux)
let packageDependencies: [Package.Dependency] = [
.package(
url: "https://github.com/ibireme/yyjson.git",
from: "0.9.0"
),
]
#else
let packageDependencies: [Package.Dependency] = [
.package(
url: "https://github.com/ibireme/yyjson.git",
from: "0.9.0"
),
.package(
url: "https://github.com/michaeleisel/JJLISO8601DateFormatter.git",
from: "0.1.8"
),
]
#endif

#if os(Linux)
let targetDependencies: [Target.Dependency] = [
.product(
name: "yyjson",
package: "yyjson"
),
]
#else
let targetDependencies: [Target.Dependency] = [
.product(
name: "yyjson",
package: "yyjson"
),
.product(
name: "JJLISO8601DateFormatter",
package: "JJLISO8601DateFormatter"
),
]
#endif

let package = Package(
name: "Ananda",
products: [
Expand All @@ -10,29 +50,11 @@ let package = Package(
targets: ["Ananda"]
),
],
dependencies: [
.package(
url: "https://github.com/ibireme/yyjson.git",
from: "0.9.0"
),
.package(
url: "https://github.com/michaeleisel/JJLISO8601DateFormatter.git",
from: "0.1.8"
),
],
dependencies: packageDependencies,
targets: [
.target(
name: "Ananda",
dependencies: [
.product(
name: "yyjson",
package: "yyjson"
),
.product(
name: "JJLISO8601DateFormatter",
package: "JJLISO8601DateFormatter"
),
],
dependencies: targetDependencies,
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"),
]
Expand Down
54 changes: 50 additions & 4 deletions Sources/Ananda/AnandaValueExtractor.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Foundation

#if !os(Linux)
import JJLISO8601DateFormatter
#endif

/// AnandaValueExtractor
///
Expand Down Expand Up @@ -82,9 +85,15 @@ public struct AnandaValueExtractor: Sendable {
return .init(timeIntervalSince1970: value)
}

#if os(Linux)
if let date = ISO8601DateFormatter.ananda_date(from: string) {
return date
}
#else
if let date = JJLISO8601DateFormatter.ananda_date(from: string) {
return date
}
#endif
}

return nil
Expand Down Expand Up @@ -114,7 +123,43 @@ public struct AnandaValueExtractor: Sendable {
}
}

extension JJLISO8601DateFormatter: @unchecked Sendable {
#if os(Linux)
extension ISO8601DateFormatter {
public static func ananda_date(from string: String) -> Date? {
if let date = ananda_iso8601A.date(from: string) {
return date
}

if let date = ananda_iso8601B.date(from: string) {
return date
}

return nil
}

private nonisolated(unsafe) static let ananda_iso8601A: ISO8601DateFormatter = {
let dateFormatter = ISO8601DateFormatter()

dateFormatter.formatOptions = [
.withInternetDateTime,
.withFractionalSeconds,
]

return dateFormatter
}()

private nonisolated(unsafe) static let ananda_iso8601B: ISO8601DateFormatter = {
let dateFormatter = ISO8601DateFormatter()

dateFormatter.formatOptions = [
.withInternetDateTime,
]

return dateFormatter
}()
}
#else
extension JJLISO8601DateFormatter {
public static func ananda_date(from string: String) -> Date? {
if let date = ananda_iso8601A.date(from: string) {
return date
Expand All @@ -127,7 +172,7 @@ extension JJLISO8601DateFormatter: @unchecked Sendable {
return nil
}

private static let ananda_iso8601A: JJLISO8601DateFormatter = {
private nonisolated(unsafe) static let ananda_iso8601A: JJLISO8601DateFormatter = {
let dateFormatter = JJLISO8601DateFormatter()

dateFormatter.formatOptions = [
Expand All @@ -138,7 +183,7 @@ extension JJLISO8601DateFormatter: @unchecked Sendable {
return dateFormatter
}()

private static let ananda_iso8601B: JJLISO8601DateFormatter = {
private nonisolated(unsafe) static let ananda_iso8601B: JJLISO8601DateFormatter = {
let dateFormatter = JJLISO8601DateFormatter()

dateFormatter.formatOptions = [
Expand All @@ -148,9 +193,10 @@ extension JJLISO8601DateFormatter: @unchecked Sendable {
return dateFormatter
}()
}
#endif

extension CharacterSet {
public static let ananda_url: Self = {
public nonisolated(unsafe) static let ananda_url: Self = {
var set = CharacterSet.urlQueryAllowed
set.insert("#")
set.formUnion(.urlPathAllowed)
Expand Down

0 comments on commit 2b6973d

Please sign in to comment.