1- use serde:: * ;
2- use std:: path:: PathBuf ;
3-
41use crate :: { app_state:: AppState , hiper:: get_hiper_dir, DynResult } ;
5-
6- #[ derive( Clone , Deserialize , Serialize ) ]
7- struct Config {
8- pub auto_restart : bool ,
9- pub use_tun : bool ,
10- pub token : String ,
11- }
2+ use std:: { collections:: HashMap , io:: Write , path:: PathBuf } ;
3+ use tinyjson:: * ;
124
135pub fn get_save_path ( ) -> DynResult < PathBuf > {
146 let hiper_path = get_hiper_dir ( ) ?;
@@ -17,33 +9,67 @@ pub fn get_save_path() -> DynResult<PathBuf> {
179
1810pub fn save_config ( app_state : & AppState ) {
1911 if let Ok ( save_path) = get_save_path ( ) {
20- let config = Config {
21- auto_restart : app_state. auto_restart ,
22- use_tun : app_state. use_tun ,
23- token : app_state. inner_token . to_owned ( ) ,
24- } ;
25- if let Ok ( file) = std:: fs:: OpenOptions :: new ( )
12+ if let Ok ( mut file) = std:: fs:: OpenOptions :: new ( )
2613 . write ( true )
2714 . create ( true )
2815 . truncate ( true )
2916 . open ( save_path)
3017 {
31- let _ = serde_json:: to_writer ( file, & config) ;
18+ let mut data_hashmap = HashMap :: with_capacity ( 16 ) ;
19+
20+ data_hashmap. insert (
21+ "token" . into ( ) ,
22+ JsonValue :: String ( app_state. inner_token . to_owned ( ) ) ,
23+ ) ;
24+ data_hashmap. insert ( "use_tun" . into ( ) , JsonValue :: Boolean ( app_state. use_tun ) ) ;
25+ data_hashmap. insert (
26+ "auto_restart" . into ( ) ,
27+ JsonValue :: Boolean ( app_state. auto_restart ) ,
28+ ) ;
29+ data_hashmap. insert (
30+ "debug_mode" . into ( ) ,
31+ JsonValue :: Boolean ( app_state. debug_mode ) ,
32+ ) ;
33+
34+ let data = JsonValue :: Object ( data_hashmap) ;
35+
36+ if let Ok ( data) = data. stringify ( ) {
37+ let _ = file. write_all ( data. as_bytes ( ) ) ;
38+ let _ = file. sync_all ( ) ;
39+ }
3240 }
3341 }
3442}
3543
3644pub fn load_config ( app_state : & mut AppState ) {
3745 if let Ok ( save_path) = get_save_path ( ) {
3846 if save_path. exists ( ) {
39- if let Ok ( file) = std:: fs:: read ( save_path) {
40- if let Ok ( data) = serde_json:: from_slice :: < Config > ( & file) {
41- if !data. token . is_empty ( ) {
42- app_state. inner_token = data. token ;
43- app_state. token = "••••••••" . into ( ) ;
47+ if let Ok ( file) = std:: fs:: read_to_string ( save_path) {
48+ if let Ok ( JsonValue :: Object ( data) ) = file. parse :: < JsonValue > ( ) {
49+ if let Some ( Some ( token) ) = data. get ( "token" ) . map ( |x| x. get :: < String > ( ) ) {
50+ if !token. is_empty ( ) {
51+ app_state. inner_token = token. to_owned ( ) ;
52+ app_state. token = "••••••••" . into ( ) ;
53+ }
54+ }
55+ if let Some ( use_tun) = data
56+ . get ( "use_tun" )
57+ . map ( |x| x. get :: < bool > ( ) . copied ( ) . unwrap_or ( false ) )
58+ {
59+ app_state. use_tun = use_tun;
60+ }
61+ if let Some ( auto_restart) = data
62+ . get ( "auto_restart" )
63+ . map ( |x| x. get :: < bool > ( ) . copied ( ) . unwrap_or ( false ) )
64+ {
65+ app_state. auto_restart = auto_restart;
66+ }
67+ if let Some ( debug_mode) = data
68+ . get ( "debug_mode" )
69+ . map ( |x| x. get :: < bool > ( ) . copied ( ) . unwrap_or ( false ) )
70+ {
71+ app_state. debug_mode = debug_mode;
4472 }
45- app_state. use_tun = data. use_tun ;
46- app_state. auto_restart = data. auto_restart ;
4773 }
4874 }
4975 }
0 commit comments