Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import Darwinpublic import Foundationpublic import ParcelCore
public enum MinimumSizeStoreError: Error, Equatable, Sendable { case invalidFileURL case unsupportedVersion(Int)}
public struct MinimumSizeStore: Sendable { public static let currentVersion = 1
public static var defaultFileURL: URL { FileManager.default.homeDirectoryForCurrentUser .appendingPathComponent("Library/Application Support/dev.vinnymac.parcel", isDirectory: true) .appendingPathComponent("minimum-sizes.json", isDirectory: false) }
public let fileURL: URL
public init(fileURL: URL = MinimumSizeStore.defaultFileURL) { self.fileURL = fileURL }
/// Missing storage starts empty. Malformed, unsupported, and unreadable storage throws. public func load() throws -> MinimumSizeLearning { guard fileURL.isFileURL else { throw MinimumSizeStoreError.invalidFileURL } let data: Data do { data = try Data(contentsOf: fileURL) } catch { if Self.isMissing(error) { return MinimumSizeLearning() } throw error } let decoder = JSONDecoder() let header = try decoder.decode(Header.self, from: data) guard header.version == Self.currentVersion else { throw MinimumSizeStoreError.unsupportedVersion(header.version) } let storage = try decoder.decode(Storage.self, from: data) return try MinimumSizeLearning(learned: storage.learned) }
/// Only confirmed observations are written; pending observations remain in memory. public func save(_ learning: MinimumSizeLearning) throws { guard fileURL.isFileURL else { throw MinimumSizeStoreError.invalidFileURL } let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys] let data = try encoder.encode(Storage(version: Self.currentVersion, learned: learning.learned)) try FileManager.default.createDirectory( at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true) try data.write(to: fileURL, options: .atomic) }
/// Deletes persisted data. Reset the in-memory learner separately. public func reset() throws { guard fileURL.isFileURL else { throw MinimumSizeStoreError.invalidFileURL } try fileURL.withUnsafeFileSystemRepresentation { path in guard let path else { throw MinimumSizeStoreError.invalidFileURL } guard Darwin.unlink(path) != 0 else { return } let code = errno guard code != ENOENT else { return } throw NSError(domain: NSPOSIXErrorDomain, code: Int(code), userInfo: [NSFilePathErrorKey: fileURL.path]) } }
private static func isMissing(_ error: any Error) -> Bool { let error = error as NSError return error.domain == NSCocoaErrorDomain && (error.code == CocoaError.fileReadNoSuchFile.rawValue || error.code == CocoaError.fileNoSuchFile.rawValue) }
private struct Header: Decodable { let version: Int }
private struct Storage: Codable { let version: Int let learned: [String: LearnedMinimumSize] }}