-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates the SVD2SwiftPlugin to format JSON decoding errors into human readable strings.
- Loading branch information
Showing
3 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Plugins/SVD2SwiftPlugin/SVD2SwiftPluginConfigurationDecodingError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters