Skip to content

Commit

Permalink
Improve SVD2SwiftPlugin errors
Browse files Browse the repository at this point in the history
Updates the SVD2SwiftPlugin to format JSON decoding errors into human
readable strings.
  • Loading branch information
rauhul committed Aug 20, 2024
1 parent ffa2b75 commit b368493
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
11 changes: 9 additions & 2 deletions Plugins/SVD2SwiftPlugin/SVD2SwiftPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ struct SVD2SwiftPlugin: BuildToolPlugin {
// Load the list of peripherals to generate from the config file.
let pluginConfigURL = URL(fileURLWithPath: pluginConfigFile.string)
let pluginConfigData = try Data(contentsOf: pluginConfigURL)
let pluginConfig = try JSONDecoder()
.decode(SVD2SwiftPluginConfiguration.self, from: pluginConfigData)
let pluginConfig: SVD2SwiftPluginConfiguration
do {
pluginConfig = try JSONDecoder()
.decode(SVD2SwiftPluginConfiguration.self, from: pluginConfigData)
} catch let error as DecodingError {
throw SVD2SwiftPluginConfigurationDecodingError(
url: pluginConfigURL,
error: error)
}
guard !pluginConfig.peripherals.isEmpty else {
throw SVD2SwiftPluginError.missingPeripherals(target, pluginConfigFile)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift MMIO open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import Foundation

struct SVD2SwiftPluginConfigurationDecodingError {
var description: String
}

extension SVD2SwiftPluginConfigurationDecodingError: CustomStringConvertible {
init(url: URL, error: DecodingError) {
func format(_ codingPath: [any CodingKey]) -> String{
if codingPath.isEmpty {
return "<root>"
} else {
var path = ""
for key in codingPath {
if !path.isEmpty {
path.append(".")
}
if let index = key.intValue {
path.append("[")
path.append("\(index)")
path.append("]")
} else {
path.append(key.stringValue)
}
}
return path
}
}

let errorFragment = switch error {
case let .typeMismatch(type, context):
"Expected type \"\(type)\" at path \"\(format(context.codingPath))\"."
case let .valueNotFound(_, context):
"Expected value at path \"\(format(context.codingPath))\"."
case let .keyNotFound(key, context):
"Expected key at path \"\(format(context.codingPath + [key]))\"."
case let .dataCorrupted(context):
"Data corrupted at path \"\(format(context.codingPath))\"."
default:
"Unknown decoding error: \(error)"
}
self.description = """
\(url.path):1:1: \
Failed to read \(FileKind.svd2swift): \
\(errorFragment)
"""
}
}

extension SVD2SwiftPluginConfigurationDecodingError: Error {}
2 changes: 1 addition & 1 deletion Tests/SVD2SwiftPluginTests/svd2swift.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"TIMER0",
"TIMER1",
],
"indentation-width": 2,
"indentation-width": 1,
"namespace-under-device": true,
"instance-member-peripherals": false,
"device-name": "Banana"
Expand Down

0 comments on commit b368493

Please sign in to comment.