Row.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Row.swift
  2. // This file is part of KeePassKit.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePassKit is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // KeePassKit is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with KeePassKit. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. import Binary
  20. public typealias Field<Type> = TLV<Type, UInt32>
  21. public protocol Row: class {
  22. associatedtype `Type`: Streamable, Equatable
  23. static var End: Type { get }
  24. var fields: [Field<Type>] { get set }
  25. init()
  26. }
  27. extension Row {
  28. public subscript(_ type: Type) -> Bytes? {
  29. get { fields.first(where: { $0.type == type })?.value }
  30. set {
  31. fields.removeAll(where: { $0.type == type })
  32. guard let value = newValue else { return }
  33. let tlv = Field(type: type, value: value)
  34. fields.insert(tlv, at: 0)
  35. }
  36. }
  37. public func set(_ field: Field<Type>) {
  38. fields.removeAll(where: { $0.type == field.type })
  39. fields.insert(field, at: 0)
  40. }
  41. public subscript<T>(_ type: Type) -> T? where T: BytesRepresentable {
  42. get {
  43. guard let bytes = self[type] else { return nil }
  44. return try? T(bytes)
  45. }
  46. set { self[type] = newValue?.bytes }
  47. }
  48. public func remove(_ type: Type) {
  49. fields.removeAll(where: { $0.type == type })
  50. }
  51. }
  52. extension Readable where Self: Row {
  53. public init(from input: Input) throws {
  54. self.init()
  55. while true {
  56. let field = try input.read() as Field<Type>
  57. guard field.type != Self.End else { break }
  58. fields.append(field)
  59. }
  60. }
  61. }
  62. extension Writable where Self: Row {
  63. public func write(to output: Output) throws {
  64. try output.write(fields)
  65. let end = Field(type: Self.End, value: [])
  66. try output.write(end)
  67. }
  68. }
  69. extension Sequence where Element: Row {
  70. public func first<T>(where type: Element.`Type`, _ predicate: (T) throws -> Bool) throws -> Element? where T: BytesRepresentable {
  71. return try first(where: {
  72. guard let bytes = $0[type] else { return false }
  73. return try predicate(try T(bytes))
  74. })
  75. }
  76. public func sorted<T>(field: Element.`Type`, by areInIncreasingOrder: (T, T) throws -> Bool) throws -> [Self.Element] where T: BytesRepresentable {
  77. return try sorted(by: {
  78. guard let rhs = $0[field], let lhs = $1[field] else { return false }
  79. return try areInIncreasingOrder(try T(rhs), try T(lhs))
  80. })
  81. }
  82. }