forked from AdguardTeam/SafariConverterLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversionResult.swift
More file actions
79 lines (64 loc) · 3.13 KB
/
ConversionResult.swift
File metadata and controls
79 lines (64 loc) · 3.13 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
import Foundation
/// Represents the final conversion result.
public struct ConversionResult: CustomStringConvertible {
public static let EMPTY_RESULT_JSON: String =
// swiftlint:disable:next line_length
"[{\"trigger\": {\"url-filter\": \".*\",\"if-domain\": [\"domain.com\"]},\"action\":{\"type\": \"ignore-previous-rules\"}}]"
/// Helper function that creates an empty result.
///
/// In this case we return a JSON with a single rule that does not do anything.
/// The reason for that is that Safari requires at least one rule to be present.
static func createEmptyResult() -> ConversionResult {
return ConversionResult(
sourceRulesCount: 0,
sourceSafariCompatibleRulesCount: 0,
safariRulesCount: 0,
advancedRulesCount: 0,
discardedSafariRules: 0,
errorsCount: 0,
safariRulesJSON: self.EMPTY_RESULT_JSON,
advancedRulesText: nil
)
}
/// Total number of AdGuard rules before the conversion started.
public let sourceRulesCount: Int
/// The number of source AdGuard rules before attempting to convert them
/// to Safari content blocking syntax. This number does not include advanced
/// rules which are counted separately.
public let sourceSafariCompatibleRulesCount: Int
/// The number of Safari rules in `safariRulesJSON`.
public let safariRulesCount: Int
/// The number of advanced rules in `advancedRulesText`.
public let advancedRulesCount: Int
/// The number of Safari rules that were discarded due to the limits that are imposed by the OS or Safari.
///
/// There are two possible reasons for discarding rules:
/// - Maximum number of rules in a content blocker (depends on the OS version, can be 50k or 150k)
/// - Maximum size of the JSON file (the issue is specific to iOS versions, see FB13282146)
public let discardedSafariRules: Int
/// Count of conversion errors (i.e. count of rules that we could not convert).
public let errorsCount: Int
/// JSON with Safari content blocking rules.
public let safariRulesJSON: String
/// AdGuard rules that need to be interpreted by web extension (or app extension).
public let advancedRulesText: String?
/// String representation of the conversion result
public var description: String {
return """
## Conversion status
* Source rules count: \(self.sourceRulesCount)
* Source rules compatible with Safari: \(self.sourceSafariCompatibleRulesCount)
* Failed to convert: \(self.errorsCount)
* Discarded due to limits: \(self.discardedSafariRules)
## Result
* Safari JSON rules count: \(self.safariRulesCount)
* JSON size: \(self.safariRulesJSON.utf8.count)
* Advanced rules count: \(self.advancedRulesCount)
* Advanced rules size: \(self.advancedRulesText?.utf8.count ?? 0)
"""
}
}
/// Make it possible to serialize ConversionResult to JSON.
///
/// We'll use this functionality for the command-line tool.
extension ConversionResult: Encodable {}