@@ -124,11 +124,14 @@ impl ConfigError {
124124 /// Create a [`ConfigError`] from a [`std::io::Error`].
125125 ///
126126 /// Maps to either `NotFound` or `Io`.
127- pub fn from_io ( path : PathBuf , error : io:: Error ) -> Self {
127+ pub fn from_io ( path : impl Into < PathBuf > , error : io:: Error ) -> Self {
128128 if error. kind ( ) == io:: ErrorKind :: NotFound {
129- Self :: NotFound { path }
129+ Self :: NotFound { path : path . into ( ) }
130130 } else {
131- Self :: Io { path, error }
131+ Self :: Io {
132+ path : path. into ( ) ,
133+ error,
134+ }
132135 }
133136 }
134137
@@ -147,52 +150,58 @@ static CACHE: OnceCell<Config> = OnceCell::new();
147150/// Internal methods for loading a `Config`.
148151#[ allow( clippy:: result_large_err) ]
149152impl Config {
150- /// Get the cached config, or to read `$CARGO_MANIFEST_DIR/sqlx.toml`.
153+ /// Get the cached config, or read `$CARGO_MANIFEST_DIR/sqlx.toml`.
151154 ///
152155 /// On success, the config is cached in a `static` and returned by future calls.
153156 ///
154157 /// Errors if `CARGO_MANIFEST_DIR` is not set, or if the config file could not be read.
155- pub fn try_from_crate ( ) -> Result < & ' static Self , ConfigError > {
156- Self :: try_read_with ( get_crate_path)
158+ ///
159+ /// If the file does not exist, the cache is populated with `Config::default()`.
160+ pub fn try_from_crate_or_default ( ) -> Result < & ' static Self , ConfigError > {
161+ CACHE . get_or_try_init ( || {
162+ Self :: read_from ( get_crate_path ( ) ?) . or_else ( |e| {
163+ if let ConfigError :: NotFound { .. } = e {
164+ Ok ( Config :: default ( ) )
165+ } else {
166+ Err ( e)
167+ }
168+ } )
169+ } )
157170 }
158171
159- /// Get the cached config, or attempt to read `sqlx.toml` from the current working directory .
172+ /// Get the cached config, or read `$CARGO_MANIFEST_DIR/ sqlx.toml`.
160173 ///
161174 /// On success, the config is cached in a `static` and returned by future calls.
162175 ///
163- /// Errors if the config file does not exist, or could not be read.
164- pub fn try_from_current_dir ( ) -> Result < & ' static Self , ConfigError > {
165- Self :: try_read_with ( || Ok ( "sqlx.toml" . into ( ) ) )
176+ /// Errors if `CARGO_MANIFEST_DIR` is not set, or if the config file could not be read.
177+ ///
178+ /// If the file does not exist, the cache is populated with `Config::default()`.
179+ pub fn try_from_crate_or_default ( ) -> Result < & ' static Self , ConfigError > {
180+ CACHE . get_or_try_init ( || {
181+ Self :: read_from ( get_crate_path ( ) ?) . or_else ( |e| {
182+ if let ConfigError :: NotFound { .. } = e {
183+ Ok ( Config :: default ( ) )
184+ } else {
185+ Err ( e)
186+ }
187+ } )
188+ } )
166189 }
167190
168191 /// Get the cached config, or attempt to read it from the path given.
169192 ///
170193 /// On success, the config is cached in a `static` and returned by future calls.
171194 ///
172195 /// Errors if the config file does not exist, or could not be read.
173- pub fn try_from_path ( path : impl Into < PathBuf > ) -> Result < & ' static Self , ConfigError > {
174- Self :: try_read_with ( || Ok ( path. into ( ) ) )
196+ pub fn try_from_path ( path : PathBuf ) -> Result < & ' static Self , ConfigError > {
197+ CACHE . get_or_try_init ( || Self :: read_from ( path) )
175198 }
176199
177200 /// Get the cached config, or return the default.
178201 pub fn get_or_default ( ) -> & ' static Self {
179202 CACHE . get_or_init ( Config :: default)
180203 }
181204
182- /// Get the cached config, or attempt to read it from the path returned by the closure.
183- ///
184- /// On success, the config is cached in a `static` and returned by future calls.
185- ///
186- /// Errors if the config file does not exist, or could not be read.
187- fn try_read_with (
188- make_path : impl FnOnce ( ) -> Result < PathBuf , ConfigError > ,
189- ) -> Result < & ' static Self , ConfigError > {
190- CACHE . get_or_try_init ( || {
191- let path = make_path ( ) ?;
192- Self :: read_from ( path)
193- } )
194- }
195-
196205 #[ cfg( feature = "sqlx-toml" ) ]
197206 fn read_from ( path : PathBuf ) -> Result < Self , ConfigError > {
198207 // The `toml` crate doesn't provide an incremental reader.
@@ -216,8 +225,8 @@ impl Config {
216225 #[ cfg( not( feature = "sqlx-toml" ) ) ]
217226 fn read_from ( path : PathBuf ) -> Result < Self , ConfigError > {
218227 match path. try_exists ( ) {
219- Ok ( true ) => Err ( ConfigError :: ParseDisabled { path } ) ,
220- Ok ( false ) => Err ( ConfigError :: NotFound { path } ) ,
228+ Ok ( true ) => Err ( ConfigError :: ParseDisabled { path : path . into ( ) } ) ,
229+ Ok ( false ) => Err ( ConfigError :: NotFound { path : path . into ( ) } ) ,
221230 Err ( e) => Err ( ConfigError :: from_io ( path, e) ) ,
222231 }
223232 }
0 commit comments