Skip to content

Commit 3b3fc83

Browse files
committed
chore: Cleanup
1 parent 21e7107 commit 3b3fc83

1 file changed

Lines changed: 34 additions & 30 deletions

File tree

SwiftFITS/FITSSection.swift

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,49 +97,53 @@ public class FITSSection: CustomStringConvertible
9797
{
9898
if self.kind == .header || self.kind == .xtension
9999
{
100-
let properties = try self.data.chunked( by: 80 ).map
100+
let properties = try FITSSection.readAndMergeProperties( data: self.data )
101+
102+
if properties.count( where: { $0.name == "END" } ) > 1
101103
{
102-
try FITSProperty( data: $0 )
104+
throw FITSError.invalidBlockData( reason: "Multiple end markers found" )
103105
}
104106

105-
let merged = try properties.reduce( into: [ FITSProperty ]() )
107+
guard let index = properties.firstIndex( where: { $0.name == "END" } )
108+
else
106109
{
107-
if $1.name == "CONTINUE"
108-
{
109-
guard let last = $0.last
110-
else
111-
{
112-
throw FITSError.invalidBlockData( reason: "No previous property to continue" )
113-
}
114-
115-
try last.merge( with: $1 )
116-
}
117-
else if let last = $0.last, last.name == "HISTORY", $1.name == "HISTORY"
118-
{
119-
try last.merge( with: $1 )
120-
}
121-
else if let last = $0.last, last.name == "COMMENT", $1.name == "COMMENT"
122-
{
123-
try last.merge( with: $1 )
124-
}
110+
throw FITSError.invalidBlockData( reason: "No end marker found" )
111+
}
112+
113+
self.properties = Array( properties[ 0 ..< index ] )
114+
}
115+
}
116+
117+
private class func readAndMergeProperties( data: Data ) throws -> [ FITSProperty ]
118+
{
119+
try data.chunked( by: 80 ).map
120+
{
121+
try FITSProperty( data: $0 )
122+
}
123+
.reduce( into: [ FITSProperty ]() )
124+
{
125+
if $1.name == "CONTINUE"
126+
{
127+
guard let last = $0.last
125128
else
126129
{
127-
$0.append( $1 )
130+
throw FITSError.invalidBlockData( reason: "No previous property to continue" )
128131
}
132+
133+
try last.merge( with: $1 )
129134
}
130-
131-
if merged.count( where: { $0.name == "END" } ) > 1
135+
else if let last = $0.last, last.name == "HISTORY", $1.name == "HISTORY"
132136
{
133-
throw FITSError.invalidBlockData( reason: "Multiple end markers found" )
137+
try last.merge( with: $1 )
138+
}
139+
else if let last = $0.last, last.name == "COMMENT", $1.name == "COMMENT"
140+
{
141+
try last.merge( with: $1 )
134142
}
135-
136-
guard let index = merged.firstIndex( where: { $0.name == "END" } )
137143
else
138144
{
139-
throw FITSError.invalidBlockData( reason: "No end marker found" )
145+
$0.append( $1 )
140146
}
141-
142-
self.properties = Array( merged[ 0 ..< index ] )
143147
}
144148
}
145149

0 commit comments

Comments
 (0)