1
1
use serde:: { Deserialize , Serialize } ;
2
- use std:: {
3
- fs:: File ,
4
- io:: { Error , Read , Write } ,
5
- path:: { Path , PathBuf } ,
6
- } ;
2
+ use std:: io:: Error ;
3
+
4
+ use std:: sync:: RwLock ;
5
+
6
+ // TODO relocate from app data service and remove app_data server
7
+ static HARDWARE_ID : RwLock < String > = RwLock :: new ( String :: new ( ) ) ;
8
+
9
+ // Should be called once in run_server
10
+ fn set_hardware_id ( hardware_id : String ) {
11
+ ( * HARDWARE_ID . write ( ) . unwrap ( ) ) = hardware_id;
12
+ }
13
+
14
+ fn get_hardware_id ( ) -> String {
15
+ HARDWARE_ID . read ( ) . unwrap ( ) . clone ( )
16
+ }
7
17
8
18
#[ derive( Deserialize , Serialize , Clone , Debug , PartialEq , Default ) ]
9
19
pub struct AppData {
10
20
pub site_hardware_id : String ,
11
21
}
12
22
13
- const APP_DATA_FILE : & str = "settings_app_data.yaml" ;
14
-
15
23
pub trait AppDataServiceTrait : Send + Sync {
16
- fn get_app_data_directory ( & self ) -> Result < PathBuf , Error > ;
17
- fn get_app_data_file ( & self ) -> Result < PathBuf , Error > ;
18
- fn load_from_file ( & self ) -> Result < AppData , Error > ;
19
- fn get_hardware_id ( & self ) -> Result < String , Error > ;
20
- fn set_hardware_id ( & self , hardware_id : String ) -> Result < ( ) , Error > ;
24
+ fn get_hardware_id ( & self ) -> Result < String , Error > {
25
+ Ok ( get_hardware_id ( ) )
26
+ }
27
+ fn set_hardware_id ( & self , hardware_id : String ) -> Result < ( ) , Error > {
28
+ Ok ( set_hardware_id ( hardware_id) )
29
+ }
21
30
}
22
31
23
32
pub struct AppDataService {
@@ -32,53 +41,4 @@ impl AppDataService {
32
41
}
33
42
}
34
43
35
- impl AppDataServiceTrait for AppDataService {
36
- fn get_app_data_directory ( & self ) -> Result < PathBuf , Error > {
37
- let root = Path :: new ( "./" ) ;
38
- let app_data_folder = root. join ( self . app_data_folder . clone ( ) ) ;
39
- if !app_data_folder. exists ( ) {
40
- std:: fs:: create_dir_all ( app_data_folder. clone ( ) ) ?;
41
- }
42
-
43
- Ok ( app_data_folder)
44
- }
45
-
46
- fn get_app_data_file ( & self ) -> Result < PathBuf , Error > {
47
- let app_data_directory = self . get_app_data_directory ( ) ?;
48
-
49
- Ok ( app_data_directory. join ( APP_DATA_FILE ) )
50
- }
51
-
52
- fn load_from_file ( & self ) -> Result < AppData , Error > {
53
- let file_path = self . get_app_data_file ( ) ?;
54
-
55
- if !file_path. exists ( ) {
56
- let mut file = File :: create ( & file_path) ?;
57
- file. write_all ( b"site_hardware_id: \" \" " ) ?;
58
- }
59
-
60
- let mut file = File :: open ( file_path) ?;
61
- let mut contents = String :: new ( ) ;
62
- file. read_to_string ( & mut contents) ?;
63
- let app_data: AppData = serde_yaml:: from_str ( & contents) . expect ( "Failed to parse app data" ) ;
64
-
65
- Ok ( app_data)
66
- }
67
-
68
- fn get_hardware_id ( & self ) -> Result < String , Error > {
69
- let app_data = AppDataService :: load_from_file ( self ) ?;
70
-
71
- Ok ( app_data. site_hardware_id )
72
- }
73
-
74
- fn set_hardware_id ( & self , hardware_id : String ) -> Result < ( ) , Error > {
75
- let file_path = self . get_app_data_file ( ) ?;
76
-
77
- let mut app_data = AppDataService :: load_from_file ( self ) ?;
78
- app_data. site_hardware_id = hardware_id;
79
- let mut file = File :: create ( file_path) ?;
80
- file. write_all ( serde_yaml:: to_string ( & app_data) . unwrap ( ) . as_bytes ( ) ) ?;
81
-
82
- Ok ( ( ) )
83
- }
84
- }
44
+ impl AppDataServiceTrait for AppDataService { }
0 commit comments