-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializablePackageOptions.swift
More file actions
98 lines (95 loc) · 4.5 KB
/
InitializablePackageOptions.swift
File metadata and controls
98 lines (95 loc) · 4.5 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// InitializablePackageOptions.swift
// RadiantKit
//
// Created by Leo Dion.
// Copyright © 2025 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
public import Foundation
/// Options for controlling how package files are written and protected.
public struct InitializablePackageOptions: OptionSet, Sendable {
/// An option to write data to an auxiliary file first and
/// then replace the original file
/// with the auxiliary file when the write completes.
public static let atomic = InitializablePackageOptions(rawValue: 1 << 0)
/// An option that attempts to write data to a file and fails with an error if
/// the
/// destination file already exists.
public static let withoutOverwriting = InitializablePackageOptions(rawValue: 1 << 1)
/// An option to not encrypt the file when writing it out.
public static let noFileProtection = InitializablePackageOptions(rawValue: 1 << 2)
/// An option to make the file accessible only while the device is unlocked.
public static let completeFileProtection = InitializablePackageOptions(rawValue: 1 << 3)
/// An option to allow the file to be accessible while the device is unlocked
/// or the file is already open.
public static let completeFileProtectionUnlessOpen =
InitializablePackageOptions(rawValue: 1 << 4)
// swiftlint:disable identifier_name
/// An option to allow the file to be accessible after a user first unlocks the
/// device.
public static let completeFileProtectionUntilFirstUserAuthentication =
InitializablePackageOptions(rawValue: 1 << 5)
// swiftlint:enable identifier_name
/// An option the system uses when determining the file protection options that
/// the system assigns to the data.
public static let fileProtectionMask = InitializablePackageOptions(rawValue: 0x0F << 2)
/// An option to create any necessary intermediate directories in the write
/// path.
public static let withIntermediateDirectories =
InitializablePackageOptions(rawValue: 1 << 6)
// Common combinations
/// No options enabled.
public static let none: InitializablePackageOptions = []
/// The raw integer value representing the option set.
public let rawValue: Int
/// Returns true if the withIntermediateDirectories option is enabled
public var withIntermediateDirectoriesIsEnabled: Bool {
contains(.withIntermediateDirectories)
}
/// Creates an option set with the specified raw value.
///
/// - Parameter rawValue: The raw integer value for the option set.
public init(rawValue: Int) { self.rawValue = rawValue }
}
extension Data.WritingOptions {
/// Initialize Data.WritingOptions from InitializablePackageOptions
/// - Parameter options: The `InitializablePackageOptions`
/// to convert to `Data.WritingOptions`
public init(options: InitializablePackageOptions) {
var dataOptions: Data.WritingOptions = []
if options.contains(.atomic) { dataOptions.insert(.atomic) }
if options.contains(.withoutOverwriting) { dataOptions.insert(.withoutOverwriting) }
if options.contains(.noFileProtection) { dataOptions.insert(.noFileProtection) }
if options.contains(.completeFileProtection) {
dataOptions.insert(.completeFileProtection)
}
if options.contains(.completeFileProtectionUnlessOpen) {
dataOptions.insert(.completeFileProtectionUnlessOpen)
}
if options.contains(.completeFileProtectionUntilFirstUserAuthentication) {
dataOptions.insert(.completeFileProtectionUntilFirstUserAuthentication)
}
self = dataOptions
}
}