// // GlobalEnums.swift // HRW // // Created by Isaac Paul on 10/19/24. // Non-commercial license, see LICENSE.MD in the project root for details // import Foundation //Some bike shedding to avoid allocations struct ComponentsIterator: IteratorProtocol { let string: String let splitCharacter: Character var currentIndex: String.Index init(_ string: String, separatedBy: Character) { self.string = string self.splitCharacter = separatedBy self.currentIndex = string.startIndex } mutating func next() -> Substring? { // Skip any leading split characters while currentIndex < string.endIndex && string[currentIndex] == splitCharacter { currentIndex = string.index(after: currentIndex) } // Check if we've reached the end of the string if currentIndex >= string.endIndex { return nil } let start = currentIndex while currentIndex < string.endIndex && string[currentIndex] != splitCharacter { currentIndex = string.index(after: currentIndex) } return string[start..(_ block: (_ input:Element) throws -> R) rethrows -> [R] { var result:[R] = [] while let item = self.next() { result.append(try block(item)) } return result } } extension String { func componentsIterator(separatedBy: Character) -> ComponentsIterator { return ComponentsIterator(self, separatedBy: separatedBy) } func componentsIterator(separatedBy: String) -> ComponentsIterator { return ComponentsIterator(self, separatedBy: separatedBy.first!) } } public enum ShadowRootMode : Substring { case open = "open" ///The template element represents an open declarative shadow root. case close = "close" ///The template element represents a closed declarative shadow root. public init?(rawValue: String) { self.init(rawValue: rawValue.asSubstring()) } } public enum Blocking : Substring { case render = "render" static func parseList(_ value:String?) throws -> [Blocking] { guard let value = value else { return [] } var iterator = value.componentsIterator(separatedBy: " ") let result = try iterator.map { input in return try expect(Blocking(rawValue: input), "unexpected value for blocking: \(input)") } return result } } public enum FetchPriority : Substring { case high = "high" case low = "low" case auto = "auto" public init?(rawValue: String) { self.init(rawValue: rawValue.asSubstring()) } public init(expect: String) throws { guard let _ = FetchPriority(rawValue: expect) else { throw AppError("Unexpected value for Fetch Priority: \(expect)") } self.init(rawValue: expect.asSubstring())! } } public enum Preload : Substring { case none = "none" case auto = "auto" case metadata = "metadata" public init?(rawValue: String) { self.init(rawValue: rawValue.asSubstring()) } public init(expect: String) throws { let strToUse:String if (expect.count == 0) { strToUse = "auto" } else { strToUse = expect } guard let _ = Preload(rawValue: strToUse) else { throw AppError("Unexpected value for Preload \(strToUse)") } self.init(rawValue: strToUse.asSubstring())! } }