@@ -4,19 +4,13 @@ import Foundation
4
4
/**
5
5
An action signature (e.g. for committers, taggers, etc).
6
6
*/
7
- public class Signature /*: internal RawRepresentable*/ {
7
+ public struct Signature /*: internal RawRepresentable*/ {
8
8
var rawValue : git_signature
9
- var managed : Bool = false
10
9
11
10
init ( rawValue: git_signature ) {
12
11
self . rawValue = rawValue
13
12
}
14
13
15
- deinit {
16
- guard managed else { return }
17
- git_signature_free ( & rawValue)
18
- }
19
-
20
14
public static func `default`( for repository: Repository ) throws -> Signature {
21
15
let pointer = UnsafeMutablePointer< UnsafeMutablePointer< git_signature>?> . allocate( capacity: 1 )
22
16
defer { pointer. deallocate ( ) }
@@ -36,17 +30,16 @@ public class Signature /*: internal RawRepresentable*/ {
36
30
- time: The time at which the action occurred.
37
31
- timeZone: The time's corresponding time zone.
38
32
*/
39
- public convenience init ( name: String ,
40
- email: String ,
41
- time: Date = Date ( ) ,
42
- timeZone: TimeZone = TimeZone . current) throws
33
+ public init ( name: String ,
34
+ email: String ,
35
+ time: Date = Date ( ) ,
36
+ timeZone: TimeZone = TimeZone . current) throws
43
37
{
44
38
var pointer : UnsafeMutablePointer < git_signature > ?
45
39
let offset = Int32 ( timeZone. secondsFromGMT ( for: time) / 60 )
46
40
let time = git_time_t ( time. timeIntervalSince1970)
47
41
try attempt { git_signature_new ( & pointer, name, email, time, offset) }
48
42
self . init ( rawValue: pointer!. pointee)
49
- // managed = true
50
43
}
51
44
52
45
/// The name of the signer.
@@ -88,3 +81,32 @@ extension Signature: Hashable {
88
81
hasher. combine ( rawValue. when. offset)
89
82
}
90
83
}
84
+
85
+ // MARK: - Codable
86
+
87
+ extension Signature : Codable {
88
+ private enum CodingKeys : String , CodingKey {
89
+ case name
90
+ case email
91
+ case time
92
+ case timeZone
93
+ }
94
+
95
+ public init ( from decoder: Decoder ) throws {
96
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
97
+ let name = try container. decode ( String . self, forKey: . name)
98
+ let email = try container. decode ( String . self, forKey: . email)
99
+ let time = try container. decode ( Date . self, forKey: . time)
100
+ let timeZone = try container. decode ( TimeZone . self, forKey: . timeZone)
101
+
102
+ try self . init ( name: name, email: email, time: time, timeZone: timeZone)
103
+ }
104
+
105
+ public func encode( to encoder: Encoder ) throws {
106
+ var container = encoder. container ( keyedBy: CodingKeys . self)
107
+ try container. encode ( name, forKey: . name)
108
+ try container. encode ( email, forKey: . email)
109
+ try container. encode ( time, forKey: . time)
110
+ try container. encode ( timeZone, forKey: . timeZone)
111
+ }
112
+ }
0 commit comments