Skip to content

Commit 080d1c8

Browse files
committed
Use LosslessStringConvertible for canonical representations
1 parent a38a51d commit 080d1c8

File tree

15 files changed

+61
-45
lines changed

15 files changed

+61
-45
lines changed

Sources/SwiftNASR/Cycle.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import Foundation
55
time period. Cycles are defined by the first day of their effectivity period.
66
*/
77

8-
public struct Cycle: Codable, CustomStringConvertible, Sendable, Identifiable, Equatable, Hashable {
8+
public struct Cycle: Codable, LosslessStringConvertible, Sendable, Identifiable, Equatable, Hashable
9+
{
910
/// The earliest reference cycle (not necessarily the earliest cycle for
1011
/// which data is available, but the earliest representable date for a
1112
/// cycle).
@@ -98,9 +99,24 @@ public struct Cycle: Codable, CustomStringConvertible, Sendable, Identifiable, E
9899

99100
public var id: String { description }
100101

102+
/// Creates a cycle by parsing a `YYYY-MM-DD` string.
103+
///
104+
/// - Parameter description: A string in `YYYY-MM-DD` format.
105+
public init?(_ description: String) {
106+
let parts = description.split(separator: "-")
107+
guard parts.count == 3,
108+
let year = UInt(parts[0]),
109+
let month = UInt8(parts[1]),
110+
let day = UInt8(parts[2]),
111+
month >= 1, month <= 12,
112+
day >= 1, day <= 31
113+
else { return nil }
114+
self.init(year: year, month: month, day: day)
115+
}
116+
101117
/**
102118
Generates a cycle from a month, day, and year. Does not validate the cycle.
103-
119+
104120
- Parameter year: The cycle year (2020 or later.
105121
- Parameter month: The cycle month (1–12).
106122
- Parameter day: The cycle day (1–31).
@@ -113,7 +129,7 @@ public struct Cycle: Codable, CustomStringConvertible, Sendable, Identifiable, E
113129

114130
/**
115131
Returns the cycle whose effectivity period includes the given date.
116-
132+
117133
- Parameter date: The date to use.
118134
- Returns: The cycle covering that date, or `nil` if the date is before the
119135
``datum`` date.
@@ -147,7 +163,7 @@ public struct Cycle: Codable, CustomStringConvertible, Sendable, Identifiable, E
147163

148164
/**
149165
Returns whether a given date falls within this cycle.
150-
166+
151167
- Parameter date: A date to check.
152168
- Returns: Whether the date falls within the cycle's effectivity period.
153169
*/

Sources/SwiftNASR/Distribution/ArchiveDataDistribution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public final class ArchiveDataDistribution: Distribution {
2323

2424
/**
2525
Creates a new instance from the given data.
26-
26+
2727
- Parameter data: The compressed distribution.
2828
- Parameter format: The data format (defaults to .txt for backward compatibility)
2929
- Throws: If the archive could not be read.

Sources/SwiftNASR/Distribution/ArchiveFileDistribution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class ArchiveFileDistribution: Distribution {
2424

2525
/**
2626
Creates a new instance from the given file.
27-
27+
2828
- Parameter location: The path to the compressed distribution file.
2929
- Parameter format: The data format (defaults to .txt for backward compatibility)
3030
- Throws: If the archive could not be read.

Sources/SwiftNASR/Distribution/DirectoryDistribution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public final class DirectoryDistribution: Distribution {
1919

2020
/**
2121
Creates a new instance from the given directory.
22-
22+
2323
- Parameter location: The path to the distribution directory.
2424
- Parameter format: The data format (defaults to .txt for backward compatibility)
2525
*/

Sources/SwiftNASR/Distribution/Distribution+Cycle.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension Distribution {
2424

2525
/**
2626
Default implementation that reads the cycle from the README file.
27-
27+
2828
- Returns: The parsed cycle, or `nil` if the cycle could not be parsed.
2929
*/
3030

@@ -62,9 +62,9 @@ extension Distribution {
6262

6363
/**
6464
Parses a cycle from a CSV-style date string.
65-
65+
6666
Expected format: DD_MMM_YYYY (e.g., 04_Sep_2025)
67-
67+
6868
- Parameter dateString: The date string to parse
6969
- Returns: The parsed cycle, or `nil` if the cycle could not be parsed.
7070
*/

Sources/SwiftNASR/Distribution/Distribution.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public protocol Distribution: Sendable {
9090

9191
/**
9292
Locates a file in the distribution by its prefix.
93-
93+
9494
- Parameter prefix: The filename prefix.
9595
- Returns: The first matching file, or `nil` if no file names match the
9696
prefix.
@@ -100,7 +100,7 @@ public protocol Distribution: Sendable {
100100

101101
/**
102102
Decompresses and reads a file asynchronously from a distribution.
103-
103+
104104
- Parameter path: The path to the file.
105105
- Parameter progressHandler: A block that receives the Progress object when
106106
the task begins. You can add it to your parent
@@ -120,18 +120,18 @@ public protocol Distribution: Sendable {
120120

121121
/**
122122
Reads the cycle from the distribution.
123-
123+
124124
- Returns: The parsed cycle, or `nil` if the cycle could not be parsed.
125125
*/
126126
func readCycle() async throws -> Cycle?
127127

128128
/**
129129
Reads a file as raw data chunks without line splitting.
130-
130+
131131
This method streams the file content as raw `Data` chunks, suitable for
132132
parsers that handle their own buffering and line detection (such as CSV
133133
parsers with multi-line quoted fields).
134-
134+
135135
- Parameter path: The path to the file within the distribution.
136136
- Parameter progressHandler: A block that receives the Progress object when
137137
the task begins.
@@ -148,7 +148,7 @@ extension Distribution {
148148

149149
/**
150150
Reads the data for a given record type from the distribution.
151-
151+
152152
- Parameter type: The record type to read data for.
153153
- Parameter progressHandler: A block that receives the Progress object when
154154
the task begins. You can add it to your parent Progress.

Sources/SwiftNASR/Downloaders/Downloader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public protocol Downloader: Loader {
1919

2020
/**
2121
Creates a downloader for a given cycle.
22-
22+
2323
- Parameter cycle: The cycle to download NASR data for. If not specified,
2424
uses the current cycle.
2525
- Parameter format: The data format to download (defaults to .txt).
@@ -29,7 +29,7 @@ public protocol Downloader: Loader {
2929

3030
/**
3131
Downloads the NASR data asynchronously.
32-
32+
3333
- Returns: The downloaded distribution, and an object for tracking progress.
3434
- Throws: If the distribution could not be downloaded.
3535
*/

Sources/SwiftNASR/Error.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ public enum Error: Swift.Error {
88

99
/**
1010
Received a bad HTTP response.
11-
11+
1212
- Parameter response: The HTTP response.
1313
*/
1414
case badResponse(_ response: URLResponse)
1515

1616
/**
1717
Download failed for a specific reason.
18-
18+
1919
- Parameter reason: The reason for the failure.
2020
*/
2121
case downloadFailed(reason: String)
@@ -35,59 +35,59 @@ public enum Error: Swift.Error {
3535

3636
/**
3737
Parsed an unknown runway surface identifier.
38-
38+
3939
- Parameter value: The unknown identifier.
4040
*/
4141
case invalidRunwaySurface(_ value: String)
4242

4343
/**
4444
Parsed an unknown pavement classification identifier.
45-
45+
4646
- Parameter value: The unknown identifier.
4747
*/
4848
case invalidPavementClassification(_ value: String)
4949

5050
/**
5151
Parsed an unknown VGSI identifier.
52-
52+
5353
- Parameter value: The unknown identifier.
5454
*/
5555
case invalidVGSI(_ value: String)
5656

5757
/**
5858
Parsed an unknown ARTCC identifier.
59-
59+
6060
- Parameter ID: The unknown identifier.
6161
*/
6262
case unknownARTCC(_ ID: String)
6363

6464
/**
6565
Parsed an ARTCC frequency record for a frequency not associated with the
6666
ARTCC.
67-
67+
6868
- Parameter frequency: The unknown frequency.
6969
- Parameter ARTCC: The ARTCC record.
7070
*/
7171
case unknownARTCCFrequency(_ frequency: UInt, ARTCC: ARTCC)
7272

7373
/***
7474
Parsed an unknown navigation aid.
75-
75+
7676
- Parameter ID: The unknown identifier.
7777
*/
7878
case unknownNavaid(_ ID: String)
7979

8080
/**
8181
Parsed an unknown ARTCC data field identifier.
82-
82+
8383
- Parameter fieldId: The unknown identifier.
8484
- Parameter ARTCC: The ARTCC record.
8585
*/
8686
case unknownFieldId(_ fieldId: String, ARTCC: ARTCC)
8787

8888
/**
8989
Parsed an unknown ARTCC frequency data field identifier.
90-
90+
9191
- Parameter fieldId: The unknown identifier.
9292
- Parameter frequency: The associated frequency.
9393
- Parameter ARTCC: The ARTCC record.
@@ -96,21 +96,21 @@ public enum Error: Swift.Error {
9696

9797
/**
9898
Attempted to parse an invalid frequency.
99-
99+
100100
- Parameter string: The invalid frequency string.
101101
*/
102102
case invalidFrequency(_ string: String)
103103

104104
/**
105105
Attempted to parse data for an unknown FSS ID.
106-
106+
107107
- Parameter string: The unknown FSS ID.
108108
*/
109109
case unknownFSS(_ ID: String)
110110

111111
/**
112112
Attempted to parse an invalid altitude format.
113-
113+
114114
- Parameter string: The invalid altitude string.
115115
*/
116116
case invalidAltitudeFormat(_ string: String)

Sources/SwiftNASR/Loaders/ArchiveLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public final class ArchiveLoader: Loader {
2020

2121
/**
2222
Creates a loader that loads from a given location on disk.
23-
23+
2424
- Parameter location: The location of the archive file on disk.
2525
- Parameter format: The data format (.txt or .csv). Defaults to .txt.
2626
*/

Sources/SwiftNASR/Loaders/DirectoryLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class DirectoryLoader: Loader {
1414

1515
/**
1616
Creates a loader that loads from a given location on disk.
17-
17+
1818
- Parameter location: The location of the distribution directory on disk.
1919
- Parameter format: The data format (defaults to .txt for backward compatibility)
2020
*/

0 commit comments

Comments
 (0)