11use std:: fs;
22use std:: io:: Read ;
3- use std:: path:: PathBuf ;
3+ use std:: path:: { Path , PathBuf } ;
44
55use directories:: ProjectDirs ;
66use serde:: { Deserialize , Serialize } ;
@@ -13,6 +13,14 @@ pub struct InstalledPluginInfo {
1313 pub description : String ,
1414}
1515
16+ #[ derive( Deserialize ) ]
17+ struct InstalledPluginManifest {
18+ id : String ,
19+ name : String ,
20+ version : String ,
21+ description : String ,
22+ }
23+
1624pub fn get_plugins_dir ( ) -> Result < PathBuf , String > {
1725 let proj_dirs = ProjectDirs :: from ( "com" , "debba" , "tabularis" )
1826 . ok_or_else ( || "Could not determine project directories" . to_string ( ) ) ?;
@@ -24,6 +32,27 @@ pub fn get_plugins_dir() -> Result<PathBuf, String> {
2432 Ok ( plugins_dir)
2533}
2634
35+ pub ( crate ) fn read_plugin_info_from_dir ( path : & Path ) -> Result < InstalledPluginInfo , String > {
36+ let manifest_path = path. join ( "manifest.json" ) ;
37+ let manifest_str = fs:: read_to_string ( & manifest_path)
38+ . map_err ( |e| format ! ( "Failed to read plugin manifest {:?}: {}" , manifest_path, e) ) ?;
39+
40+ let manifest: InstalledPluginManifest = serde_json:: from_str ( & manifest_str)
41+ . map_err ( |e| format ! ( "Failed to parse plugin manifest {:?}: {}" , manifest_path, e) ) ?;
42+
43+ Ok ( InstalledPluginInfo {
44+ id : manifest. id ,
45+ name : manifest. name ,
46+ version : manifest. version ,
47+ description : manifest. description ,
48+ } )
49+ }
50+
51+ pub fn read_installed_plugin ( plugin_id : & str ) -> Result < InstalledPluginInfo , String > {
52+ let plugins_dir = get_plugins_dir ( ) ?;
53+ read_plugin_info_from_dir ( & plugins_dir. join ( plugin_id) )
54+ }
55+
2756pub async fn download_and_install ( plugin_id : & str , download_url : & str ) -> Result < ( ) , String > {
2857 let plugins_dir = get_plugins_dir ( ) ?;
2958 let tmp_dir = plugins_dir. join ( format ! ( ".tmp-{}" , plugin_id) ) ;
@@ -210,30 +239,9 @@ pub fn list_installed() -> Result<Vec<InstalledPluginInfo>, String> {
210239 continue ;
211240 }
212241
213- let manifest_str = match fs:: read_to_string ( & manifest_path) {
214- Ok ( s) => s,
215- Err ( _) => continue ,
216- } ;
217-
218- #[ derive( Deserialize ) ]
219- struct Manifest {
220- id : String ,
221- name : String ,
222- version : String ,
223- description : String ,
242+ if let Ok ( plugin) = read_plugin_info_from_dir ( & path) {
243+ plugins. push ( plugin) ;
224244 }
225-
226- let manifest: Manifest = match serde_json:: from_str ( & manifest_str) {
227- Ok ( m) => m,
228- Err ( _) => continue ,
229- } ;
230-
231- plugins. push ( InstalledPluginInfo {
232- id : manifest. id ,
233- name : manifest. name ,
234- version : manifest. version ,
235- description : manifest. description ,
236- } ) ;
237245 }
238246
239247 Ok ( plugins)
0 commit comments