Skip to content

Commit 644f18f

Browse files
authored
Merge pull request #27 from bannzai/bugfix/exception/unknown_defaults
Handling unknown defaults for NSLayoutConstraint.Attributes and Relation
2 parents 7288e42 + ea3f94c commit 644f18f

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

Sources/Gedatsu/FormatterFunctions.swift

+16-3
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,32 @@ public func buildTreeContent(context: Context) -> String {
1313
return content
1414
}
1515
public func buildHeader(context: Context) -> String {
16-
return context.exclusiveConstraints.compactMap { constraint in
16+
var shouldShowErrorMessage = false
17+
let validate: ([HasDisplayName]) -> Bool = { !$0.allSatisfy(\.isValid) }
18+
let content = context.exclusiveConstraints.compactMap { constraint in
1719
let address = addres(of: constraint)
1820
switch (constraint.firstItem, constraint.secondItem) {
1921
case (.none, .none):
2022
return "NSLayoutConstraint: \(address) Unknown case. \(constraint.displayIdentifier)"
2123
case (.some(let lhs), .some(let rhs)):
22-
let (lAttribute, rAttribute) = (constraint.firstAttribute.displayName, constraint.secondAttribute.displayName)
24+
let (lAttribute, rAttribute) = (constraint.firstAttribute, constraint.secondAttribute)
2325
let relation = constraint.relation
24-
return "NSLayoutConstraint: \(address) \(itemType(of: lhs)).\(lAttribute) \(relation.displayName) \(itemType(of: rhs)).\(rAttribute) \(constraint.displayIdentifier)"
26+
shouldShowErrorMessage = shouldShowErrorMessage || validate([lAttribute, rAttribute, relation])
27+
return "NSLayoutConstraint: \(address) \(itemType(of: lhs)).\(lAttribute.displayName) \(relation.displayName) \(itemType(of: rhs)).\(rAttribute.displayName) \(constraint.displayIdentifier)"
2528
case (.some(let item), .none):
29+
shouldShowErrorMessage = shouldShowErrorMessage || validate([constraint.firstAttribute, constraint.relation])
2630
return "NSLayoutConstraint: \(address) \(itemType(of: item)).\(constraint.firstAttribute.displayName) \(constraint.relation.displayName) \(constraint.constant) \(constraint.displayIdentifier)"
2731
case (.none, .some(let item)):
32+
shouldShowErrorMessage = shouldShowErrorMessage || validate([constraint.secondAttribute, constraint.relation])
2833
return "NSLayoutConstraint: \(address) \(itemType(of: item)).\(constraint.secondAttribute.displayName) \(constraint.relation.displayName) \(constraint.constant) \(constraint.displayIdentifier)"
2934
}
3035
}.joined(separator: "\n")
36+
if shouldShowErrorMessage {
37+
return """
38+
Gedatsu catch unknown attribute or relation pattern. See issues for a solution to this problem. If that doesn't solve, please create a new issue."
39+
https://github.com/bannzai/gedatsu/issues
40+
\(content)
41+
"""
42+
}
43+
return content
3144
}

Sources/Gedatsu/NSLayoutConstraintExtension.swift

+33-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,24 @@ internal extension NSLayoutConstraint {
1313
}
1414
}
1515

16-
internal extension NSLayoutConstraint.Attribute {
16+
internal protocol HasDisplayName {
17+
var isValid: Bool { get }
18+
var displayName: String { get }
19+
}
20+
21+
extension NSLayoutConstraint.Attribute: HasDisplayName {
22+
var isValid: Bool {
23+
switch self {
24+
case .left, .right, .top, .bottom, .leading, .trailing, .width, .height, .centerX, .centerY, .lastBaseline, .firstBaseline, .notAnAttribute:
25+
return true
26+
#if os(iOS)
27+
case .leftMargin, .rightMargin, .topMargin, .bottomMargin, .leadingMargin, .trailingMargin, .centerXWithinMargins, .centerYWithinMargins:
28+
return true
29+
#endif
30+
@unknown default:
31+
return false
32+
}
33+
}
1734
var displayName: String {
1835
switch self {
1936
case .left:
@@ -61,11 +78,23 @@ internal extension NSLayoutConstraint.Attribute {
6178
case .notAnAttribute:
6279
return "notAnAttribute"
6380
@unknown default:
64-
fatalError()
81+
return "⚠️️️️⚠️️️️⚠️️️️ Unknown Attribute case for rawValue == \(rawValue) ⚠️️️️⚠️️️️⚠️️️️ "
6582
}
6683
}
6784
}
68-
internal extension NSLayoutConstraint.Relation {
85+
extension NSLayoutConstraint.Relation: HasDisplayName {
86+
var isValid: Bool {
87+
switch self {
88+
case .equal:
89+
return true
90+
case .greaterThanOrEqual:
91+
return true
92+
case .lessThanOrEqual:
93+
return true
94+
@unknown default:
95+
return false
96+
}
97+
}
6998
var displayName: String {
7099
switch self {
71100
case .equal:
@@ -75,7 +104,7 @@ internal extension NSLayoutConstraint.Relation {
75104
case .lessThanOrEqual:
76105
return "<="
77106
@unknown default:
78-
fatalError()
107+
return "⚠️️️️⚠️️️️⚠️️️️ Unknown Relation case for rawValue == \(rawValue) ⚠️️️️⚠️️️️⚠️️️️ "
79108
}
80109
}
81110
}

0 commit comments

Comments
 (0)