1+ use std:: fmt;
12use std:: time:: Duration ;
23
34use anyhow:: Result ;
45
5- #[ derive( Debug ) ]
6+ #[ derive( Debug , PartialEq , Eq ) ]
7+ pub enum PythonPackage {
8+ Maturin ,
9+ MyPy ,
10+ PreCommit ,
11+ Pytest ,
12+ PytestCov ,
13+ Ruff ,
14+ Tomli ,
15+ }
16+
17+ impl fmt:: Display for PythonPackage {
18+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
19+ match self {
20+ PythonPackage :: Maturin => write ! ( f, "maturin" ) ,
21+ PythonPackage :: MyPy => write ! ( f, "mypy" ) ,
22+ PythonPackage :: PreCommit => write ! ( f, "pre-commit" ) ,
23+ PythonPackage :: Pytest => write ! ( f, "pytest" ) ,
24+ PythonPackage :: PytestCov => write ! ( f, "pytest-cov" ) ,
25+ PythonPackage :: Ruff => write ! ( f, "ruff" ) ,
26+ PythonPackage :: Tomli => write ! ( f, "tomli" ) ,
27+ }
28+ }
29+ }
30+
31+ #[ derive( Debug , PartialEq , Eq ) ]
632pub enum PreCommitHook {
733 PreCommit ,
834 MyPy ,
935 Ruff ,
1036}
1137
38+ impl fmt:: Display for PreCommitHook {
39+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
40+ match self {
41+ PreCommitHook :: MyPy => write ! ( f, "mypy" ) ,
42+ PreCommitHook :: PreCommit => write ! ( f, "pre-commit" ) ,
43+ PreCommitHook :: Ruff => write ! ( f, "ruff" ) ,
44+ }
45+ }
46+ }
47+
1248pub trait LatestVersion {
1349 fn get_latest_version ( & mut self ) -> Result < ( ) > ;
1450}
1551
1652#[ derive( Debug ) ]
1753pub struct PreCommitHookVersion {
18- pub id : PreCommitHook ,
54+ pub hook : PreCommitHook ,
1955 pub repo : String ,
2056 pub rev : String ,
2157}
@@ -46,29 +82,45 @@ impl LatestVersion for PreCommitHookVersion {
4682 }
4783}
4884
85+ impl PreCommitHookVersion {
86+ pub fn new ( hook : PreCommitHook ) -> Self {
87+ let rev = default_pre_commit_rev ( & hook) ;
88+ let repo = pre_commit_repo ( & hook) ;
89+ PreCommitHookVersion { hook, repo, rev }
90+ }
91+ }
92+
4993#[ derive( Debug ) ]
5094pub struct PythonPackageVersion {
51- pub name : String ,
95+ pub package : PythonPackage ,
5296 pub version : String ,
5397}
5498
5599impl LatestVersion for PythonPackageVersion {
56100 fn get_latest_version ( & mut self ) -> Result < ( ) > {
57- let url = format ! ( "https://pypi.org/pypi/{}/json" , self . name) ;
101+ let name = self . package . to_string ( ) ;
102+ let url = format ! ( "https://pypi.org/pypi/{}/json" , name) ;
58103 let client = reqwest:: blocking:: Client :: new ( ) ;
59104 let response = client
60105 . get ( url)
61106 . timeout ( Duration :: new ( 5 , 0 ) )
62107 . send ( ) ?
63108 . text ( ) ?;
64109 let info: serde_json:: Value = serde_json:: from_str ( & response) ?;
65- self . name = info[ "info" ] [ "name" ] . to_string ( ) . replace ( '"' , "" ) ;
66110 self . version = info[ "info" ] [ "version" ] . to_string ( ) . replace ( '"' , "" ) ;
67111
68112 Ok ( ( ) )
69113 }
70114}
71115
116+ impl PythonPackageVersion {
117+ pub fn new ( package : PythonPackage , min_python_version : & str ) -> Self {
118+ let version = default_version ( & package, min_python_version) ;
119+
120+ PythonPackageVersion { package, version }
121+ }
122+ }
123+
72124#[ derive( Debug ) ]
73125pub struct RustPackageVersion {
74126 pub name : String ,
@@ -95,3 +147,63 @@ impl LatestVersion for RustPackageVersion {
95147 Ok ( ( ) )
96148 }
97149}
150+
151+ pub fn default_version ( package : & PythonPackage , min_python_version : & str ) -> String {
152+ match package {
153+ PythonPackage :: Maturin => "1.4.0" . to_string ( ) ,
154+ PythonPackage :: MyPy => "1.7.1" . to_string ( ) ,
155+
156+ // TODO: This isn't a good resolver but it will work for now. Should be imporoved.
157+ PythonPackage :: PreCommit => {
158+ let version_info: Vec < & str > = min_python_version. split ( '.' ) . collect ( ) ;
159+ if let Ok ( minor) = version_info[ 1 ] . parse :: < i32 > ( ) {
160+ if minor < 10 {
161+ "3.5.0" . to_string ( )
162+ } else {
163+ "3.6.0" . to_string ( )
164+ }
165+ } else {
166+ "3.6.0" . to_string ( )
167+ }
168+ }
169+ PythonPackage :: Pytest => "7.4.2" . to_string ( ) ,
170+ PythonPackage :: PytestCov => "4.1.0" . to_string ( ) ,
171+ PythonPackage :: Ruff => "0.1.7" . to_string ( ) ,
172+ PythonPackage :: Tomli => "2.0.1" . to_string ( ) ,
173+ }
174+ }
175+
176+ pub fn default_pre_commit_rev ( hook : & PreCommitHook ) -> String {
177+ match hook {
178+ PreCommitHook :: MyPy => "v1.7.1" . to_string ( ) ,
179+ PreCommitHook :: PreCommit => "v4.5.0" . to_string ( ) ,
180+ PreCommitHook :: Ruff => "v0.1.7" . to_string ( ) ,
181+ }
182+ }
183+
184+ pub fn pre_commit_repo ( hook : & PreCommitHook ) -> String {
185+ match hook {
186+ PreCommitHook :: MyPy => "https://github.com/pre-commit/mirrors-mypy" . to_string ( ) ,
187+ PreCommitHook :: PreCommit => "https://github.com/pre-commit/pre-commit-hooks" . to_string ( ) ,
188+ PreCommitHook :: Ruff => "https://github.com/astral-sh/ruff-pre-commit" . to_string ( ) ,
189+ }
190+ }
191+
192+ #[ cfg( test) ]
193+ mod tests {
194+ use super :: * ;
195+
196+ #[ test]
197+ fn test_less_than_three_ten ( ) {
198+ let version = default_version ( & PythonPackage :: PreCommit , "3.9" ) ;
199+
200+ assert_eq ! ( "3.5.0" , version)
201+ }
202+
203+ #[ test]
204+ fn test_three_ten ( ) {
205+ let version = default_version ( & PythonPackage :: PreCommit , "3.10" ) ;
206+
207+ assert_eq ! ( "3.6.0" , version)
208+ }
209+ }
0 commit comments