1+ use anyhow:: bail;
12use anyhow:: Result ;
23use directories:: ProjectDirs ;
34use log:: debug;
@@ -60,7 +61,7 @@ impl SystemState {
6061 state_dir : Option < String > ,
6162 disable_state_storing : bool ,
6263 compute_pool_id : u32 ,
63- ) -> Self {
64+ ) -> Result < Self > {
6465 let default_state_dir = get_default_state_dir ( ) ;
6566 debug ! ( "Default state dir: {default_state_dir:?}" ) ;
6667 let state_path = state_dir
@@ -84,7 +85,7 @@ impl SystemState {
8485 endpoint = loaded_state. endpoint ;
8586 p2p_keypair = Some ( loaded_state. p2p_keypair ) ;
8687 } else {
87- debug ! ( "Failed to load state from {state_file:?}" ) ;
88+ bail ! ( "failed to load state from {state_file:?}" ) ;
8889 }
8990 }
9091 }
@@ -93,15 +94,15 @@ impl SystemState {
9394 p2p_keypair = Some ( p2p:: Keypair :: generate_ed25519 ( ) ) ;
9495 }
9596
96- Self {
97+ Ok ( Self {
9798 last_heartbeat : Arc :: new ( RwLock :: new ( None ) ) ,
9899 is_running : Arc :: new ( RwLock :: new ( false ) ) ,
99100 endpoint : Arc :: new ( RwLock :: new ( endpoint) ) ,
100101 state_dir_overwrite : state_path. clone ( ) ,
101102 disable_state_storing,
102103 compute_pool_id,
103104 p2p_keypair : p2p_keypair. expect ( "p2p keypair must be Some at this point" ) ,
104- }
105+ } )
105106 }
106107
107108 fn save_state ( & self , heartbeat_endpoint : Option < String > ) -> Result < ( ) > {
@@ -141,8 +142,7 @@ impl SystemState {
141142 match serde_json:: from_str ( & contents) {
142143 Ok ( state) => return Ok ( Some ( state) ) ,
143144 Err ( e) => {
144- debug ! ( "Error parsing state file: {e}" ) ;
145- return Ok ( None ) ;
145+ bail ! ( "failed to parse state file: {e}" ) ;
146146 }
147147 }
148148 }
@@ -232,7 +232,8 @@ mod tests {
232232 Some ( temp_dir. path ( ) . to_string_lossy ( ) . to_string ( ) ) ,
233233 false ,
234234 0 ,
235- ) ;
235+ )
236+ . unwrap ( ) ;
236237 let _ = state
237238 . set_running ( true , Some ( "http://localhost:8080/heartbeat" . to_string ( ) ) )
238239 . await ;
@@ -255,30 +256,33 @@ mod tests {
255256 let state_file = temp_dir. path ( ) . join ( STATE_FILENAME ) ;
256257 fs:: write ( & state_file, "invalid_toml_content" ) . expect ( "Failed to write to state file" ) ;
257258
258- let state = SystemState :: new (
259+ assert ! ( SystemState :: new(
259260 Some ( temp_dir. path( ) . to_string_lossy( ) . to_string( ) ) ,
260261 false ,
261262 0 ,
262- ) ;
263- assert ! ( !( state. is_running( ) . await ) ) ;
264- assert_eq ! ( state. get_heartbeat_endpoint( ) . await , None ) ;
263+ )
264+ . is_err( ) ) ;
265265 }
266266
267267 #[ tokio:: test]
268268 async fn test_load_state ( ) {
269+ let keypair = p2p:: Keypair :: generate_ed25519 ( ) ;
270+ let state = PersistedSystemState {
271+ endpoint : Some ( "http://localhost:8080/heartbeat" . to_string ( ) ) ,
272+ p2p_keypair : keypair,
273+ } ;
274+ let serialized = serde_json:: to_string_pretty ( & state) . unwrap ( ) ;
275+
269276 let temp_dir = setup_test_dir ( ) ;
270277 let state_file = temp_dir. path ( ) . join ( STATE_FILENAME ) ;
271- fs:: write (
272- & state_file,
273- r#"{"endpoint": "http://localhost:8080/heartbeat"}"# ,
274- )
275- . expect ( "Failed to write to state file" ) ;
278+ fs:: write ( & state_file, serialized) . unwrap ( ) ;
276279
277280 let state = SystemState :: new (
278281 Some ( temp_dir. path ( ) . to_string_lossy ( ) . to_string ( ) ) ,
279282 false ,
280283 0 ,
281- ) ;
284+ )
285+ . unwrap ( ) ;
282286 assert_eq ! (
283287 state. get_heartbeat_endpoint( ) . await ,
284288 Some ( "http://localhost:8080/heartbeat" . to_string( ) )
0 commit comments