11use crate :: env_manager:: {
2- emit_download_progress , DownloadStatus , EnvironmentProvider , EnvironmentVersion ,
2+ DownloadStatus , EnvironmentProvider , EnvironmentVersion , emit_download_progress ,
33} ;
44use log:: { debug, error, info, warn} ;
55use serde:: { Deserialize , Serialize } ;
@@ -45,7 +45,10 @@ impl ScalaEnvironmentProvider {
4545 error ! ( "创建 Scala 安装目录失败: {}" , e) ;
4646 }
4747
48- Self { install_dir, cache_file }
48+ Self {
49+ install_dir,
50+ cache_file,
51+ }
4952 }
5053
5154 fn get_default_install_dir ( ) -> PathBuf {
@@ -147,17 +150,14 @@ impl ScalaEnvironmentProvider {
147150 request = request. header ( "Authorization" , format ! ( "token {}" , token) ) ;
148151 }
149152
150- let response = request
151- . send ( )
152- . await
153- . map_err ( |e| {
154- // 如果请求失败,尝试使用缓存(即使过期)
155- if let Some ( cached_releases) = self . read_cache_ignore_expiry ( ) {
156- warn ! ( "GitHub API 请求失败,使用过期缓存: {}" , e) ;
157- return format ! ( "GitHub API 请求失败,已使用缓存数据: {}" , e) ;
158- }
159- format ! ( "请求 GitHub API 失败: {}" , e)
160- } ) ?;
153+ let response = request. send ( ) . await . map_err ( |e| {
154+ // 如果请求失败,尝试使用缓存(即使过期)
155+ if let Some ( cached_releases) = self . read_cache_ignore_expiry ( ) {
156+ warn ! ( "GitHub API 请求失败,使用过期缓存: {}" , e) ;
157+ return format ! ( "GitHub API 请求失败,已使用缓存数据: {}" , e) ;
158+ }
159+ format ! ( "请求 GitHub API 失败: {}" , e)
160+ } ) ?;
161161
162162 let status = response. status ( ) ;
163163
@@ -170,7 +170,10 @@ impl ScalaEnvironmentProvider {
170170 if let Some ( cached_releases) = self . read_cache_ignore_expiry ( ) {
171171 return Ok ( cached_releases) ;
172172 }
173- format ! ( "GitHub API 限流已超出。请稍后再试,或设置 GITHUB_TOKEN 环境变量以增加限额。详情: {}" , body)
173+ format ! (
174+ "GitHub API 限流已超出。请稍后再试,或设置 GITHUB_TOKEN 环境变量以增加限额。详情: {}" ,
175+ body
176+ )
174177 } else {
175178 format ! ( "GitHub API 返回错误 ({}): {}" , status, body)
176179 }
@@ -204,18 +207,16 @@ impl ScalaEnvironmentProvider {
204207 }
205208
206209 match std:: fs:: read_to_string ( & self . cache_file ) {
207- Ok ( content) => {
208- match serde_json:: from_str :: < CachedReleases > ( & content) {
209- Ok ( cached) => {
210- info ! ( "使用缓存的 Scala 版本列表(忽略过期时间)" ) ;
211- Some ( cached. releases )
212- }
213- Err ( e) => {
214- warn ! ( "解析缓存文件失败: {}" , e) ;
215- None
216- }
210+ Ok ( content) => match serde_json:: from_str :: < CachedReleases > ( & content) {
211+ Ok ( cached) => {
212+ info ! ( "使用缓存的 Scala 版本列表(忽略过期时间)" ) ;
213+ Some ( cached. releases )
217214 }
218- }
215+ Err ( e) => {
216+ warn ! ( "解析缓存文件失败: {}" , e) ;
217+ None
218+ }
219+ } ,
219220 Err ( e) => {
220221 warn ! ( "读取缓存文件失败: {}" , e) ;
221222 None
@@ -288,8 +289,7 @@ impl ScalaEnvironmentProvider {
288289 ) ;
289290
290291 // 创建目标文件
291- let mut file = std:: fs:: File :: create ( dest)
292- . map_err ( |e| format ! ( "创建文件失败: {}" , e) ) ?;
292+ let mut file = std:: fs:: File :: create ( dest) . map_err ( |e| format ! ( "创建文件失败: {}" , e) ) ?;
293293
294294 let mut downloaded: u64 = 0 ;
295295 let mut stream = response. bytes_stream ( ) ;
@@ -329,7 +329,11 @@ impl ScalaEnvironmentProvider {
329329 app_handle : AppHandle ,
330330 version : & str ,
331331 ) -> Result < ( ) , String > {
332- info ! ( "开始解压: {} -> {}" , archive_path. display( ) , dest_dir. display( ) ) ;
332+ info ! (
333+ "开始解压: {} -> {}" ,
334+ archive_path. display( ) ,
335+ dest_dir. display( )
336+ ) ;
333337
334338 emit_download_progress (
335339 & app_handle,
@@ -340,8 +344,7 @@ impl ScalaEnvironmentProvider {
340344 DownloadStatus :: Extracting ,
341345 ) ;
342346
343- std:: fs:: create_dir_all ( dest_dir)
344- . map_err ( |e| format ! ( "创建目录失败: {}" , e) ) ?;
347+ std:: fs:: create_dir_all ( dest_dir) . map_err ( |e| format ! ( "创建目录失败: {}" , e) ) ?;
345348
346349 if archive_path. extension ( ) . and_then ( |s| s. to_str ( ) ) == Some ( "zip" ) {
347350 // 解压 ZIP 文件
@@ -358,14 +361,14 @@ impl ScalaEnvironmentProvider {
358361 fn extract_zip ( & self , archive_path : & PathBuf , dest_dir : & PathBuf ) -> Result < ( ) , String > {
359362 use zip:: ZipArchive ;
360363
361- let file = std :: fs :: File :: open ( archive_path )
362- . map_err ( |e| format ! ( "打开压缩文件失败: {}" , e) ) ?;
364+ let file =
365+ std :: fs :: File :: open ( archive_path ) . map_err ( |e| format ! ( "打开压缩文件失败: {}" , e) ) ?;
363366
364- let mut archive = ZipArchive :: new ( file)
365- . map_err ( |e| format ! ( "读取 ZIP 文件失败: {}" , e) ) ?;
367+ let mut archive = ZipArchive :: new ( file) . map_err ( |e| format ! ( "读取 ZIP 文件失败: {}" , e) ) ?;
366368
367369 for i in 0 ..archive. len ( ) {
368- let mut file = archive. by_index ( i)
370+ let mut file = archive
371+ . by_index ( i)
369372 . map_err ( |e| format ! ( "读取 ZIP 条目失败: {}" , e) ) ?;
370373
371374 let outpath = match file. enclosed_name ( ) {
@@ -374,15 +377,13 @@ impl ScalaEnvironmentProvider {
374377 } ;
375378
376379 if file. name ( ) . ends_with ( '/' ) {
377- std:: fs:: create_dir_all ( & outpath)
378- . map_err ( |e| format ! ( "创建目录失败: {}" , e) ) ?;
380+ std:: fs:: create_dir_all ( & outpath) . map_err ( |e| format ! ( "创建目录失败: {}" , e) ) ?;
379381 } else {
380382 if let Some ( p) = outpath. parent ( ) {
381- std:: fs:: create_dir_all ( p)
382- . map_err ( |e| format ! ( "创建目录失败: {}" , e) ) ?;
383+ std:: fs:: create_dir_all ( p) . map_err ( |e| format ! ( "创建目录失败: {}" , e) ) ?;
383384 }
384- let mut outfile = std :: fs :: File :: create ( & outpath )
385- . map_err ( |e| format ! ( "创建文件失败: {}" , e) ) ?;
385+ let mut outfile =
386+ std :: fs :: File :: create ( & outpath ) . map_err ( |e| format ! ( "创建文件失败: {}" , e) ) ?;
386387 std:: io:: copy ( & mut file, & mut outfile)
387388 . map_err ( |e| format ! ( "解压文件失败: {}" , e) ) ?;
388389 }
@@ -392,8 +393,7 @@ impl ScalaEnvironmentProvider {
392393 {
393394 use std:: os:: unix:: fs:: PermissionsExt ;
394395 if let Some ( mode) = file. unix_mode ( ) {
395- std:: fs:: set_permissions ( & outpath, std:: fs:: Permissions :: from_mode ( mode) )
396- . ok ( ) ;
396+ std:: fs:: set_permissions ( & outpath, std:: fs:: Permissions :: from_mode ( mode) ) . ok ( ) ;
397397 }
398398 }
399399 }
@@ -405,13 +405,14 @@ impl ScalaEnvironmentProvider {
405405 use flate2:: read:: GzDecoder ;
406406 use tar:: Archive ;
407407
408- let file = std :: fs :: File :: open ( archive_path )
409- . map_err ( |e| format ! ( "打开压缩文件失败: {}" , e) ) ?;
408+ let file =
409+ std :: fs :: File :: open ( archive_path ) . map_err ( |e| format ! ( "打开压缩文件失败: {}" , e) ) ?;
410410
411411 let gz = GzDecoder :: new ( file) ;
412412 let mut archive = Archive :: new ( gz) ;
413413
414- archive. unpack ( dest_dir)
414+ archive
415+ . unpack ( dest_dir)
415416 . map_err ( |e| format ! ( "解压 tar.gz 失败: {}" , e) ) ?;
416417
417418 Ok ( ( ) )
@@ -421,10 +422,12 @@ impl ScalaEnvironmentProvider {
421422 async fn update_plugin_config ( & self , version : & str , install_path : & str ) -> Result < ( ) , String > {
422423 use crate :: config:: { get_app_config_internal, update_app_config} ;
423424
424- info ! ( "更新 Scala 插件配置: 版本={}, 路径={}" , version, install_path) ;
425+ info ! (
426+ "更新 Scala 插件配置: 版本={}, 路径={}" ,
427+ version, install_path
428+ ) ;
425429
426- let mut config = get_app_config_internal ( )
427- . map_err ( |e| format ! ( "获取配置失败: {}" , e) ) ?;
430+ let mut config = get_app_config_internal ( ) . map_err ( |e| format ! ( "获取配置失败: {}" , e) ) ?;
428431
429432 if let Some ( ref mut plugins) = config. plugins {
430433 if let Some ( scala_plugin) = plugins. iter_mut ( ) . find ( |p| p. language == "scala" ) {
@@ -439,11 +442,15 @@ impl ScalaEnvironmentProvider {
439442 } ;
440443 scala_plugin. run_command = Some ( String :: from ( run_cmd) ) ;
441444
442- info ! ( "已更新 Scala 插件配置: execute_home={}, run_command={}" , install_path, run_cmd) ;
445+ info ! (
446+ "已更新 Scala 插件配置: execute_home={}, run_command={}" ,
447+ install_path, run_cmd
448+ ) ;
443449 }
444450 }
445451
446- update_app_config ( config) . await
452+ update_app_config ( config)
453+ . await
447454 . map_err ( |e| format ! ( "保存配置失败: {}" , e) ) ?;
448455
449456 Ok ( ( ) )
@@ -511,14 +518,15 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
511518 return Ok ( installed) ;
512519 }
513520
514- let entries = std :: fs :: read_dir ( & self . install_dir )
515- . map_err ( |e| format ! ( "读取安装目录失败: {}" , e) ) ?;
521+ let entries =
522+ std :: fs :: read_dir ( & self . install_dir ) . map_err ( |e| format ! ( "读取安装目录失败: {}" , e) ) ?;
516523
517524 for entry in entries {
518525 if let Ok ( entry) = entry {
519526 let path = entry. path ( ) ;
520527 if path. is_dir ( ) {
521- let version = path. file_name ( )
528+ let version = path
529+ . file_name ( )
522530 . and_then ( |n| n. to_str ( ) )
523531 . unwrap_or ( "" )
524532 . to_string ( ) ;
@@ -584,15 +592,19 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
584592
585593 // 下载文件
586594 let download_url = & version_info. download_url ;
587- let file_name = download_url. split ( '/' ) . last ( )
595+ let file_name = download_url
596+ . split ( '/' )
597+ . last ( )
588598 . ok_or_else ( || "无效的下载 URL" . to_string ( ) ) ?;
589599 let temp_file = std:: env:: temp_dir ( ) . join ( file_name) ;
590600
591- self . download_file ( download_url, & temp_file, app_handle. clone ( ) , version) . await ?;
601+ self . download_file ( download_url, & temp_file, app_handle. clone ( ) , version)
602+ . await ?;
592603
593604 // 解压到安装目录
594605 let install_path = self . get_version_install_path ( version) ;
595- self . extract_archive ( & temp_file, & install_path, app_handle. clone ( ) , version) . await ?;
606+ self . extract_archive ( & temp_file, & install_path, app_handle. clone ( ) , version)
607+ . await ?;
596608
597609 // 清理临时文件
598610 std:: fs:: remove_file ( & temp_file) . ok ( ) ;
@@ -621,7 +633,8 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
621633 }
622634
623635 // 更新插件配置
624- self . update_plugin_config ( version, & actual_install_path. to_string_lossy ( ) ) . await ?;
636+ self . update_plugin_config ( version, & actual_install_path. to_string_lossy ( ) )
637+ . await ?;
625638
626639 emit_download_progress (
627640 & app_handle,
@@ -659,7 +672,8 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
659672 }
660673 }
661674
662- self . update_plugin_config ( version, & actual_install_path. to_string_lossy ( ) ) . await ?;
675+ self . update_plugin_config ( version, & actual_install_path. to_string_lossy ( ) )
676+ . await ?;
663677
664678 info ! ( "成功切换到 Scala {}" , version) ;
665679 Ok ( ( ) )
@@ -668,8 +682,7 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
668682 async fn get_current_version ( & self ) -> Result < Option < String > , String > {
669683 use crate :: config:: get_app_config_internal;
670684
671- let config = get_app_config_internal ( )
672- . map_err ( |e| format ! ( "获取配置失败: {}" , e) ) ?;
685+ let config = get_app_config_internal ( ) . map_err ( |e| format ! ( "获取配置失败: {}" , e) ) ?;
673686
674687 if let Some ( plugins) = config. plugins {
675688 if let Some ( scala_plugin) = plugins. iter ( ) . find ( |p| p. language == "scala" ) {
0 commit comments