Skip to content

Commit

Permalink
feat: add URL support
Browse files Browse the repository at this point in the history
  • Loading branch information
elihwyma committed Oct 30, 2024
1 parent c9cd198 commit 6fc01b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
23 changes: 23 additions & 0 deletions Sources/PugiSwift/Decoding/Conformances/URL+XMLDecodable.swift
Original file line number Diff line number Diff line change
@@ -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
}

}
4 changes: 4 additions & 0 deletions Sources/PugiSwift/Decoding/XMLDecoderError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public enum XMLDecoderError: Error, LocalizedError, CustomStringConvertible {

case couldNotGetName

case failedToParseURL(string: String)

public var description: String {
localizedDescription
}
Expand All @@ -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)"
}
}

Expand Down
22 changes: 0 additions & 22 deletions Tests/PugiSwiftTests/SimpleDecoderTest.swift
Original file line number Diff line number Diff line change
@@ -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 =
"""
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<records value="Hello World">
<record>
<name>Paul Koch</name>
<list>17</list>
</record>
</records>
"""
}

0 comments on commit 6fc01b8

Please sign in to comment.