From 6fc01b8c038cf43882a6388452230de73333a8ac Mon Sep 17 00:00:00 2001 From: Amy While <26681721+elihwyma@users.noreply.github.com> Date: Wed, 30 Oct 2024 19:13:48 +0100 Subject: [PATCH] feat: add URL support --- .../Conformances/URL+XMLDecodable.swift | 23 +++++++++++++++++++ .../PugiSwift/Decoding/XMLDecoderError.swift | 4 ++++ Tests/PugiSwiftTests/SimpleDecoderTest.swift | 22 ------------------ 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 Sources/PugiSwift/Decoding/Conformances/URL+XMLDecodable.swift diff --git a/Sources/PugiSwift/Decoding/Conformances/URL+XMLDecodable.swift b/Sources/PugiSwift/Decoding/Conformances/URL+XMLDecodable.swift new file mode 100644 index 0000000..f552979 --- /dev/null +++ b/Sources/PugiSwift/Decoding/Conformances/URL+XMLDecodable.swift @@ -0,0 +1,23 @@ +// +// URL+XMLDecodable.swift +// PugiSwift +// +// Created by Amy on 30/10/2024. +// + +import Foundation + +extension URL: XMLDecodable { + + public init(from attribute: (any AttributeProtocol)?) throws(XMLDecoderError) { + guard let attribute else { + throw .noValueFound + } + let str = attribute.as_string(def: "") + guard let url = URL(string: str) else { + throw .failedToParseURL(string: str) + } + self = url + } + +} diff --git a/Sources/PugiSwift/Decoding/XMLDecoderError.swift b/Sources/PugiSwift/Decoding/XMLDecoderError.swift index c9fb2e4..7f5cbb5 100644 --- a/Sources/PugiSwift/Decoding/XMLDecoderError.swift +++ b/Sources/PugiSwift/Decoding/XMLDecoderError.swift @@ -23,6 +23,8 @@ public enum XMLDecoderError: Error, LocalizedError, CustomStringConvertible { case couldNotGetName + case failedToParseURL(string: String) + public var description: String { localizedDescription } @@ -43,6 +45,8 @@ public enum XMLDecoderError: Error, LocalizedError, CustomStringConvertible { "Not Implemented" case .couldNotGetName: "Could not get name" + case .failedToParseURL(string: let string): + "Failed to parse into URL: \(string)" } } diff --git a/Tests/PugiSwiftTests/SimpleDecoderTest.swift b/Tests/PugiSwiftTests/SimpleDecoderTest.swift index 765b15a..b1d99c8 100644 --- a/Tests/PugiSwiftTests/SimpleDecoderTest.swift +++ b/Tests/PugiSwiftTests/SimpleDecoderTest.swift @@ -1,25 +1,3 @@ import Testing import Foundation @testable import PugiSwift - -@Node struct Records { - -} - -@Node struct Record { - let name: String - let list: Int -} - -@Test func debugging() throws { - let str = - """ - - - - Paul Koch - 17 - - - """ -}