1+ use super :: api_connector:: APIOperations ;
12use super :: plugin_parser:: Information ;
23use crate :: core:: config:: CONFIGURATION ;
3- use crate :: core:: {
4- Base as BasePlugin , Installed as InstalledPlugin , Plugin as DetailsPlugin , PluginParser ,
5- } ;
4+ use crate :: core:: { APIConnector , Installed as InstalledPlugin , PluginParser } ;
65use globset:: Glob ;
76use rusqlite:: { params, Connection , Statement } ;
8- use serde:: { Deserialize , Serialize } ;
97use std:: { collections:: HashMap , error:: Error , fs:: read_dir, path:: Path } ;
108
119pub struct Synchronizer { }
@@ -25,14 +23,15 @@ impl Synchronizer {
2523 db_plugins : & HashMap < String , InstalledPlugin > ,
2624 ) {
2725 for ( key, element) in local_plugins {
28- let retrieved_plugin = Self :: fetch_plugin_details ( & element. name ) ;
26+ let retrieved_plugin = APIConnector :: fetch_details ( & element. name ) ;
2927 if db_plugins. contains_key ( key) {
3028 let local_plugin = db_plugins. get ( key) . unwrap ( ) ;
3129 if local_plugin. latest_version != retrieved_plugin. base_plugin . latest_version {
3230 Self :: update_plugin (
3331 & local_plugin. title ,
3432 & retrieved_plugin. base_plugin . latest_version ,
35- ) ;
33+ )
34+ . unwrap ( ) ;
3635 }
3736 } else {
3837 Self :: insert_plugin ( & InstalledPlugin :: new (
@@ -78,56 +77,6 @@ impl Synchronizer {
7877 Ok ( local_plugins)
7978 }
8079
81- pub async fn fetch_plugins (
82- ) -> Result < HashMap < String , BasePlugin > , crate :: core:: synchronizer:: APIError > {
83- match reqwest:: get ( "https://young-hamlet-23901.herokuapp.com/plugins" ) . await {
84- Ok ( response) => match response. json :: < HashMap < String , BasePlugin > > ( ) . await {
85- Ok ( plugins) => Ok ( plugins) ,
86- Err ( _) => Err ( APIError :: FetchError ) ,
87- } ,
88- Err ( _) => Err ( APIError :: FetchError ) ,
89- }
90- }
91-
92- pub fn fetch_plugin_details ( title : & str ) -> DetailsPlugin {
93- let response = reqwest:: blocking:: get ( format ! (
94- "https://young-hamlet-23901.herokuapp.com/plugins/{}" ,
95- title. to_lowercase( )
96- ) )
97- . expect ( "Failed to connect with API" )
98- . json :: < JSONResponse > ( )
99- . expect ( "Failed to parse response" ) ;
100-
101- DetailsPlugin :: new (
102- response. plugin_id ,
103- & response. title ,
104- "" ,
105- & response. category ,
106- & response. current_version ,
107- & response. latest_version ,
108- & response. folders ,
109- & response. files ,
110- )
111- }
112-
113- // Used to synchronize the local database with the remote plugin server
114- pub async fn update_local_plugins ( ) -> Result < ( ) , ( ) > {
115- let fetched_plugins = Self :: fetch_plugins ( ) . await ;
116- let conn = Connection :: open ( & CONFIGURATION . lock ( ) . unwrap ( ) . db_file ) . unwrap ( ) ;
117-
118- if fetched_plugins. is_ok ( ) {
119- for ( _, plugin) in fetched_plugins. unwrap ( ) {
120- let installed_plugin = Synchronizer :: get_plugin ( & plugin. title ) ;
121- if !installed_plugin. is_empty ( )
122- && installed_plugin[ 0 ] . latest_version != plugin. latest_version
123- {
124- conn. execute ( "UPDATE plugin SET current_version = ?1, latest_version = ?2 WHERE plugin_id = ?3" , params ! [ installed_plugin[ 0 ] . description, plugin. latest_version, plugin. plugin_id] ) . unwrap ( ) ;
125- }
126- }
127- }
128- Ok ( ( ) )
129- }
130-
13180 // Creates the local database if it doesn't exist.
13281 pub fn create_plugins_db ( ) {
13382 let conn = Connection :: open ( & CONFIGURATION . lock ( ) . unwrap ( ) . db_file ) . unwrap ( ) ;
@@ -228,32 +177,4 @@ impl Synchronizer {
228177 }
229178 plugins
230179 }
231-
232- pub fn get_plugin ( name : & str ) -> Vec < InstalledPlugin > {
233- let conn = Connection :: open ( & CONFIGURATION . lock ( ) . unwrap ( ) . db_file ) . unwrap ( ) ;
234- let mut stmt = conn
235- . prepare ( "SELECT plugin_id, title, description, category, current_version, latest_version, folder_name FROM plugin WHERE LOWER(title) = ?1" )
236- . unwrap ( ) ;
237-
238- Self :: execute_stmt ( & mut stmt, & name. to_lowercase ( ) )
239- }
240- }
241-
242- #[ derive( Default , Serialize , Deserialize , Debug , Clone , Eq , PartialEq , Hash , PartialOrd , Ord ) ]
243- struct JSONResponse {
244- pub plugin_id : i32 ,
245- pub title : String ,
246- #[ serde( default ) ]
247- pub description : String ,
248- pub category : String ,
249- #[ serde( default ) ]
250- pub current_version : String ,
251- pub latest_version : String ,
252- pub folders : String ,
253- pub files : Vec < String > ,
254- }
255-
256- #[ derive( Debug , Clone ) ]
257- pub enum APIError {
258- FetchError ,
259180}
0 commit comments