FileKit.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // FileKit.swift
  3. // FileKit
  4. //
  5. // The MIT License (MIT)
  6. //
  7. // Copyright (c) 2015-2017 Nikolai Vazquez
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. //
  27. /// Information regarding [FileKit](https://github.com/nvzqz/FileKit).
  28. ///
  29. /// - Author: [Nikolai Vazquez](https://github.com/nvzqz)
  30. ///
  31. /// - Copyright: [MIT License](https://opensource.org/licenses/MIT)
  32. ///
  33. /// - Version: [v5.0.0](https://github.com/nvzqz/FileKit/releases/tag/v4.0.0)
  34. ///
  35. /// - Requires: Xcode 9, Swift 4.0
  36. ///
  37. public enum FileKitInfo {
  38. /// The current version.
  39. ///
  40. /// FileKit follows [Semantic Versioning v2.0.0](http://semver.org/).
  41. public static let version = "v5.0.0"
  42. /// The current release.
  43. public static let release = 12
  44. /// FileKit is licensed under the [MIT License](https://opensource.org/licenses/MIT).
  45. public static let license = "MIT"
  46. /// A brief description of FileKit.
  47. public static let description = "A Swift framework that allows for simple and expressive file management."
  48. /// Where the project can be found.
  49. public static let projectURL = "https://github.com/nvzqz/FileKit"
  50. }
  51. import Foundation
  52. public struct FileKit {
  53. /// Shared json decoder instance
  54. public static var jsonDecoder = JSONDecoder()
  55. /// Shared json encoder instance
  56. public static var jsonEncoder = JSONEncoder()
  57. /// Shared property list decoder instance
  58. public static var propertyListDecoder = PropertyListDecoder()
  59. /// Shared property list encoder instance
  60. public static var propertyListEncoder = PropertyListEncoder()
  61. }
  62. extension FileKit {
  63. /// Write an `Encodable` object to path
  64. ///
  65. /// - Parameter codable: The codable object to write.
  66. /// - Parameter path: The destination path for write operation.
  67. /// - Parameter encoder: A specific JSON encoder (default: FileKit.jsonEncoder).
  68. ///
  69. public static func write<T: Encodable>(_ codable: T, to path: Path, with encoder: JSONEncoder = FileKit.jsonEncoder) throws {
  70. do {
  71. let data = try encoder.encode(codable)
  72. try DataFile(path: path).write(data)
  73. } catch let error as FileKitError {
  74. throw error
  75. } catch {
  76. throw FileKitError.writeToFileFail(path: path, error: error)
  77. }
  78. }
  79. /// Read an `Encodable` object from path
  80. ///
  81. /// - Parameter path: The destination path for write operation.
  82. /// - Parameter decoder: A specific JSON decoder (default: FileKit.jsonDecoder).
  83. ///
  84. public static func read<T: Decodable>(from path: Path, with decoder: JSONDecoder = FileKit.jsonDecoder) throws -> T {
  85. let data = try DataFile(path: path).read()
  86. do {
  87. return try decoder.decode(T.self, from: data)
  88. } catch {
  89. throw FileKitError.readFromFileFail(path: path, error: error)
  90. }
  91. }
  92. }