@@ -5,8 +5,6 @@ use std::fs;
55use std:: { collections:: HashSet , sync:: RwLock } ;
66use tracing:: info;
77
8- const CONFIG_FILE_NAME : & str = "config.json" ;
9-
108#[ derive( Serialize , Deserialize ) ]
119#[ serde( rename_all = "camelCase" ) ]
1210pub struct Config {
@@ -27,19 +25,23 @@ pub struct Config {
2725 pub opt_out : DashMap < String , bool > ,
2826 #[ serde( rename = "adminAPIKey" ) ]
2927 pub admin_api_key : Option < String > ,
28+ #[ serde( skip) ]
29+ config_path : Option < std:: path:: PathBuf > ,
3030}
3131
3232impl Config {
33- pub fn load ( ) -> anyhow:: Result < Self > {
34- let contents = fs:: read_to_string ( CONFIG_FILE_NAME )
35- . with_context ( || format ! ( "Failed to load config from {CONFIG_FILE_NAME}" ) ) ?;
36- serde_json:: from_str ( & contents) . context ( "Config deserializtion error" )
33+ pub fn load ( config_path : & std:: path:: Path ) -> anyhow:: Result < Self > {
34+ let contents = fs:: read_to_string ( config_path)
35+ . with_context ( || format ! ( "Failed to load config from {}" , config_path. display( ) ) ) ?;
36+ let mut s: Self = serde_json:: from_str ( & contents) . context ( "Config deserializtion error" ) ?;
37+ s. config_path = Some ( config_path. to_owned ( ) ) ;
38+ Ok ( s)
3739 }
3840
3941 pub fn save ( & self ) -> anyhow:: Result < ( ) > {
4042 info ! ( "Updating config" ) ;
4143 let json = serde_json:: to_string_pretty ( self ) ?;
42- fs:: write ( CONFIG_FILE_NAME , json) ?;
44+ fs:: write ( self . config_path . as_ref ( ) . expect ( "config path should always be available" ) , json) ?;
4345
4446 Ok ( ( ) )
4547 }
0 commit comments