Skip to content

Commit

Permalink
Merge pull request #27 from bannzai/bugfix/exception/unknown_defaults
Browse files Browse the repository at this point in the history
Handling unknown defaults for NSLayoutConstraint.Attributes and Relation
  • Loading branch information
bannzai authored Jun 6, 2020
2 parents 7288e42 + ea3f94c commit 644f18f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
19 changes: 16 additions & 3 deletions Sources/Gedatsu/FormatterFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,32 @@ public func buildTreeContent(context: Context) -> String {
return content
}
public func buildHeader(context: Context) -> String {
return context.exclusiveConstraints.compactMap { constraint in
var shouldShowErrorMessage = false
let validate: ([HasDisplayName]) -> Bool = { !$0.allSatisfy(\.isValid) }
let content = context.exclusiveConstraints.compactMap { constraint in
let address = addres(of: constraint)
switch (constraint.firstItem, constraint.secondItem) {
case (.none, .none):
return "NSLayoutConstraint: \(address) Unknown case. \(constraint.displayIdentifier)"
case (.some(let lhs), .some(let rhs)):
let (lAttribute, rAttribute) = (constraint.firstAttribute.displayName, constraint.secondAttribute.displayName)
let (lAttribute, rAttribute) = (constraint.firstAttribute, constraint.secondAttribute)
let relation = constraint.relation
return "NSLayoutConstraint: \(address) \(itemType(of: lhs)).\(lAttribute) \(relation.displayName) \(itemType(of: rhs)).\(rAttribute) \(constraint.displayIdentifier)"
shouldShowErrorMessage = shouldShowErrorMessage || validate([lAttribute, rAttribute, relation])
return "NSLayoutConstraint: \(address) \(itemType(of: lhs)).\(lAttribute.displayName) \(relation.displayName) \(itemType(of: rhs)).\(rAttribute.displayName) \(constraint.displayIdentifier)"
case (.some(let item), .none):
shouldShowErrorMessage = shouldShowErrorMessage || validate([constraint.firstAttribute, constraint.relation])
return "NSLayoutConstraint: \(address) \(itemType(of: item)).\(constraint.firstAttribute.displayName) \(constraint.relation.displayName) \(constraint.constant) \(constraint.displayIdentifier)"
case (.none, .some(let item)):
shouldShowErrorMessage = shouldShowErrorMessage || validate([constraint.secondAttribute, constraint.relation])
return "NSLayoutConstraint: \(address) \(itemType(of: item)).\(constraint.secondAttribute.displayName) \(constraint.relation.displayName) \(constraint.constant) \(constraint.displayIdentifier)"
}
}.joined(separator: "\n")
if shouldShowErrorMessage {
return """
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."
https://github.com/bannzai/gedatsu/issues
\(content)
"""
}
return content
}
37 changes: 33 additions & 4 deletions Sources/Gedatsu/NSLayoutConstraintExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,24 @@ internal extension NSLayoutConstraint {
}
}

internal extension NSLayoutConstraint.Attribute {
internal protocol HasDisplayName {
var isValid: Bool { get }
var displayName: String { get }
}

extension NSLayoutConstraint.Attribute: HasDisplayName {
var isValid: Bool {
switch self {
case .left, .right, .top, .bottom, .leading, .trailing, .width, .height, .centerX, .centerY, .lastBaseline, .firstBaseline, .notAnAttribute:
return true
#if os(iOS)
case .leftMargin, .rightMargin, .topMargin, .bottomMargin, .leadingMargin, .trailingMargin, .centerXWithinMargins, .centerYWithinMargins:
return true
#endif
@unknown default:
return false
}
}
var displayName: String {
switch self {
case .left:
Expand Down Expand Up @@ -61,11 +78,23 @@ internal extension NSLayoutConstraint.Attribute {
case .notAnAttribute:
return "notAnAttribute"
@unknown default:
fatalError()
return "⚠️️️️⚠️️️️⚠️️️️ Unknown Attribute case for rawValue == \(rawValue) ⚠️️️️⚠️️️️⚠️️️️ "
}
}
}
internal extension NSLayoutConstraint.Relation {
extension NSLayoutConstraint.Relation: HasDisplayName {
var isValid: Bool {
switch self {
case .equal:
return true
case .greaterThanOrEqual:
return true
case .lessThanOrEqual:
return true
@unknown default:
return false
}
}
var displayName: String {
switch self {
case .equal:
Expand All @@ -75,7 +104,7 @@ internal extension NSLayoutConstraint.Relation {
case .lessThanOrEqual:
return "<="
@unknown default:
fatalError()
return "⚠️️️️⚠️️️️⚠️️️️ Unknown Relation case for rawValue == \(rawValue) ⚠️️️️⚠️️️️⚠️️️️ "
}
}
}

0 comments on commit 644f18f

Please sign in to comment.