Skip to content

Commit 540f3d3

Browse files
committed
fix clippy warnings
1 parent 48d5bec commit 540f3d3

File tree

7 files changed

+182
-106
lines changed

7 files changed

+182
-106
lines changed

src/app_config.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,23 @@ impl AppConfig {
3434
config_file
3535
));
3636
}
37-
log::debug!("Loading configuration from file: {config_file:?}");
37+
log::debug!("Loading configuration from file: {}", config_file.display());
3838
load_from_file(config_file)?
3939
} else if let Some(config_dir) = &cli.config_dir {
40-
log::debug!("Loading configuration from directory: {config_dir:?}");
40+
log::debug!(
41+
"Loading configuration from directory: {}",
42+
config_dir.display()
43+
);
4144
load_from_directory(config_dir)?
4245
} else {
4346
log::debug!("Loading configuration from environment");
4447
load_from_env()?
4548
};
4649
if let Some(web_root) = &cli.web_root {
47-
log::debug!("Setting web root to value from the command line: {web_root:?}");
50+
log::debug!(
51+
"Setting web root to value from the command line: {}",
52+
web_root.display()
53+
);
4854
config.web_root.clone_from(web_root);
4955
}
5056
if let Some(config_dir) = &cli.config_dir {
@@ -56,8 +62,8 @@ impl AppConfig {
5662

5763
if !config.configuration_directory.exists() {
5864
log::info!(
59-
"Configuration directory does not exist, creating it: {:?}",
60-
config.configuration_directory
65+
"Configuration directory does not exist, creating it: {}",
66+
config.configuration_directory.display()
6167
);
6268
std::fs::create_dir_all(&config.configuration_directory).with_context(|| {
6369
format!(
@@ -80,6 +86,10 @@ impl AppConfig {
8086
.context("The provided configuration is invalid")?;
8187

8288
log::debug!("Loaded configuration: {config:#?}");
89+
log::info!(
90+
"Configuration loaded from {}",
91+
config.configuration_directory.display()
92+
);
8393

8494
Ok(config)
8595
}
@@ -302,7 +312,7 @@ fn cannonicalize_if_possible(path: &std::path::Path) -> PathBuf {
302312
/// This should be called only once at the start of the program.
303313
pub fn load_from_directory(directory: &Path) -> anyhow::Result<AppConfig> {
304314
let cannonical = cannonicalize_if_possible(directory);
305-
log::debug!("Loading configuration from {cannonical:?}");
315+
log::debug!("Loading configuration from {}", cannonical.display());
306316
let config_file = directory.join("sqlpage");
307317
let mut app_config = load_from_file(&config_file)?;
308318
app_config.configuration_directory = directory.into();
@@ -311,7 +321,7 @@ pub fn load_from_directory(directory: &Path) -> anyhow::Result<AppConfig> {
311321

312322
/// Parses and loads the configuration from the given file.
313323
pub fn load_from_file(config_file: &Path) -> anyhow::Result<AppConfig> {
314-
log::debug!("Loading configuration from file: {config_file:?}");
324+
log::debug!("Loading configuration from file: {}", config_file.display());
315325
let config = Config::builder()
316326
.add_source(config::File::from(config_file).required(false))
317327
.add_source(env_config())
@@ -447,7 +457,7 @@ fn create_default_database(configuration_directory: &Path) -> String {
447457
}
448458
}
449459

450-
log::warn!("No DATABASE_URL provided, and {configuration_directory:?} is not writeable. Using a temporary in-memory SQLite database. All the data created will be lost when this server shuts down.");
460+
log::warn!("No DATABASE_URL provided, and {} is not writeable. Using a temporary in-memory SQLite database. All the data created will be lost when this server shuts down.", configuration_directory.display());
451461
prefix + ":memory:?cache=shared"
452462
}
453463

src/file_cache.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<T: AsyncFromStrWithState> FileCache<T> {
9898

9999
/// Adds a static file to the cache so that it will never be looked up from the disk
100100
pub fn add_static(&mut self, path: PathBuf, contents: T) {
101-
log::trace!("Adding static file {path:?} to the cache.");
101+
log::trace!("Adding static file {} to the cache.", path.display());
102102
self.static_files.insert(path, Cached::new(contents));
103103
}
104104

@@ -117,10 +117,13 @@ impl<T: AsyncFromStrWithState> FileCache<T> {
117117
path: &Path,
118118
privileged: bool,
119119
) -> anyhow::Result<Arc<T>> {
120-
log::trace!("Attempting to get from cache {path:?}");
120+
log::trace!("Attempting to get from cache {}", path.display());
121121
if let Some(cached) = self.cache.read().await.get(path) {
122122
if app_state.config.environment.is_prod() && !cached.needs_check() {
123-
log::trace!("Cache answer without filesystem lookup for {path:?}");
123+
log::trace!(
124+
"Cache answer without filesystem lookup for {}",
125+
path.display()
126+
);
124127
return Ok(Arc::clone(&cached.content));
125128
}
126129
match app_state
@@ -129,16 +132,23 @@ impl<T: AsyncFromStrWithState> FileCache<T> {
129132
.await
130133
{
131134
Ok(false) => {
132-
log::trace!("Cache answer with filesystem metadata read for {path:?}");
135+
log::trace!(
136+
"Cache answer with filesystem metadata read for {}",
137+
path.display()
138+
);
133139
cached.update_check_time();
134140
return Ok(Arc::clone(&cached.content));
135141
}
136-
Ok(true) => log::trace!("{path:?} was changed, updating cache..."),
137-
Err(e) => log::trace!("Cannot read metadata of {path:?}, re-loading it: {e:#}"),
142+
Ok(true) => log::trace!("{} was changed, updating cache...", path.display()),
143+
Err(e) => log::trace!(
144+
"Cannot read metadata of {}, re-loading it: {:#}",
145+
path.display(),
146+
e
147+
),
138148
}
139149
}
140150
// Read lock is released
141-
log::trace!("Loading and parsing {path:?}");
151+
log::trace!("Loading and parsing {}", path.display());
142152
let file_contents = app_state
143153
.file_system
144154
.read_to_string(app_state, path, privileged)
@@ -157,32 +167,39 @@ impl<T: AsyncFromStrWithState> FileCache<T> {
157167
}) =>
158168
{
159169
if let Some(static_file) = self.static_files.get(path) {
160-
log::trace!("File {path:?} not found, loading it from static files instead.");
170+
log::trace!(
171+
"File {} not found, loading it from static files instead.",
172+
path.display()
173+
);
161174
let cached: Cached<T> = static_file.make_fresh();
162175
Ok(cached)
163176
} else {
164-
Err(e).with_context(|| format!("Couldn't load {path:?} into cache"))
177+
Err(e).with_context(|| format!("Couldn't load {} into cache", path.display()))
165178
}
166179
}
167-
Err(e) => Err(e).with_context(|| format!("Couldn't load {path:?} into cache")),
180+
Err(e) => {
181+
Err(e).with_context(|| format!("Couldn't load {} into cache", path.display()))
182+
}
168183
};
169184

170185
match parsed {
171186
Ok(value) => {
172187
let new_val = Arc::clone(&value.content);
173-
log::trace!("Writing to cache {path:?}");
188+
log::trace!("Writing to cache {}", path.display());
174189
self.cache.write().await.insert(PathBuf::from(path), value);
175-
log::trace!("Done writing to cache {path:?}");
176-
log::trace!("{path:?} loaded in cache");
190+
log::trace!("Done writing to cache {}", path.display());
191+
log::trace!("{} loaded in cache", path.display());
177192
Ok(new_val)
178193
}
179194
Err(e) => {
180195
log::trace!(
181-
"Evicting {path:?} from the cache because the following error occurred: {e}"
196+
"Evicting {} from the cache because the following error occurred: {}",
197+
path.display(),
198+
e
182199
);
183-
log::trace!("Removing from cache {path:?}");
200+
log::trace!("Removing from cache {}", path.display());
184201
self.cache.write().await.remove(path);
185-
log::trace!("Done removing from cache {path:?}");
202+
log::trace!("Done removing from cache {}", path.display());
186203
Err(e)
187204
}
188205
}

src/filesystem.rs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ impl FileSystem {
5151
.file_modified_since_in_db(app_state, path, since)
5252
.await
5353
}
54-
(Err(e), _) => {
55-
Err(e).with_context(|| format!("Unable to read local file metadata for {path:?}"))
56-
}
54+
(Err(e), _) => Err(e).with_context(|| {
55+
format!("Unable to read local file metadata for {}", path.display())
56+
}),
5757
}
5858
}
5959

@@ -64,8 +64,12 @@ impl FileSystem {
6464
priviledged: bool,
6565
) -> anyhow::Result<String> {
6666
let bytes = self.read_file(app_state, path, priviledged).await?;
67-
String::from_utf8(bytes)
68-
.with_context(|| format!("The file at {path:?} contains invalid UTF8 characters"))
67+
String::from_utf8(bytes).with_context(|| {
68+
format!(
69+
"The file at {} contains invalid UTF8 characters",
70+
path.display()
71+
)
72+
})
6973
}
7074

7175
/**
@@ -78,7 +82,11 @@ impl FileSystem {
7882
priviledged: bool,
7983
) -> anyhow::Result<Vec<u8>> {
8084
let local_path = self.safe_local_path(app_state, path, priviledged)?;
81-
log::debug!("Reading file {path:?} from {local_path:?}");
85+
log::debug!(
86+
"Reading file {} from {}",
87+
path.display(),
88+
local_path.display()
89+
);
8290
let local_result = tokio::fs::read(&local_path).await;
8391
match (local_result, &self.db_fs_queries) {
8492
(Ok(f), _) => Ok(f),
@@ -90,7 +98,9 @@ impl FileSystem {
9098
status: actix_web::http::StatusCode::NOT_FOUND,
9199
}
92100
.into()),
93-
(Err(e), _) => Err(e).with_context(|| format!("Unable to read local file {path:?}")),
101+
(Err(e), _) => {
102+
Err(e).with_context(|| format!("Unable to read local file {}", path.display()))
103+
}
94104
}
95105
}
96106

@@ -104,14 +114,16 @@ impl FileSystem {
104114
// Templates requests are always made to the static TEMPLATES_DIR, because this is where they are stored in the database
105115
// but when serving them from the filesystem, we need to serve them from the `SQLPAGE_CONFIGURATION_DIRECTORY/templates` directory
106116
if let Ok(template_path) = path.strip_prefix(TEMPLATES_DIR) {
107-
let normalized = [
108-
&app_state.config.configuration_directory,
109-
Path::new("templates"),
110-
template_path,
111-
]
112-
.iter()
113-
.collect();
114-
log::trace!("Normalizing template path {path:?} to {normalized:?}");
117+
let normalized = app_state
118+
.config
119+
.configuration_directory
120+
.join("templates")
121+
.join(template_path);
122+
log::trace!(
123+
"Normalizing template path {} to {}",
124+
path.display(),
125+
normalized.display()
126+
);
115127
return Ok(normalized);
116128
}
117129
} else {
@@ -149,7 +161,10 @@ impl FileSystem {
149161

150162
// If not in local fs and we have db_fs, check database
151163
if !local_exists {
152-
log::debug!("File {path:?} not found in local filesystem, checking database");
164+
log::debug!(
165+
"File {} not found in local filesystem, checking database",
166+
path.display()
167+
);
153168
if let Some(db_fs) = &self.db_fs_queries {
154169
return db_fs.file_exists(app_state, path).await;
155170
}
@@ -245,9 +260,11 @@ impl DbFsQueries {
245260
.bind(since)
246261
.bind(path.display().to_string());
247262
log::trace!(
248-
"Checking if file {path:?} was modified since {since} by executing query: \n\
263+
"Checking if file {} was modified since {} by executing query: \n\
249264
{}\n\
250265
with parameters: {:?}",
266+
path.display(),
267+
since,
251268
self.was_modified.sql(),
252269
(since, path)
253270
);
@@ -256,7 +273,10 @@ impl DbFsQueries {
256273
.await
257274
.map(|modified| modified == Some((1,)))
258275
.with_context(|| {
259-
format!("Unable to check when {path:?} was last modified in the database")
276+
format!(
277+
"Unable to check when {} was last modified in the database",
278+
path.display()
279+
)
260280
})
261281
}
262282

@@ -278,7 +298,7 @@ impl DbFsQueries {
278298
.into())
279299
}
280300
})
281-
.with_context(|| format!("Unable to read {path:?} from the database"))
301+
.with_context(|| format!("Unable to read {} from the database", path.display()))
282302
}
283303

284304
async fn file_exists(&self, app_state: &AppState, path: &Path) -> anyhow::Result<bool> {
@@ -287,17 +307,21 @@ impl DbFsQueries {
287307
.query_as::<(i32,)>()
288308
.bind(path.display().to_string());
289309
log::trace!(
290-
"Checking if file {path:?} exists by executing query: \n\
310+
"Checking if file {} exists by executing query: \n\
291311
{}\n\
292312
with parameters: {:?}",
313+
path.display(),
293314
self.exists.sql(),
294315
(path,)
295316
);
296317
let result = query.fetch_optional(&app_state.db.connection).await;
297318
log::debug!("DB File exists result: {result:?}");
298-
result
299-
.map(|result| result.is_some())
300-
.with_context(|| format!("Unable to check if {path:?} exists in the database"))
319+
result.map(|result| result.is_some()).with_context(|| {
320+
format!(
321+
"Unable to check if {} exists in the database",
322+
path.display()
323+
)
324+
})
301325
}
302326
}
303327

src/webserver/database/connect.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,24 @@ impl Database {
103103
fn add_on_return_to_pool(config: &AppConfig, pool_options: PoolOptions<Any>) -> PoolOptions<Any> {
104104
let on_disconnect_file = config.configuration_directory.join(ON_RESET_FILE);
105105
if !on_disconnect_file.exists() {
106-
log::debug!("Not creating a custom SQL connection cleanup handler because {on_disconnect_file:?} does not exist");
106+
log::debug!(
107+
"Not creating a custom SQL connection cleanup handler because {} does not exist",
108+
on_disconnect_file.display()
109+
);
107110
return pool_options;
108111
}
109-
log::info!("Creating a custom SQL connection cleanup handler from {on_disconnect_file:?}");
112+
log::info!(
113+
"Creating a custom SQL connection cleanup handler from {}",
114+
on_disconnect_file.display()
115+
);
110116
let sql = match std::fs::read_to_string(&on_disconnect_file) {
111117
Ok(sql) => std::sync::Arc::new(sql),
112118
Err(e) => {
113-
log::error!("Unable to read the file {on_disconnect_file:?}: {e}");
119+
log::error!(
120+
"Unable to read the file {}: {}",
121+
on_disconnect_file.display(),
122+
e
123+
);
114124
return pool_options;
115125
}
116126
};
@@ -146,20 +156,30 @@ fn add_on_connection_handler(
146156
) -> PoolOptions<Any> {
147157
let on_connect_file = config.configuration_directory.join(ON_CONNECT_FILE);
148158
if !on_connect_file.exists() {
149-
log::debug!("Not creating a custom SQL database connection handler because {on_connect_file:?} does not exist");
159+
log::debug!(
160+
"Not creating a custom SQL database connection handler because {} does not exist",
161+
on_connect_file.display()
162+
);
150163
return pool_options;
151164
}
152-
log::info!("Creating a custom SQL database connection handler from {on_connect_file:?}");
165+
log::info!(
166+
"Creating a custom SQL database connection handler from {}",
167+
on_connect_file.display()
168+
);
153169
let sql = match std::fs::read_to_string(&on_connect_file) {
154170
Ok(sql) => std::sync::Arc::new(sql),
155171
Err(e) => {
156-
log::error!("Unable to read the file {on_connect_file:?}: {e}");
172+
log::error!(
173+
"Unable to read the file {}: {}",
174+
on_connect_file.display(),
175+
e
176+
);
157177
return pool_options;
158178
}
159179
};
160180
log::trace!("The custom SQL database connection handler is:\n{sql}");
161181
pool_options.after_connect(move |conn, _metadata| {
162-
log::debug!("Running {on_connect_file:?} on new connection");
182+
log::debug!("Running {} on new connection", on_connect_file.display());
163183
let sql = std::sync::Arc::clone(&sql);
164184
Box::pin(async move {
165185
let r = conn.execute(sql.as_str()).await?;

0 commit comments

Comments
 (0)