33// SPDX-License-Identifier: GPL-3.0-or-later
44
55import Foundation
6+ import SwiftyXMLParser
67
78public struct NKTag : Sendable , Equatable , Hashable {
89 public let id : String
@@ -14,4 +15,48 @@ public struct NKTag: Sendable, Equatable, Hashable {
1415 self . name = name
1516 self . color = color
1617 }
18+
19+ static func parse( xmlData: Data ) -> [ NKTag ] {
20+ let xml = XML . parse ( xmlData)
21+ let responses = xml [ " d:multistatus " , " d:response " ]
22+ var tags : [ NKTag ] = [ ]
23+
24+ for response in responses {
25+ let propstat = response [ " d:propstat " ] [ 0 ]
26+ guard let id = propstat [ " d:prop " , " oc:id " ] . text,
27+ let name = propstat [ " d:prop " , " oc:display-name " ] . text else {
28+ continue
29+ }
30+
31+ let color = normalizedColor ( propstat [ " d:prop " , " nc:color " ] . text)
32+
33+ tags. append ( NKTag ( id: id, name: name, color: color) )
34+ }
35+
36+ return tags
37+ }
38+
39+ static func parse( systemTagElements: XML . Accessor ) -> [ NKTag ] {
40+ var tags : [ NKTag ] = [ ]
41+
42+ for element in systemTagElements {
43+ guard let name = element. text? . trimmingCharacters ( in: . whitespacesAndNewlines) ,
44+ !name. isEmpty else {
45+ continue
46+ }
47+
48+ let id = element. attributes [ " oc:id " ] ?? " "
49+ let color = normalizedColor ( element. attributes [ " nc:color " ] )
50+ tags. append ( NKTag ( id: id, name: name, color: color) )
51+ }
52+
53+ return tags
54+ }
55+
56+ private static func normalizedColor( _ rawValue: String ? ) -> String ? {
57+ guard let rawValue, !rawValue. isEmpty else {
58+ return nil
59+ }
60+ return rawValue. hasPrefix ( " # " ) ? rawValue : " # \( rawValue) "
61+ }
1762}
0 commit comments