forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.swift
More file actions
47 lines (41 loc) · 1.37 KB
/
FileUtils.swift
File metadata and controls
47 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Foundation
public struct FileUtils{
public typealias ReadabilityErrorMessageProvider = (ReadabilityStatus) -> String?
public enum ReadabilityStatus {
case readable
case notFound
case permissionDenied
public var isReadable: Bool {
switch self {
case .readable: true
case .notFound, .permissionDenied: false
}
}
public func errorMessage(using provider: ReadabilityErrorMessageProvider? = nil) -> String? {
if let provider = provider {
return provider(self)
}
// Default error messages
switch self {
case .readable:
return nil
case .notFound:
return "File may have been removed or is unavailable."
case .permissionDenied:
return "Permission Denied to access file."
}
}
}
public static func checkFileReadability(at path: String) -> ReadabilityStatus {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: path) {
if fileManager.isReadableFile(atPath: path) {
return .readable
} else {
return .permissionDenied
}
} else {
return .notFound
}
}
}