feat: Initial csv parsing for uploading rooms for a project. Need to style the upload form.
All checks were successful
CI / Linux Tests (push) Successful in 5m41s
All checks were successful
CI / Linux Tests (push) Successful in 5m41s
This commit is contained in:
43
Sources/CSVParser/Interface.swift
Normal file
43
Sources/CSVParser/Interface.swift
Normal file
@@ -0,0 +1,43 @@
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import ManualDCore
|
||||
import Parsing
|
||||
|
||||
extension DependencyValues {
|
||||
public var csvParser: CSVParser {
|
||||
get { self[CSVParser.self] }
|
||||
set { self[CSVParser.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
@DependencyClient
|
||||
public struct CSVParser: Sendable {
|
||||
public var parseRooms: @Sendable (Room.CSV) async throws -> [Room.Create]
|
||||
}
|
||||
|
||||
extension CSVParser: DependencyKey {
|
||||
public static let testValue = Self()
|
||||
|
||||
public static let liveValue = Self(
|
||||
parseRooms: { csv in
|
||||
guard let string = String(data: csv.file, encoding: .utf8) else {
|
||||
throw CSVParsingError("Unreadable file data")
|
||||
}
|
||||
let rows = try RoomCSVParser().parse(string[...].utf8)
|
||||
let rooms = rows.reduce(into: [Room.Create]()) {
|
||||
if case .room(let room) = $1 {
|
||||
$0.append(room)
|
||||
}
|
||||
}
|
||||
return rooms
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
public struct CSVParsingError: Error {
|
||||
let reason: String
|
||||
|
||||
public init(_ reason: String) {
|
||||
self.reason = reason
|
||||
}
|
||||
}
|
||||
51
Sources/CSVParser/Internal/Room+parsing.swift
Normal file
51
Sources/CSVParser/Internal/Room+parsing.swift
Normal file
@@ -0,0 +1,51 @@
|
||||
import ManualDCore
|
||||
import Parsing
|
||||
|
||||
struct RoomCSVParser: Parser {
|
||||
var body: some Parser<Substring.UTF8View, [RoomRowType]> {
|
||||
Many {
|
||||
RoomRowParser()
|
||||
} separator: {
|
||||
"\n".utf8
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RoomRowParser: Parser {
|
||||
|
||||
var body: some Parser<Substring.UTF8View, RoomRowType> {
|
||||
OneOf {
|
||||
RoomCreateParser().map { RoomRowType.room($0) }
|
||||
Prefix { $0 != UInt8(ascii: "\n") }
|
||||
.map(.string)
|
||||
.map { RoomRowType.header($0) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum RoomRowType {
|
||||
case header(String)
|
||||
case room(Room.Create)
|
||||
}
|
||||
|
||||
struct RoomCreateParser: ParserPrinter {
|
||||
|
||||
var body: some ParserPrinter<Substring.UTF8View, Room.Create> {
|
||||
ParsePrint {
|
||||
Prefix { $0 != UInt8(ascii: ",") }.map(.string)
|
||||
",".utf8
|
||||
Double.parser()
|
||||
",".utf8
|
||||
Optionally {
|
||||
Double.parser()
|
||||
}
|
||||
",".utf8
|
||||
Optionally {
|
||||
Double.parser()
|
||||
}
|
||||
",".utf8
|
||||
Int.parser()
|
||||
}
|
||||
.map(.memberwise(Room.Create.init))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user