From d5e6ab24045b461d56431174bc0c863dbe92ffa1 Mon Sep 17 00:00:00 2001 From: Fumiya Tanaka Date: Sun, 3 Dec 2023 15:43:39 +0900 Subject: [PATCH] Refactor (#44) * use latest swift syntax version * Fix compile error in CsvBuilder * Refactor. --------- Co-authored-by: infinitepower18 <44692189+infinitepower18@users.noreply.github.com> --- Sources/Csv2Img/Csv.swift | 9 ++++----- Sources/Csv2Img/CsvColumn.swift | 4 ++-- Sources/Csv2Img/ImageMaker.swift | 6 ++---- Sources/Csv2Img/Maker.swift | 6 +++--- Sources/Csv2Img/PdfMaker.swift | 19 +++++++++---------- Sources/Csv2Img/TypeConvertible.swift | 7 ++++--- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Sources/Csv2Img/Csv.swift b/Sources/Csv2Img/Csv.swift index 80d7da8..6ed0aea 100644 --- a/Sources/Csv2Img/Csv.swift +++ b/Sources/Csv2Img/Csv.swift @@ -130,7 +130,7 @@ extension Csv { /** `ExportType` is a enum that expresses */ - public enum ExportType: String, Hashable { + public enum ExportType: String, Hashable, CaseIterable { /// `png` output case png /// `pdf` output (Work In Progress) @@ -365,10 +365,9 @@ extension Csv { } if let maker = maker as? ImageMaker { if let fontSize = fontSize { - maker.setFontSize(fontSize) + maker.set(fontSize: fontSize) } - // TODO: When Swift5.7 is supported officailly, replace `CGImage` with `any CsvExportable`. - let exportable: CGImage = try await withCheckedThrowingContinuation { continuation in + let exportable: any CsvExportable = try await withCheckedThrowingContinuation { continuation in queue.async { [weak self] in guard let self = self else { continuation.resume(throwing: Csv.Error.underlying(nil)) @@ -389,7 +388,7 @@ extension Csv { return AnyCsvExportable(exportable) } else if let maker = maker as? PdfMaker { if let fontSize = fontSize { - maker.setFontSize(fontSize) + maker.set(fontSize: fontSize) } let exportable: PDFDocument = try await withCheckedThrowingContinuation { continuation in queue.async { [weak self] in diff --git a/Sources/Csv2Img/CsvColumn.swift b/Sources/Csv2Img/CsvColumn.swift index 41f941b..8660413 100644 --- a/Sources/Csv2Img/CsvColumn.swift +++ b/Sources/Csv2Img/CsvColumn.swift @@ -24,7 +24,7 @@ extension Csv { /// →Column is [1, 2, 3, 4] and Row is [5, 6, 7, 8]. /// /// Because this class is usually initialized via ``Csv``, you do not have to take care about ``Column`` in detail. - public struct Column { + public struct Column: Sendable { public var name: Name public var style: Style @@ -37,7 +37,7 @@ extension Csv { extension Csv.Column { /// ``Style`` decides the appearance of certain ``Column`` group. - public struct Style { + public struct Style: Sendable { /// `color` is a ``CGColor`` corresponding to textColor which is used when drawing public var color: CGColor /// `applyOnlyColumn` determines whether this style affects both `Column` and `Row` or not. diff --git a/Sources/Csv2Img/ImageMaker.swift b/Sources/Csv2Img/ImageMaker.swift index b54c1a4..f6f79bf 100644 --- a/Sources/Csv2Img/ImageMaker.swift +++ b/Sources/Csv2Img/ImageMaker.swift @@ -25,12 +25,10 @@ public enum ImageMakingError: Error { /// No overview available protocol ImageMakerType: Maker { var latestOutput: CGImage? { get } - func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping (Double) -> Void) throws -> CGImage - func setFontSize(_ size: Double) } /// `ImageMarker` generate png-image from ``Csv``. -class ImageMaker: ImageMakerType { +final class ImageMaker: ImageMakerType { typealias Exportable = CGImage @@ -48,7 +46,7 @@ class ImageMaker: ImageMakerType { var latestOutput: CGImage? - func setFontSize(_ size: Double) { + func set(fontSize size: Double) { self.fontSize = size } diff --git a/Sources/Csv2Img/Maker.swift b/Sources/Csv2Img/Maker.swift index 6ab3f88..6cac727 100644 --- a/Sources/Csv2Img/Maker.swift +++ b/Sources/Csv2Img/Maker.swift @@ -1,10 +1,10 @@ import Foundation -public protocol Maker { +public protocol Maker: Sendable { associatedtype Exportable: CsvExportable var maximumRowCount: Int? { get } - func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping (Double) -> Void) throws -> Exportable - func setFontSize(_ size: Double) + func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping @Sendable (Double) -> Void) throws -> Exportable + func set(fontSize size: Double) } diff --git a/Sources/Csv2Img/PdfMaker.swift b/Sources/Csv2Img/PdfMaker.swift index 05b96ba..acefdd0 100644 --- a/Sources/Csv2Img/PdfMaker.swift +++ b/Sources/Csv2Img/PdfMaker.swift @@ -6,6 +6,7 @@ public enum PdfMakingError: Error { /// Failed to get/create `CGContext`. case noContextAvailabe case failedToGeneratePdf + case failedToSavePdf(at : String) case emptyRows case underlying(Error) } @@ -13,13 +14,11 @@ public enum PdfMakingError: Error { /// No overview available protocol PdfMakerType: Maker { var latestOutput: PDFDocument? { get } - func make(columns: [Csv.Column], rows: [Csv.Row], progress: @escaping (Double) -> Void) throws -> PDFDocument func setMetadata(_ metadata: PDFMetadata) - func setFontSize(_ size: Double) } /// ``PdfMaker`` generate pdf from ``Csv`` (Work In Progress). -class PdfMaker: PdfMakerType { +final class PdfMaker: PdfMakerType { typealias Exportable = PDFDocument @@ -33,17 +32,16 @@ class PdfMaker: PdfMakerType { self.metadata = metadata } - var maximumRowCount: Int? - var fontSize: Double + let maximumRowCount: Int? + private(set) var fontSize: Double var metadata: PDFMetadata var latestOutput: PDFDocument? - func setFontSize(_ size: Double) { + func set(fontSize size: Double) { self.fontSize = size } - /// generate png-image data from ``Csv``. func make( columns: [Csv.Column], @@ -53,6 +51,7 @@ class PdfMaker: PdfMakerType { // NOTE: Anchor is bottom-left. let horizontalSpace: Double = 8 let verticalSpace: Double = 12 + let maxRowsHeight: Double = 480 let size = min(maximumRowCount ?? rows.count, rows.count) let rows = rows[..