1+ use  cargo_util_schemas:: core:: PartialVersion ; 
2+ use  cargo_util_schemas:: manifest:: RustVersion ; 
3+ 
14use  super :: encode:: Metadata ; 
25use  crate :: core:: dependency:: DepKind ; 
36use  crate :: core:: { Dependency ,  PackageId ,  PackageIdSpec ,  PackageIdSpecQuery ,  Summary ,  Target } ; 
@@ -48,7 +51,7 @@ pub struct Resolve {
4851
4952/// A version to indicate how a `Cargo.lock` should be serialized. 
5053/// 
51- /// When creating a new lockfile, the version with `#[ default]`  is used. 
54+ /// When creating a new lockfile, the version in [`ResolveVersion:: default`]  is used. 
5255/// If an old version of lockfile already exists, it will stay as-is. 
5356/// 
5457/// It's important that if a new version is added that this is not updated 
@@ -64,25 +67,30 @@ pub struct Resolve {
6467/// 
6568/// It's theorized that we can add more here over time to track larger changes 
6669/// to the `Cargo.lock` format, but we've yet to see how that strategy pans out. 
67- #[ derive( Default ,   PartialEq ,  Eq ,  Clone ,  Copy ,  Debug ,  PartialOrd ,  Ord ) ]  
70+ #[ derive( PartialEq ,  Eq ,  Clone ,  Copy ,  Debug ,  PartialOrd ,  Ord ) ]  
6871pub  enum  ResolveVersion  { 
6972    /// Historical baseline for when this abstraction was added. 
7073V1 , 
7174    /// A more compact format, more amenable to avoiding source-control merge 
7275/// conflicts. The `dependencies` arrays are compressed and checksums are 
73- /// listed inline. Introduced in 2019 in version 1.38. New lockfiles use 
74- /// V2 by default from 1.41 to 1.52. 
76+ /// listed inline. 
77+ /// 
78+ /// * Introduced in 2019 in version 1.38. 
79+ /// * New lockfiles use V2 by default from 1.41 to 1.52. 
7580V2 , 
7681    /// A format that explicitly lists a `version` at the top of the file as 
7782/// well as changing how git dependencies are encoded. Dependencies with 
7883/// `branch = "master"` are no longer encoded the same way as those without 
79- /// branch specifiers. Introduced in 2020 in version 1.47. New lockfiles use 
80- /// V3 by default staring in 1.53. 
81- #[ default]  
84+ /// branch specifiers. 
85+ /// 
86+ /// * Introduced in 2020 in version 1.47. 
87+ /// * New lockfiles use V3 by default starting in 1.53. 
8288V3 , 
8389    /// SourceId URL serialization is aware of URL encoding. For example, 
8490/// `?branch=foo bar` is now encoded as `?branch=foo+bar` and can be decoded 
85- /// back and forth correctly. Introduced in 2024 in version 1.77. 
91+ /// back and forth correctly. 
92+ /// 
93+ /// * Introduced in 2024 in version 1.78. 
8694V4 , 
8795    /// Unstable. Will collect a certain amount of changes and then go. 
8896/// 
@@ -91,6 +99,17 @@ pub enum ResolveVersion {
9199} 
92100
93101impl  ResolveVersion  { 
102+     /// Gets the default lockfile version. 
103+ /// 
104+ /// This is intended to be private. 
105+ /// You shall use [`ResolveVersion::with_rust_version`] always. 
106+ /// 
107+ /// Update this and the description of enum variants of [`ResolveVersion`] 
108+ /// when we're changing the default lockfile version. 
109+ fn  default ( )  -> ResolveVersion  { 
110+         ResolveVersion :: V3 
111+     } 
112+ 
94113    /// The maximum version of lockfile made into the stable channel. 
95114/// 
96115/// Any version larger than this needs `-Znext-lockfile-bump` to enable. 
@@ -99,6 +118,40 @@ impl ResolveVersion {
99118pub  fn  max_stable ( )  -> ResolveVersion  { 
100119        ResolveVersion :: V4 
101120    } 
121+ 
122+     /// Gets the default lockfile version for the given Rust version. 
123+ pub  fn  with_rust_version ( rust_version :  Option < & RustVersion > )  -> Self  { 
124+         let  Some ( rust_version)  = rust_version else  { 
125+             return  ResolveVersion :: default ( ) ; 
126+         } ; 
127+ 
128+         let  rust_1_41 = PartialVersion  { 
129+             major :  1 , 
130+             minor :  Some ( 41 ) , 
131+             patch :  None , 
132+             pre :  None , 
133+             build :  None , 
134+         } 
135+         . try_into ( ) 
136+         . expect ( "PartialVersion 1.41" ) ; 
137+         let  rust_1_53 = PartialVersion  { 
138+             major :  1 , 
139+             minor :  Some ( 53 ) , 
140+             patch :  None , 
141+             pre :  None , 
142+             build :  None , 
143+         } 
144+         . try_into ( ) 
145+         . expect ( "PartialVersion 1.53" ) ; 
146+ 
147+         if  rust_version >= & rust_1_53 { 
148+             ResolveVersion :: V3 
149+         }  else  if  rust_version >= & rust_1_41 { 
150+             ResolveVersion :: V2 
151+         }  else  { 
152+             ResolveVersion :: V1 
153+         } 
154+     } 
102155} 
103156
104157impl  Resolve  { 
0 commit comments