DispatchEvent.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // GCDFSEvent.swift
  3. // FileKit
  4. //
  5. // Created by ijump on 5/2/16.
  6. // Copyright © 2017 Nikolai Vazquez. All rights reserved.
  7. //
  8. import Foundation
  9. /// File System Events.
  10. public struct DispatchFileSystemEvents: OptionSet, CustomStringConvertible, CustomDebugStringConvertible {
  11. // MARK: - Events
  12. /// The file-system object was deleted from the namespace.
  13. public static let Delete = DispatchFileSystemEvents(rawValue: DispatchSource.FileSystemEvent.delete.rawValue)
  14. /// The file-system object data changed.
  15. public static let Write = DispatchFileSystemEvents(rawValue: DispatchSource.FileSystemEvent.write.rawValue)
  16. /// The file-system object changed in size.
  17. public static let Extend = DispatchFileSystemEvents(rawValue: DispatchSource.FileSystemEvent.extend.rawValue)
  18. /// The file-system object metadata changed.
  19. public static let Attribute = DispatchFileSystemEvents(rawValue: DispatchSource.FileSystemEvent.attrib.rawValue)
  20. /// The file-system object link count changed.
  21. public static let Link = DispatchFileSystemEvents(rawValue: DispatchSource.FileSystemEvent.link.rawValue)
  22. /// The file-system object was renamed in the namespace.
  23. public static let Rename = DispatchFileSystemEvents(rawValue: DispatchSource.FileSystemEvent.rename.rawValue)
  24. /// The file-system object was revoked.
  25. public static let Revoke = DispatchFileSystemEvents(rawValue: DispatchSource.FileSystemEvent.revoke.rawValue)
  26. /// The file-system object was created.
  27. public static let Create = DispatchFileSystemEvents(rawValue: 0x1000)
  28. /// All of the event IDs.
  29. public static let All: DispatchFileSystemEvents = [.Delete, .Write, .Extend, .Attribute, .Link, .Rename, .Revoke, .Create]
  30. // MARK: - All Events
  31. /// An array of all of the events.
  32. public static let allEvents: [DispatchFileSystemEvents] = [
  33. .Delete, .Write, .Extend, .Attribute, .Link, .Rename, .Revoke, .Create
  34. ]
  35. /// The names of all of the events.
  36. public static let allEventNames: [String] = [
  37. "Delete", "Write", "Extend", "Attribute", "Link", "Rename", "Revoke", "Create"
  38. ]
  39. // MARK: - Properties
  40. /// The raw event value.
  41. public let rawValue: UInt
  42. /// A textual representation of `self`.
  43. public var description: String {
  44. var result = ""
  45. for (index, element) in DispatchFileSystemEvents.allEvents.enumerated() {
  46. if self.contains(element) {
  47. let name = DispatchFileSystemEvents.allEventNames[index]
  48. result += result.isEmpty ? "\(name)": ", \(name)"
  49. }
  50. }
  51. return String(describing: type(of: self)) + "[\(result)]"
  52. }
  53. /// A textual representation of `self`, suitable for debugging.
  54. public var debugDescription: String {
  55. var result = ""
  56. for (index, element) in DispatchFileSystemEvents.allEvents.enumerated() {
  57. if self.contains(element) {
  58. let name = DispatchFileSystemEvents.allEventNames[index] + "(\(element.rawValue))"
  59. result += result.isEmpty ? "\(name)": ", \(name)"
  60. }
  61. }
  62. return String(describing: type(of: self)) + "[\(result)]"
  63. }
  64. // MARK: - Initialization
  65. /// Creates a set of events from a raw value.
  66. ///
  67. /// - Parameter rawValue: The raw value to initialize from.
  68. public init(rawValue: UInt) {
  69. self.rawValue = rawValue
  70. }
  71. }