-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathStructBuilder.swift
108 lines (87 loc) · 4.08 KB
/
StructBuilder.swift
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
99
100
101
102
103
104
105
106
107
108
//
// RectBuilder.swift
// Eject
//
// Created by Brian King on 10/19/16.
// Copyright © 2016 Brian King. All rights reserved.
//
import Foundation
struct RectBuilder: Builder {
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
guard let parent = parent else { throw XIBParser.Error.needParent }
let key = try attributes.removeRequiredValue(forKey: "key")
let x = try attributes.removeFloatString(forKey: "x") ?? "0"
let y = try attributes.removeFloatString(forKey: "y") ?? "0"
let width = try attributes.removeFloatString(forKey: "width") ?? "0"
let height = try attributes.removeFloatString(forKey: "height") ?? "0"
if key == "frame" && document.configuration.useFrames == false {
return parent
}
try document.addVariableConfiguration(
for: parent.identifier,
attribute: key,
value: BasicValue(value: "CGRect(x: \(x), y: \(y), width: \(width), height: \(height))")
)
return parent
}
}
struct SizeBuilder: Builder {
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
guard let parent = parent else { throw XIBParser.Error.needParent }
let key = try attributes.removeRequiredValue(forKey: "key")
let width = try attributes.removeFloatString(forKey: "width") ?? "0"
let height = try attributes.removeFloatString(forKey: "height") ?? "0"
try document.addVariableConfiguration(
for: parent.identifier,
attribute: key,
value: BasicValue(value: "CGSize(width: \(width), height: \(height))")
)
return parent
}
}
struct InsetBuilder: Builder {
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
guard let parent = parent else { throw XIBParser.Error.needParent }
let key = try attributes.removeRequiredValue(forKey: "key")
let x = try attributes.removeFloat(forKey: "minX")
let y = try attributes.removeFloat(forKey: "minY")
let width = try attributes.removeFloat(forKey: "maxX")
let height = try attributes.removeFloat(forKey: "maxY")
let edgeInsets = "UIEdgeInsets(top: \(y.shortString), left: \(x.shortString), bottom: \((y + height).shortString), right: \((x + width).shortString))"
try document.addVariableConfiguration(
for: parent.identifier,
attribute: key,
value: BasicValue(value: edgeInsets))
return parent
}
}
struct EdgeInsetBuilder: Builder {
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
guard let parent = parent else { throw XIBParser.Error.needParent }
let key = try attributes.removeRequiredValue(forKey: "key")
let top = try attributes.removeFloat(forKey: "top")
let left = try attributes.removeFloat(forKey: "left")
let bottom = try attributes.removeFloat(forKey: "bottom")
let right = try attributes.removeFloat(forKey: "right")
let edgeInsets = "UIEdgeInsets(top: \(top.shortString), left: \(left.shortString), bottom: \(bottom.shortString), right: \(right.shortString))"
try document.addVariableConfiguration(
for: parent.identifier,
attribute: key,
value: BasicValue(value: edgeInsets))
return parent
}
}
struct BasicBuilder: Builder {
let key: String
let format: ValueFormat
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
guard let parent = parent else { throw XIBParser.Error.needParent }
let value = try attributes.removeRequiredValue(forKey: key)
try document.addVariableConfiguration(
for: parent.identifier,
attribute: key,
value: BasicValue(value: value, format: format)
)
return parent
}
}