1- use crate :: { APP_USER_AGENT , error:: Result } ;
2- use anyhow:: { Context , anyhow, bail} ;
1+ use crate :: {
2+ Config ,
3+ models:: { CrateData , CrateOwner , OwnerKind , ReleaseData , Search , SearchCrate , SearchMeta } ,
4+ } ;
5+ use anyhow:: { Context , Result , anyhow, bail} ;
36use chrono:: { DateTime , Utc } ;
47use docs_rs_types:: Version ;
5- use docs_rs_utils:: retry_async;
8+ use docs_rs_utils:: { APP_USER_AGENT , retry_async} ;
69use reqwest:: header:: { ACCEPT , HeaderValue , USER_AGENT } ;
7- use serde:: { Deserialize , Serialize } ;
8- use std:: fmt;
10+ use serde:: Deserialize ;
911use tracing:: instrument;
1012use url:: Url ;
1113
@@ -16,84 +18,14 @@ pub struct RegistryApi {
1618 client : reqwest:: Client ,
1719}
1820
19- #[ derive( Debug ) ]
20- pub struct CrateData {
21- pub ( crate ) owners : Vec < CrateOwner > ,
22- }
23-
24- #[ derive( Debug ) ]
25- pub ( crate ) struct ReleaseData {
26- pub ( crate ) release_time : DateTime < Utc > ,
27- pub ( crate ) yanked : bool ,
28- pub ( crate ) downloads : i32 ,
29- }
30-
31- impl Default for ReleaseData {
32- fn default ( ) -> ReleaseData {
33- ReleaseData {
34- release_time : Utc :: now ( ) ,
35- yanked : false ,
36- downloads : 0 ,
37- }
38- }
39- }
40-
41- #[ derive( Debug , Clone ) ]
42- pub struct CrateOwner {
43- pub ( crate ) avatar : String ,
44- pub ( crate ) login : String ,
45- pub ( crate ) kind : OwnerKind ,
46- }
47-
48- #[ derive(
49- Debug ,
50- Clone ,
51- Copy ,
52- PartialEq ,
53- Eq ,
54- PartialOrd ,
55- Ord ,
56- Serialize ,
57- Deserialize ,
58- sqlx:: Type ,
59- bincode:: Encode ,
60- ) ]
61- #[ sqlx( type_name = "owner_kind" , rename_all = "lowercase" ) ]
62- #[ serde( rename_all = "lowercase" ) ]
63- pub enum OwnerKind {
64- User ,
65- Team ,
66- }
67-
68- impl fmt:: Display for OwnerKind {
69- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
70- match self {
71- Self :: User => f. write_str ( "user" ) ,
72- Self :: Team => f. write_str ( "team" ) ,
73- }
21+ impl RegistryApi {
22+ pub fn from_config ( config : & Config ) -> Result < Self > {
23+ Self :: new (
24+ config. registry_api_host . clone ( ) ,
25+ config. crates_io_api_call_retries ,
26+ )
7427 }
75- }
76-
77- #[ derive( Deserialize , Debug ) ]
7828
79- pub ( crate ) struct SearchCrate {
80- pub ( crate ) name : String ,
81- }
82-
83- #[ derive( Deserialize , Debug ) ]
84-
85- pub ( crate ) struct SearchMeta {
86- pub ( crate ) next_page : Option < String > ,
87- pub ( crate ) prev_page : Option < String > ,
88- }
89-
90- #[ derive( Deserialize , Debug ) ]
91- pub ( crate ) struct Search {
92- pub ( crate ) crates : Vec < SearchCrate > ,
93- pub ( crate ) meta : SearchMeta ,
94- }
95-
96- impl RegistryApi {
9729 pub fn new ( api_base : Url , max_retries : u32 ) -> Result < Self > {
9830 let headers = vec ! [
9931 ( USER_AGENT , HeaderValue :: from_static( APP_USER_AGENT ) ) ,
@@ -124,11 +56,7 @@ impl RegistryApi {
12456 }
12557
12658 #[ instrument( skip( self ) ) ]
127- pub ( crate ) async fn get_release_data (
128- & self ,
129- name : & str ,
130- version : & Version ,
131- ) -> Result < ReleaseData > {
59+ pub async fn get_release_data ( & self , name : & str , version : & Version ) -> Result < ReleaseData > {
13260 let ( release_time, yanked, downloads) = self
13361 . get_release_time_yanked_downloads ( name, version)
13462 . await
@@ -256,7 +184,7 @@ impl RegistryApi {
256184 }
257185
258186 /// Fetch crates from the registry's API
259- pub ( crate ) async fn search ( & self , query_params : & str ) -> Result < Search > {
187+ pub async fn search ( & self , query_params : & str ) -> Result < Search > {
260188 #[ derive( Deserialize , Debug ) ]
261189 struct SearchError {
262190 detail : String ,
0 commit comments