Skip to content

Commit

Permalink
chore: format all swift files
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Aug 5, 2024
1 parent db74a21 commit f538c97
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 148 deletions.
6 changes: 5 additions & 1 deletion swift/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ all: build

.PHONY: build
build: cargo
swift build
swift build -c release

.PHONY: fmt
fmt:
swift format

.PHONY: cargo
cargo:
Expand Down
251 changes: 144 additions & 107 deletions swift/Sources/KclLib/API.swift
Original file line number Diff line number Diff line change
@@ -1,112 +1,149 @@
import CKclLib
import Foundation
import SwiftProtobuf
import CKclLib

public class API: Service {
private static let ERROR_PREFIX = "ERROR:"

public init() {}

// Parses a single KCL file and returns its Abstract Syntax Tree (AST) as a JSON string.
public func parseFile(_ args: ParseFile_Args) throws -> ParseFile_Result {
return try ParseFile_Result(serializedBytes: callNative(name: "KclvmService.ParseFile", args: try args.serializedBytes()))
}

// Parses a KCL program and returns the Abstract Syntax Tree (AST) in JSON format.
public func parseProgram(_ args: ParseProgram_Args) throws -> ParseProgram_Result {
return try ParseProgram_Result(serializedBytes: callNative(name: "KclvmService.ParseProgram", args: try args.serializedBytes()))
}

// Loads a KCL package and retrieves AST, symbol, type, and definition information.
public func loadPackage(_ args: LoadPackage_Args) throws -> LoadPackage_Result {
return try LoadPackage_Result(serializedBytes: callNative(name: "KclvmService.LoadPackage", args: try args.serializedBytes()))
}

// Executes a KCL file with provided arguments.
public func execProgram(_ args: ExecProgram_Args) throws -> ExecProgram_Result {
return try ExecProgram_Result(serializedBytes: callNative(name: "KclvmService.ExecProgram", args: try args.serializedBytes()))
}

// Overrides specified elements in a KCL file according to given arguments.
public func overrideFile(_ args: OverrideFile_Args) throws -> OverrideFile_Result {
return try OverrideFile_Result(serializedBytes: callNative(name: "KclvmService.OverrideFile", args: try args.serializedBytes()))
}

// Lists all variables declared in a KCL file.
public func listVariables(_ args: ListVariables_Args) throws -> ListVariables_Result {
return try ListVariables_Result(serializedBytes: callNative(name: "KclvmService.ListVariables", args: try args.serializedBytes()))
}

// Lists all options defined in a KCL program.
public func listOptions(_ args: ParseProgram_Args) throws -> ListOptions_Result {
return try ListOptions_Result(serializedBytes: callNative(name: "KclvmService.ListOptions", args: try args.serializedBytes()))
}

// Retrieves the full schema type mapping for a KCL program.
public func getSchemaTypeMapping(_ args: GetSchemaTypeMapping_Args) throws -> GetSchemaTypeMapping_Result {
return try GetSchemaTypeMapping_Result(serializedBytes: callNative(name: "KclvmService.GetSchemaTypeMapping", args: try args.serializedBytes()))
}

// Formats source code according to KCL style guidelines.
public func formatCode(_ args: FormatCode_Args) throws -> FormatCode_Result {
return try FormatCode_Result(serializedBytes: callNative(name: "KclvmService.FormatCode", args: try args.serializedBytes()))
}

// Formats KCL files or directories to conform to style guidelines.
public func formatPath(_ args: FormatPath_Args) throws -> FormatPath_Result {
return try FormatPath_Result(serializedBytes: callNative(name: "KclvmService.FormatPath", args: try args.serializedBytes()))
}

// Runs linting checks on KCL files and reports errors and warnings.
public func lintPath(_ args: LintPath_Args) throws -> LintPath_Result {
return try LintPath_Result(serializedBytes: callNative(name: "KclvmService.LintPath", args: try args.serializedBytes()))
}

// Validates a data string against a schema defined in a KCL code string.
public func validateCode(_ args: ValidateCode_Args) throws -> ValidateCode_Result {
return try ValidateCode_Result(serializedBytes: callNative(name: "KclvmService.ValidateCode", args: try args.serializedBytes()))
}

// Builds configuration from settings files.
public func loadSettingsFiles(_ args: LoadSettingsFiles_Args) throws -> LoadSettingsFiles_Result {
return try LoadSettingsFiles_Result(serializedBytes: callNative(name: "KclvmService.LoadSettingsFiles", args: try args.serializedBytes()))
}

// Renames symbols across files within a KCL package.
public func rename(_ args: Rename_Args) throws -> Rename_Result {
return try Rename_Result(serializedBytes: callNative(name: "KclvmService.Rename", args: try args.serializedBytes()))
}

// Renames symbols in source code without modifying files directly.
public func renameCode(_ args: RenameCode_Args) throws -> RenameCode_Result {
return try RenameCode_Result(serializedBytes: callNative(name: "KclvmService.RenameCode", args: try args.serializedBytes()))
}

// Executes tests on KCL packages using specified test arguments.
public func test(_ args: Test_Args) throws -> Test_Result {
return try Test_Result(serializedBytes: callNative(name: "KclvmService.Test", args: try args.serializedBytes()))
}

// Updates dependencies for a KCL project based on defined specifications.
public func updateDependencies(_ args: UpdateDependencies_Args) throws -> UpdateDependencies_Result {
return try UpdateDependencies_Result(serializedBytes: callNative(name: "KclvmService.UpdateDependencies", args: try args.serializedBytes()))
}

// Retrieves version information about the KCL service.
public func getVersion(_ args: GetVersion_Args) throws -> GetVersion_Result {
return try GetVersion_Result(serializedBytes: callNative(name: "KclvmService.GetVersion", args: try args.serializedBytes()))
}

private func callNative(name: String, args: Data) -> Data {
// Convert name to byte array
let nameBytes = [UInt8](name.utf8)
var resultBuf = [UInt8](repeating: 0, count: 2048 * 2048)
let resultLength = CKclLib.callNative(nameBytes, UInt(nameBytes.count), [UInt8](args), UInt(args.count), &resultBuf)
let result = Data(bytes: resultBuf, count: Int(resultLength))
let resultString = String(bytes: result, encoding: .utf8) ?? ""
if resultString.isEmpty || !resultString.hasPrefix(API.ERROR_PREFIX) {
return result
}
fatalError(String(resultString.dropFirst(API.ERROR_PREFIX.count)))
}
private static let ERROR_PREFIX = "ERROR:"

public init() {}

// Parses a single KCL file and returns its Abstract Syntax Tree (AST) as a JSON string.
public func parseFile(_ args: ParseFile_Args) throws -> ParseFile_Result {
return try ParseFile_Result(
serializedBytes: callNative(name: "KclvmService.ParseFile", args: try args.serializedBytes()))
}

// Parses a KCL program and returns the Abstract Syntax Tree (AST) in JSON format.
public func parseProgram(_ args: ParseProgram_Args) throws -> ParseProgram_Result {
return try ParseProgram_Result(
serializedBytes: callNative(
name: "KclvmService.ParseProgram", args: try args.serializedBytes()))
}

// Loads a KCL package and retrieves AST, symbol, type, and definition information.
public func loadPackage(_ args: LoadPackage_Args) throws -> LoadPackage_Result {
return try LoadPackage_Result(
serializedBytes: callNative(
name: "KclvmService.LoadPackage", args: try args.serializedBytes()))
}

// Executes a KCL file with provided arguments.
public func execProgram(_ args: ExecProgram_Args) throws -> ExecProgram_Result {
return try ExecProgram_Result(
serializedBytes: callNative(
name: "KclvmService.ExecProgram", args: try args.serializedBytes()))
}

// Overrides specified elements in a KCL file according to given arguments.
public func overrideFile(_ args: OverrideFile_Args) throws -> OverrideFile_Result {
return try OverrideFile_Result(
serializedBytes: callNative(
name: "KclvmService.OverrideFile", args: try args.serializedBytes()))
}

// Lists all variables declared in a KCL file.
public func listVariables(_ args: ListVariables_Args) throws -> ListVariables_Result {
return try ListVariables_Result(
serializedBytes: callNative(
name: "KclvmService.ListVariables", args: try args.serializedBytes()))
}

// Lists all options defined in a KCL program.
public func listOptions(_ args: ParseProgram_Args) throws -> ListOptions_Result {
return try ListOptions_Result(
serializedBytes: callNative(
name: "KclvmService.ListOptions", args: try args.serializedBytes()))
}

// Retrieves the full schema type mapping for a KCL program.
public func getSchemaTypeMapping(_ args: GetSchemaTypeMapping_Args) throws
-> GetSchemaTypeMapping_Result
{
return try GetSchemaTypeMapping_Result(
serializedBytes: callNative(
name: "KclvmService.GetSchemaTypeMapping", args: try args.serializedBytes()))
}

// Formats source code according to KCL style guidelines.
public func formatCode(_ args: FormatCode_Args) throws -> FormatCode_Result {
return try FormatCode_Result(
serializedBytes: callNative(name: "KclvmService.FormatCode", args: try args.serializedBytes())
)
}

// Formats KCL files or directories to conform to style guidelines.
public func formatPath(_ args: FormatPath_Args) throws -> FormatPath_Result {
return try FormatPath_Result(
serializedBytes: callNative(name: "KclvmService.FormatPath", args: try args.serializedBytes())
)
}

// Runs linting checks on KCL files and reports errors and warnings.
public func lintPath(_ args: LintPath_Args) throws -> LintPath_Result {
return try LintPath_Result(
serializedBytes: callNative(name: "KclvmService.LintPath", args: try args.serializedBytes()))
}

// Validates a data string against a schema defined in a KCL code string.
public func validateCode(_ args: ValidateCode_Args) throws -> ValidateCode_Result {
return try ValidateCode_Result(
serializedBytes: callNative(
name: "KclvmService.ValidateCode", args: try args.serializedBytes()))
}

// Builds configuration from settings files.
public func loadSettingsFiles(_ args: LoadSettingsFiles_Args) throws -> LoadSettingsFiles_Result {
return try LoadSettingsFiles_Result(
serializedBytes: callNative(
name: "KclvmService.LoadSettingsFiles", args: try args.serializedBytes()))
}

// Renames symbols across files within a KCL package.
public func rename(_ args: Rename_Args) throws -> Rename_Result {
return try Rename_Result(
serializedBytes: callNative(name: "KclvmService.Rename", args: try args.serializedBytes()))
}

// Renames symbols in source code without modifying files directly.
public func renameCode(_ args: RenameCode_Args) throws -> RenameCode_Result {
return try RenameCode_Result(
serializedBytes: callNative(name: "KclvmService.RenameCode", args: try args.serializedBytes())
)
}

// Executes tests on KCL packages using specified test arguments.
public func test(_ args: Test_Args) throws -> Test_Result {
return try Test_Result(
serializedBytes: callNative(name: "KclvmService.Test", args: try args.serializedBytes()))
}

// Updates dependencies for a KCL project based on defined specifications.
public func updateDependencies(_ args: UpdateDependencies_Args) throws
-> UpdateDependencies_Result
{
return try UpdateDependencies_Result(
serializedBytes: callNative(
name: "KclvmService.UpdateDependencies", args: try args.serializedBytes()))
}

// Retrieves version information about the KCL service.
public func getVersion(_ args: GetVersion_Args) throws -> GetVersion_Result {
return try GetVersion_Result(
serializedBytes: callNative(name: "KclvmService.GetVersion", args: try args.serializedBytes())
)
}

private func callNative(name: String, args: Data) -> Data {
// Convert name to byte array
let nameBytes = [UInt8](name.utf8)
var resultBuf = [UInt8](repeating: 0, count: 2048 * 2048)
let resultLength = CKclLib.callNative(
nameBytes, UInt(nameBytes.count), [UInt8](args), UInt(args.count), &resultBuf)
let result = Data(bytes: resultBuf, count: Int(resultLength))
let resultString = String(bytes: result, encoding: .utf8) ?? ""
if resultString.isEmpty || !resultString.hasPrefix(API.ERROR_PREFIX) {
return result
}
fatalError(String(resultString.dropFirst(API.ERROR_PREFIX.count)))
}
}
72 changes: 36 additions & 36 deletions swift/Sources/KclLib/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,57 @@ import Foundation

// Define the protocol for services that manage various operations on KCL programs.
public protocol Service {
// Parses a single KCL file and returns its Abstract Syntax Tree (AST) as a JSON string.
func parseFile(_ args: ParseFile_Args) throws -> ParseFile_Result
// Parses a single KCL file and returns its Abstract Syntax Tree (AST) as a JSON string.
func parseFile(_ args: ParseFile_Args) throws -> ParseFile_Result

// Parses a KCL program and returns the Abstract Syntax Tree (AST) in JSON format.
func parseProgram(_ args: ParseProgram_Args) throws -> ParseProgram_Result
// Parses a KCL program and returns the Abstract Syntax Tree (AST) in JSON format.
func parseProgram(_ args: ParseProgram_Args) throws -> ParseProgram_Result

// Loads a KCL package and retrieves AST, symbol, type, and definition information.
func loadPackage(_ args: LoadPackage_Args) throws -> LoadPackage_Result
// Loads a KCL package and retrieves AST, symbol, type, and definition information.
func loadPackage(_ args: LoadPackage_Args) throws -> LoadPackage_Result

// Executes a KCL file with provided arguments.
func execProgram(_ args: ExecProgram_Args) throws -> ExecProgram_Result
// Executes a KCL file with provided arguments.
func execProgram(_ args: ExecProgram_Args) throws -> ExecProgram_Result

// Overrides specified elements in a KCL file according to given arguments.
func overrideFile(_ args: OverrideFile_Args) throws -> OverrideFile_Result
// Overrides specified elements in a KCL file according to given arguments.
func overrideFile(_ args: OverrideFile_Args) throws -> OverrideFile_Result

// Lists all variables declared in a KCL file.
func listVariables(_ args: ListVariables_Args) throws -> ListVariables_Result
// Lists all variables declared in a KCL file.
func listVariables(_ args: ListVariables_Args) throws -> ListVariables_Result

// Lists all options defined in a KCL program.
func listOptions(_ args: ParseProgram_Args) throws -> ListOptions_Result
// Lists all options defined in a KCL program.
func listOptions(_ args: ParseProgram_Args) throws -> ListOptions_Result

// Retrieves the full schema type mapping for a KCL program.
func getSchemaTypeMapping(_ args: GetSchemaTypeMapping_Args) throws -> GetSchemaTypeMapping_Result
// Retrieves the full schema type mapping for a KCL program.
func getSchemaTypeMapping(_ args: GetSchemaTypeMapping_Args) throws -> GetSchemaTypeMapping_Result

// Formats source code according to KCL style guidelines.
func formatCode(_ args: FormatCode_Args) throws -> FormatCode_Result
// Formats source code according to KCL style guidelines.
func formatCode(_ args: FormatCode_Args) throws -> FormatCode_Result

// Formats KCL files or directories to conform to style guidelines.
func formatPath(_ args: FormatPath_Args) throws -> FormatPath_Result
// Formats KCL files or directories to conform to style guidelines.
func formatPath(_ args: FormatPath_Args) throws -> FormatPath_Result

// Runs linting checks on KCL files and reports errors and warnings.
func lintPath(_ args: LintPath_Args) throws -> LintPath_Result
// Runs linting checks on KCL files and reports errors and warnings.
func lintPath(_ args: LintPath_Args) throws -> LintPath_Result

// Validates a data string against a schema defined in a KCL code string.
func validateCode(_ args: ValidateCode_Args) throws -> ValidateCode_Result
// Validates a data string against a schema defined in a KCL code string.
func validateCode(_ args: ValidateCode_Args) throws -> ValidateCode_Result

// Builds configuration from settings files.
func loadSettingsFiles(_ args: LoadSettingsFiles_Args) throws -> LoadSettingsFiles_Result
// Builds configuration from settings files.
func loadSettingsFiles(_ args: LoadSettingsFiles_Args) throws -> LoadSettingsFiles_Result

// Renames symbols across files within a KCL package.
func rename(_ args: Rename_Args) throws -> Rename_Result
// Renames symbols across files within a KCL package.
func rename(_ args: Rename_Args) throws -> Rename_Result

// Renames symbols in source code without modifying files directly.
func renameCode(_ args: RenameCode_Args) throws -> RenameCode_Result
// Renames symbols in source code without modifying files directly.
func renameCode(_ args: RenameCode_Args) throws -> RenameCode_Result

// Executes tests on KCL packages using specified test arguments.
func test(_ args: Test_Args) throws -> Test_Result
// Executes tests on KCL packages using specified test arguments.
func test(_ args: Test_Args) throws -> Test_Result

// Updates dependencies for a KCL project based on defined specifications.
func updateDependencies(_ args: UpdateDependencies_Args) throws -> UpdateDependencies_Result
// Updates dependencies for a KCL project based on defined specifications.
func updateDependencies(_ args: UpdateDependencies_Args) throws -> UpdateDependencies_Result

// Retrieves version information about the KCL service.
func getVersion(_ args: GetVersion_Args) throws -> GetVersion_Result
// Retrieves version information about the KCL service.
func getVersion(_ args: GetVersion_Args) throws -> GetVersion_Result
}
6 changes: 2 additions & 4 deletions wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
},
"license": "Apache-2.0",
"dependencies": {
"wasi-js": "^1.7.3"
"wasi-js": "^1.7.3",
"prettier": "^2.8.4"
},
"bundledDependencies": [
"wasi-js"
],
"devDependencies": {
"@types/node": "^20.11.0",
"prettier": "^2.8.4",
Expand Down

0 comments on commit f538c97

Please sign in to comment.