Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds TLE collection parser, rm IDE directories #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ fastlane/test_output

iOSInjectionProject/
Package.resolved
.idea
.swiftpm
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

16 changes: 0 additions & 16 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

2 changes: 0 additions & 2 deletions .idea/swift-sgp.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

14 changes: 14 additions & 0 deletions Sources/SGPKit/Array+chunked.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Array+chunked.swift
//
//
// Created by p on 5/4/23.
//

extension Array {
func chunked(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
}
28 changes: 23 additions & 5 deletions Sources/SGPKit/TLEParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import Foundation

/// A TLE parser
public final class TLEParser {
public struct TLEParser {

/// Describes all the possible errors that can be thrown while parsing
public enum Error: Swift.Error {
Expand All @@ -48,7 +48,7 @@ public final class TLEParser {
/// - Parameter data: the buffer to parse
/// - Returns: a TLE model
/// - Throws: TLEParser.Error
public func parse(_ data: Data) throws -> TLE {
static public func parse(_ data: Data) throws -> TLE {
guard !data.isEmpty else {
throw Error.empty
}
Expand All @@ -58,9 +58,9 @@ public final class TLEParser {
}

let lines = string
.trimmingCharacters(in: .newlines)
.split(separator: "\n")
.map({ String($0) })
.split(whereSeparator: { $0.isNewline })
.map({ String($0).trimmingCharacters(in: .whitespacesAndNewlines) })
.filter { $0.count > 0 }

guard lines.count == 3 else {
throw Error.wrongLineCount(lines.count)
Expand All @@ -78,4 +78,22 @@ public final class TLEParser {
secondLine: lines[2]
)
}

static public func parseCollection(_ data: Data, encoding: String.Encoding = .ascii) throws -> [TLE] {
guard !data.isEmpty else { throw Error.empty }
guard let dataString = String(data: data, encoding: encoding) else {
throw Error.encodingError
}

let lines = dataString
.split(whereSeparator: { $0.isNewline })
.map({ String($0).trimmingCharacters(in: .whitespacesAndNewlines) })
.filter { $0.count > 0 }

if lines.count % 3 != 0 { throw Error.invalidLineLength }

let elements = lines.chunked(into: 3)
let tles = elements.map({ TLE(title: $0[0], firstLine: $0[1], secondLine: $0[2]) })
return tles
}
}
12 changes: 4 additions & 8 deletions Tests/SGPKitTests/TLEParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ final class TLEParserTests: QuickSpec {
context("when parsing an empty buffer") {
it("should throw an exception") {
let emptyBuffer = Data()
let parser = TLEParser()

expect { try parser.parse(emptyBuffer) }.to(throwError(TLEParser.Error.empty))
expect { try TLEParser.parse(emptyBuffer) }.to(throwError(TLEParser.Error.empty))
}
}

context("when parsing a not empty buffer") {
context("if the buffer contains valid data") {
it("should return a TLE model") {
let parser = TLEParser()
let validData = self.loadValidTLEData()

let expectedTLE = TLE(
Expand All @@ -53,7 +51,7 @@ final class TLEParserTests: QuickSpec {
)

do {
let tle = try parser.parse(validData)
let tle = try TLEParser.parse(validData)
expect(tle.title).to(equal(expectedTLE.title))
expect(tle.firstLine).to(equal(expectedTLE.firstLine))
expect(tle.secondLine).to(equal(expectedTLE.secondLine))
Expand All @@ -65,11 +63,10 @@ final class TLEParserTests: QuickSpec {

context("if the encoded TLE doesn't have 3 lines") {
it("should raise an exception") {
let parser = TLEParser()
let invalidData = self.loadOneLineTLEData()

do {
_ = try parser.parse(invalidData)
_ = try TLEParser.parse(invalidData)
fail()
} catch let tleParserError as TLEParser.Error {
if case let TLEParser.Error.wrongLineCount(count) = tleParserError {
Expand All @@ -85,11 +82,10 @@ final class TLEParserTests: QuickSpec {

context("if not all the lines of the TLE have 69 characters") {
it("should raise an exception") {
let parser = TLEParser()
let invalidData = self.loadInvalidLineLengthTLEData()

do {
_ = try parser.parse(invalidData)
_ = try TLEParser.parse(invalidData)
fail()
} catch let tleParserError as TLEParser.Error {
if case TLEParser.Error.invalidLineLength = tleParserError {
Expand Down