@@ -15,50 +15,44 @@ import TSCUtility
1515/// A checkout state represents the current state of a repository.
1616///
1717/// A state will always has a revision. It can also have a branch or a version but not both.
18- public struct CheckoutState : Equatable , Hashable {
18+ public enum CheckoutState : Equatable , Hashable {
1919
20- /// The revision of the checkout.
21- public let revision : Revision
22-
23- /// The version of the checkout, if known.
24- public let version : Version ?
25-
26- /// The branch of the checkout, if known.
27- public let branch : String ?
28-
29- /// Create a checkout state with given data. It is invalid to provide both version and branch.
30- ///
31- /// This is deliberately fileprivate so CheckoutState is not initialized
32- /// with both version and branch. All other initializers should delegate to it.
33- fileprivate init ( revision: Revision , version: Version ? , branch: String ? ) {
34- assert ( version == nil || branch == nil , " Can't set both branch and version. " )
35- self . revision = revision
36- self . version = version
37- self . branch = branch
38- }
39-
40- /// Create a checkout state with given revision and branch.
41- public init ( revision: Revision , branch: String ? = nil ) {
42- self . init ( revision: revision, version: nil , branch: branch)
43- }
20+ case revision( Revision )
21+ case version( Version , revision: Revision )
22+ case branch( name: String , revision: Revision )
4423
45- /// Create a checkout state with given revision and version.
46- public init ( revision: Revision , version: Version ) {
47- self . init ( revision: revision, version: version, branch: nil )
24+ /// The revision of the checkout.
25+ public var revision : Revision {
26+ get {
27+ switch self {
28+ case . revision( let revision) :
29+ return revision
30+ case . version( _, let revision) :
31+ return revision
32+ case . branch( _, let revision) :
33+ return revision
34+ }
35+ }
4836 }
4937
5038 public var isBranchOrRevisionBased : Bool {
51- return version == nil
39+ switch self {
40+ case . revision, . branch:
41+ return true
42+ case . version:
43+ return false
44+ }
5245 }
5346
5447 /// Returns requirement induced by this state.
5548 public var requirement : PackageRequirement {
56- if let version = version {
49+ switch self {
50+ case . revision( let revision) :
51+ return . revision( revision. identifier)
52+ case . version( let version, _) :
5753 return . versionSet( . exact( version) )
58- } else if let branch = branch {
54+ case . branch ( let branch, _ ) :
5955 return . revision( branch)
60- } else {
61- return . revision( revision. identifier)
6256 }
6357 }
6458}
@@ -67,27 +61,62 @@ public struct CheckoutState: Equatable, Hashable {
6761
6862extension CheckoutState : CustomStringConvertible {
6963 public var description : String {
70- return version? . description ?? branch ?? revision. identifier
64+ switch self {
65+ case . revision( let revision) :
66+ return revision. identifier
67+ case . version( let version, _) :
68+ return version. description
69+ case . branch( let branch, _) :
70+ return branch
71+ }
7172 }
7273}
7374
7475// MARK: - JSON
7576
7677extension CheckoutState : JSONMappable , JSONSerializable {
7778 public init ( json: JSON ) throws {
78- self . init (
79- revision: try json. get ( " revision " ) ,
80- version: json. get ( " version " ) ,
81- branch: json. get ( " branch " )
82- )
79+ let revision : Revision = try json. get ( " revision " )
80+ let version : Version ? = json. get ( " version " )
81+ let branch : String ? = json. get ( " branch " )
82+
83+ switch ( version, branch) {
84+ case ( . none, . none) :
85+ self = . revision( revision)
86+ case ( . some( let version) , . none) :
87+ self = . version( version, revision: revision)
88+ case ( . none, . some( let branch) ) :
89+ self = . branch( name: branch, revision: revision)
90+ case ( . some( _) , . some( _) ) :
91+ preconditionFailure ( " Can't set both branch and version. " )
92+ }
8393 }
8494
8595 public func toJSON( ) -> JSON {
86- return . init( [
87- " revision " : revision. identifier,
88- " version " : version. toJSON ( ) ,
89- " branch " : branch. toJSON ( ) ,
90- ] )
96+ let revision : Revision
97+ let version : Version ?
98+ let branch : String ?
99+
100+ switch self {
101+ case . revision( let _revision) :
102+ revision = _revision
103+ version = nil
104+ branch = nil
105+ case . version( let _version, let _revision) :
106+ revision = _revision
107+ version = _version
108+ branch = nil
109+ case . branch( let _branch, let _revision) :
110+ revision = _revision
111+ version = nil
112+ branch = _branch
113+ }
114+
115+ return . init( [
116+ " revision " : revision. identifier,
117+ " version " : version. toJSON ( ) ,
118+ " branch " : branch. toJSON ( ) ,
119+ ] )
91120 }
92121}
93122
0 commit comments