Skip to content

Commit 12dd5db

Browse files
committed
use optionals instead
1 parent 2bb9750 commit 12dd5db

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

Plugins/JExtractSwiftPlugin/JExtractSwiftPlugin.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,6 @@ struct JExtractSwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
8989
arguments += ["--java-package", javaPackage]
9090
}
9191

92-
if let unsignedNumbersMode = configuration.unsignedNumbersMode {
93-
arguments += ["--unsigned-numbers", unsignedNumbersMode.rawValue]
94-
}
95-
96-
if let minimumInputAccessLevelMode = configuration.minimumInputAccessLevelMode {
97-
arguments += ["--minimum-input-access-level", minimumInputAccessLevelMode.rawValue]
98-
}
99-
100-
if let memoryManagementMode = configuration.memoryManagementMode {
101-
arguments += ["--memory-management-mode", memoryManagementMode.rawValue]
102-
}
103-
10492
let swiftFiles = sourceModule.sourceFiles.map { $0.url }.filter {
10593
$0.pathExtension == "swift"
10694
}

Sources/SwiftJavaConfigurationShared/Configuration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public struct Configuration: Codable {
3939
public var outputJavaDirectory: String?
4040

4141
public var mode: JExtractGenerationMode?
42+
public var effectiveMode: JExtractGenerationMode {
43+
mode ?? .default
44+
}
4245

4346
public var writeEmptyFiles: Bool? // FIXME: default it to false, but that plays not nice with Codable
4447

Sources/SwiftJavaConfigurationShared/GenerationMode.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public enum JExtractGenerationMode: String, Codable {
1919

2020
/// Java Native Interface
2121
case jni
22+
23+
public static var `default`: JExtractGenerationMode {
24+
.ffm
25+
}
2226
}
2327

2428
/// Configures how Swift unsigned integers should be extracted by jextract.

Sources/SwiftJavaTool/Commands/JExtractCommand.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ extension SwiftJava {
6262
var writeEmptyFiles: Bool = false
6363

6464
@Option(help: "The mode of generation to use for the output files. Used with jextract mode. By default, unsigned Swift types are imported as their bit-width compatible signed Java counterparts, and annotated using the '@Unsigned' annotation. You may choose the 'wrapGuava' mode in order to import types as class wrapper types (`UnsignedInteger` et al) defined by the Google Guava library's `com.google.common.primitives' package. that ensure complete type-safety with regards to unsigned values, however they incur an allocation and performance overhead.")
65-
var unsignedNumbers: JExtractUnsignedIntegerMode = .default
65+
var unsignedNumbers: JExtractUnsignedIntegerMode?
6666

6767
@Option(help: "The lowest access level of Swift declarations that should be extracted, defaults to 'public'.")
68-
var minimumInputAccessLevel: JExtractMinimumAccessLevelMode = .default
68+
var minimumInputAccessLevel: JExtractMinimumAccessLevelMode?
6969

7070
@Option(help: "The memory management mode to use for the generated code. By default, the user must explicitly provide `SwiftArena` to all calls that require it. By choosing `allowGlobalAutomatic`, user can omit this parameter and a global GC-based arena will be used.")
71-
var memoryManagementMode: JExtractMemoryManagementMode = .default
71+
var memoryManagementMode: JExtractMemoryManagementMode?
7272

7373
@Option(
7474
help: """
@@ -86,18 +86,14 @@ extension SwiftJava.JExtractCommand {
8686
if let javaPackage {
8787
config.javaPackage = javaPackage
8888
}
89-
if let mode {
90-
config.mode = mode
91-
} else if config.mode == nil {
92-
config.mode = .ffm
93-
}
89+
configure(&config.mode, overrideWith: mode)
9490
config.swiftModule = self.effectiveSwiftModule
9591
config.outputJavaDirectory = outputJava
9692
config.outputSwiftDirectory = outputSwift
9793
config.writeEmptyFiles = writeEmptyFiles
98-
config.unsignedNumbersMode = unsignedNumbers
99-
config.minimumInputAccessLevelMode = minimumInputAccessLevel
100-
config.memoryManagementMode = memoryManagementMode
94+
configure(&config.unsignedNumbersMode, overrideWith: unsignedNumbers)
95+
configure(&config.minimumInputAccessLevelMode, overrideWith: minimumInputAccessLevelMode)
96+
configure(&config.memoryManagementMode, overrideWith: memoryManagementMode)
10197

10298
try checkModeCompatibility()
10399

@@ -132,6 +128,12 @@ extension SwiftJava.JExtractCommand {
132128
}
133129
}
134130
}
131+
132+
func configure<T>(_ setting: inout T, overrideWith value: T?) {
133+
if let value {
134+
setting = value
135+
}
136+
}
135137
}
136138

137139
struct IncompatibleModeError: Error {

0 commit comments

Comments
 (0)