Skip to content

Commit

Permalink
Improve FileStore
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkenso committed Sep 20, 2024
1 parent d174704 commit 762beff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
25 changes: 15 additions & 10 deletions Sources/SpellbookFoundation/Filesystem & Bundle/FileStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ public struct FileStore<T> {
}

extension FileStore where T == Data {
public static var standard = FileStore(
read: { location, ifNotExists in
try Data(contentsOf: location, ifNoFile: ifNotExists)
},
write: { data, location, createDirectories in
if createDirectories {
try FileManager.default.createDirectoryTree(for: location)
@available(*, deprecated, renamed: "standard(writingOptions:)")
public static let standard: FileStore = .standard()

public static func standard(writingOptions: Data.WritingOptions = .atomic) -> FileStore {
.init(
read: { location, ifNotExists in
try Data(contentsOf: location, ifNoFile: ifNotExists)
},
write: { data, location, createDirectories in
if createDirectories {
try FileManager.default.createDirectoryTree(for: location)
}
try data.write(to: location, options: writingOptions)
}
try data.write(to: location)
}
)
)
}
}

extension FileStore {
Expand Down
8 changes: 4 additions & 4 deletions Tests/SpellbookTests/Filesystem & Bundle/FileStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FileStoreTests: XCTestCase {

func test_standard() throws {
let url = tempDir.location.appendingPathComponent("test.file")
let store = FileStore.standard
let store = FileStore.standard()
XCTAssertThrowsError(try store.read(from: url))
XCTAssertEqual(try store.read(from: url, default: Data(pod: 100500)), Data(pod: 100500))

Expand All @@ -36,11 +36,11 @@ class FileStoreTests: XCTestCase {

func test_exact() throws {
let url = tempDir.location.appendingPathComponent("test.file")
let store = FileStore.standard.exact(url)
let store = FileStore.standard().exact(url)
XCTAssertThrowsError(try store.read())
XCTAssertEqual(try store.read(default: Data(pod: 20)), Data(pod: 20))

let storeWithDefault = FileStore.standard.exact(url, default: Data(pod: 10))
let storeWithDefault = FileStore.standard().exact(url, default: Data(pod: 10))
XCTAssertEqual(try storeWithDefault.read(), Data(pod: 10))
XCTAssertEqual(try storeWithDefault.read(default: Data(pod: 20)), Data(pod: 20))

Expand All @@ -49,7 +49,7 @@ class FileStoreTests: XCTestCase {

func test_codable() throws {
let url = tempDir.location.appendingPathComponent("test.file")
let store = FileStore.standard.codable(Int.self, using: .json()).exact(url)
let store = FileStore.standard().codable(Int.self, using: .json()).exact(url)
XCTAssertThrowsError(try store.read())
XCTAssertEqual(try store.read(default: 20), 20)
XCTAssertFalse(FileManager.default.fileExists(at: url))
Expand Down

0 comments on commit 762beff

Please sign in to comment.