11import Foundation
22
3- final class AttributesStorage {
4- struct Config {
3+ enum AttributesStorage {
4+ struct AttributesConfig : Config {
55 let cacheUrl : URL
66 let directoryUrl : URL
77 let fileUrl : URL
@@ -17,58 +17,38 @@ final class AttributesStorage {
1717 }
1818 }
1919
20- private static let directoryName = Bundle ( for : AttributesStorage . self ) . bundleIdentifier ?? " BacktraceCache "
20+ private static let directoryName = Bundle . main . bundleIdentifier ?? " BacktraceCache "
2121
2222 static func store( _ attributes: Attributes , fileName: String ) throws {
23- let config = try Config ( fileName: fileName)
24-
25- if !FileManager. default. fileExists ( atPath: config. directoryUrl. path) {
26- try FileManager . default. createDirectory ( atPath: config. directoryUrl. path,
27- withIntermediateDirectories: false ,
28- attributes: nil )
29- }
30-
31- if #available( iOS 11 . 0 , tvOS 11 . 0 , macOS 10 . 13 , * ) {
32- try ( attributes as NSDictionary ) . write ( to: config. fileUrl)
33- } else {
34- guard ( attributes as NSDictionary ) . write ( to: config. fileUrl, atomically: true ) else {
35- throw FileError . fileNotWritten
36- }
37- }
23+ try store ( attributes, fileName: fileName, storage: ReportMetadataStorageImpl . self)
24+ }
25+
26+ static func store< T: ReportMetadataStorage > ( _ attributes: Attributes , fileName: String , storage: T . Type ) throws {
27+ let config = try AttributesConfig ( fileName: fileName)
28+ try T . storeToFile ( attributes, config: config)
3829 BacktraceLogger . debug ( " Stored attributes at path: \( config. fileUrl) " )
3930 }
4031
4132 static func retrieve( fileName: String ) throws -> Attributes {
42- let config = try Config ( fileName: fileName)
43- guard FileManager . default. fileExists ( atPath: config. fileUrl. path) else {
44- throw FileError . fileNotExists
45- }
46- // load file to NSDictionary
47- let dictionary : NSDictionary
48- if #available( iOS 11 . 0 , tvOS 11 . 0 , macOS 10 . 13 , * ) {
49- dictionary = try NSDictionary ( contentsOf: config. fileUrl, error: ( ) )
50- } else {
51- guard let dictionaryFromFile = NSDictionary ( contentsOf: config. fileUrl) else {
52- throw FileError . invalidPropertyList
53- }
54- dictionary = dictionaryFromFile
55- }
56- // cast safety to AttributesType
57- guard let attributes: Attributes = dictionary as? Attributes else {
58- throw FileError . invalidPropertyList
59- }
33+ try retrieve ( fileName: fileName, storage: ReportMetadataStorageImpl . self)
34+ }
35+
36+ static func retrieve< T: ReportMetadataStorage > ( fileName: String , storage: T . Type ) throws -> Attributes {
37+ let config = try AttributesConfig ( fileName: fileName)
38+ let dictionary = try T . retrieveFromFile ( config: config)
39+ // cast safely to AttributesType
40+ let attributes : Attributes = dictionary as Attributes
6041 BacktraceLogger . debug ( " Retrieved attributes from path: \( config. fileUrl) " )
6142 return attributes
6243 }
6344
6445 static func remove( fileName: String ) throws {
65- let config = try Config ( fileName: fileName)
66- // check file exists
67- guard FileManager . default. fileExists ( atPath: config. fileUrl. path) else {
68- throw FileError . fileNotExists
69- }
70- // remove file
71- try FileManager . default. removeItem ( at: config. fileUrl)
46+ try remove ( fileName: fileName, storage: ReportMetadataStorageImpl . self)
47+ }
48+
49+ static func remove< T: ReportMetadataStorage > ( fileName: String , storage: T . Type ) throws {
50+ let config = try AttributesConfig ( fileName: fileName)
51+ try T . removeFile ( config: config)
7252 BacktraceLogger . debug ( " Removed attributes at path: \( config. fileUrl) " )
7353 }
7454}
0 commit comments