Skip to content

Commit 879cee7

Browse files
committed
refactor: move from PathBuf, to Path
1 parent 40c7ffd commit 879cee7

File tree

9 files changed

+64
-72
lines changed

9 files changed

+64
-72
lines changed

src/databases/sqlite.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::Path;
22
use std::str::FromStr;
33

44
use async_trait::async_trait;
@@ -17,7 +17,7 @@ use crate::tracker::key::AuthKey;
1717

1818
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1919
pub struct Sqlite3DatabaseSettings {
20-
pub database_file_path: PathBuf,
20+
pub database_file_path: Box<Path>,
2121
}
2222

2323
impl TryFrom<&DatabaseSettings> for Sqlite3DatabaseSettings {
@@ -44,7 +44,7 @@ pub struct SqliteDatabase {
4444

4545
impl SqliteDatabase {
4646
pub fn new(settings: &Sqlite3DatabaseSettings) -> Result<SqliteDatabase, r2d2::Error> {
47-
let cm = SqliteConnectionManager::file(settings.database_file_path.as_path());
47+
let cm = SqliteConnectionManager::file(&settings.database_file_path);
4848
let pool = Pool::new(cm).expect("Failed to create r2d2 SQLite connection pool.");
4949
Ok(SqliteDatabase { pool })
5050
}

src/errors/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::Path;
22
use std::sync::Arc;
33

44
use thiserror::Error;
@@ -49,8 +49,14 @@ impl Reject for ServerError {}
4949
#[derive(Error, Clone, Debug, Eq, Hash, PartialEq)]
5050
pub enum FilePathError {
5151
#[error("File Path failed to Canonicalize: {input} : {source}.")]
52-
FilePathIsUnresolvable { input: PathBuf, source: Arc<wrappers::IoError> },
52+
FilePathIsUnresolvable {
53+
input: Box<Path>,
54+
source: Arc<wrappers::IoError>,
55+
},
5356

5457
#[error("File Path destination is not a file: {input} : {source}.")]
55-
FilePathIsNotAvailable { input: PathBuf, source: Arc<wrappers::IoError> },
58+
FilePathIsNotAvailable {
59+
input: Box<Path>,
60+
source: Arc<wrappers::IoError>,
61+
},
5662
}

src/errors/settings_manager.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::Path;
22
use std::sync::Arc;
33

44
use thiserror::Error;
@@ -16,24 +16,24 @@ pub enum SettingsManagerError {
1616
FailedToCreateNewFile { source: FilePathError },
1717

1818
#[error("Unable to resolve path at: \"{at}\"!")]
19-
FailedToResolvePath { at: PathBuf, source: Arc<wrappers::IoError> },
19+
FailedToResolvePath { at: Box<Path>, source: Arc<wrappers::IoError> },
2020

2121
#[error("Unable to prepare directory at: \"{at}\" : {source}!")]
22-
FailedToPrepareDirectory { at: PathBuf, source: Arc<wrappers::IoError> },
22+
FailedToPrepareDirectory { at: Box<Path>, source: Arc<wrappers::IoError> },
2323

2424
#[error("Unable to resolve a directory at: \"{at}\"!")]
25-
FailedToResolveDirectory { at: PathBuf },
25+
FailedToResolveDirectory { at: Box<Path> },
2626

2727
#[error("Unable to read file, {message}: \"{from}\" : {source}.")]
2828
FailedToReadFromFile {
2929
message: String,
30-
from: PathBuf,
30+
from: Box<Path>,
3131
source: Box<Self>,
3232
},
3333
#[error("Unable to write file, {message}: \"{to}\": {source}.")]
3434
FailedToWriteToFile {
3535
message: String,
36-
to: PathBuf,
36+
to: Box<Path>,
3737
source: Box<Self>,
3838
},
3939

@@ -64,14 +64,14 @@ pub enum SettingsManagerError {
6464

6565
#[error("Unable to import old settings from: \"{from}\" : \"{source}\"")]
6666
FailedToImportOldSettings {
67-
from: PathBuf,
67+
from: Box<Path>,
6868
source: Box<settings::SettingsError>,
6969
},
7070

7171
#[error("Unable to successfully move file from: {from} to: {to} \"{source}\"")]
7272
FailedToMoveFile {
73-
from: PathBuf,
74-
to: PathBuf,
73+
from: Box<Path>,
74+
to: Box<Path>,
7575
source: Arc<IoError>,
7676
},
7777
}

src/helpers.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
use std::fs::{File, OpenOptions};
2-
use std::path::{Path, PathBuf};
2+
use std::path::Path;
33

44
use crate::errors::wrappers::IoError;
55
use crate::errors::FilePathError;
66

7-
pub fn get_file_at(at: &Path, mode: &OpenOptions) -> Result<(File, PathBuf), FilePathError> {
7+
pub fn get_file_at(at: &Path, mode: &OpenOptions) -> Result<(File, Box<Path>), FilePathError> {
88
let file = mode.open(at).map_err(|error| FilePathError::FilePathIsNotAvailable {
9-
input: at.to_owned(),
9+
input: at.into(),
1010
source: IoError::from(error).into(),
1111
})?;
1212

1313
let at = Path::new(at)
1414
.canonicalize()
1515
.map_err(|error| FilePathError::FilePathIsUnresolvable {
16-
input: at.to_owned(),
16+
input: at.into(),
1717
source: IoError::from(error).into(),
1818
})?;
1919

20-
Ok((file, at))
20+
Ok((file, at.into_boxed_path()))
2121
}

src/http/server.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::net::SocketAddr;
2-
use std::path::PathBuf;
2+
use std::path::Path;
33
use std::str::FromStr;
44
use std::sync::Arc;
55

@@ -62,8 +62,8 @@ pub struct TlsServiceSettings {
6262
pub enabled: bool,
6363
pub display_name: String,
6464
pub socket: SocketAddr,
65-
pub certificate_file_path: PathBuf,
66-
pub key_file_path: PathBuf,
65+
pub certificate_file_path: Box<Path>,
66+
pub key_file_path: Box<Path>,
6767
}
6868

6969
impl Default for TlsServiceSettings {
@@ -73,8 +73,8 @@ impl Default for TlsServiceSettings {
7373
enabled: false,
7474
display_name: "HTTP (default)".to_string(),
7575
socket: SocketAddr::from_str("0.0.0.0:6969").unwrap(),
76-
certificate_file_path: Default::default(),
77-
key_file_path: Default::default(),
76+
certificate_file_path: Path::new("").into(),
77+
key_file_path: Path::new("").into(),
7878
}
7979
}
8080
}
@@ -149,8 +149,8 @@ impl HttpServer {
149149
pub fn start_tls(
150150
&self,
151151
socket_addr: SocketAddr,
152-
ssl_cert_path: PathBuf,
153-
ssl_key_path: PathBuf,
152+
ssl_cert_path: &Path,
153+
ssl_key_path: &Path,
154154
) -> impl warp::Future<Output = ()> {
155155
let (_addr, server) = warp::serve(routes(self.tracker.clone()))
156156
.tls()

src/jobs/tls_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn start_tls_job(settings: &TlsServiceSettings, tracker: Arc<TorrentTracker>
1717
settings.display_name, settings.socket
1818
);
1919
http_tracker
20-
.start_tls(settings.socket, settings.certificate_file_path, settings.key_file_path)
20+
.start_tls(settings.socket, &settings.certificate_file_path, &settings.key_file_path)
2121
.await;
2222
})
2323
}

src/settings/manager.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ffi::OsString;
33
use std::fs::{self, OpenOptions};
44
use std::hash::{Hash, Hasher};
55
use std::io::{Cursor, Read, Write};
6-
use std::path::{Path, PathBuf};
6+
use std::path::Path;
77

88
use log::{info, warn};
99

@@ -78,7 +78,7 @@ impl SettingsManager {
7878
Self::write_default(default.as_path())?;
7979
let manager = Self::load(old.as_path(), local.as_path(), backup, error)?;
8080

81-
manager.save(local.as_path(), &Some(backup.to_path_buf()))?;
81+
manager.save(local.as_path(), &Some(backup.into()))?;
8282

8383
Ok(manager)
8484
}
@@ -110,7 +110,7 @@ impl SettingsManager {
110110
Ok(Default::default())
111111
}
112112

113-
pub fn save(&self, to: &Path, archive_folder: &Option<PathBuf>) -> Result<(), SettingsManagerError> {
113+
pub fn save(&self, to: &Path, archive_folder: &Option<Box<Path>>) -> Result<(), SettingsManagerError> {
114114
// lets backup the previous configuration, if we have any...
115115
let existing = get_file_at(to, OpenOptions::new().read(true)).ok();
116116

@@ -204,7 +204,7 @@ impl SettingsManager {
204204
}
205205
}
206206

207-
fn backup(&self, to: &Path, folder: PathBuf) -> Result<(), SettingsManagerError> {
207+
fn backup(&self, to: &Path, folder: &Path) -> Result<(), SettingsManagerError> {
208208
let ext = match to.extension().map(|f| f.to_os_string()) {
209209
Some(mut ext) => {
210210
ext.push(".json");
@@ -218,12 +218,12 @@ impl SettingsManager {
218218
self.write_json(data.by_ref())
219219
.map_err(|err| SettingsManagerError::FailedToWriteToFile {
220220
message: "(backup)".to_string(),
221-
to: to.to_path_buf(),
221+
to: to.into(),
222222

223223
source: err.into(),
224224
})?;
225225

226-
Self::archive(Cursor::new(data), &to.with_extension(ext), &folder)?;
226+
Self::archive(Cursor::new(data), &to.with_extension(ext), folder)?;
227227
Ok(())
228228
}
229229

@@ -233,7 +233,7 @@ impl SettingsManager {
233233
let to_folder = to_folder
234234
.canonicalize()
235235
.map_err(|err| SettingsManagerError::FailedToResolvePath {
236-
at: to_folder.to_owned(),
236+
at: to_folder.into(),
237237
source: IoError::from(err).into(),
238238
})?;
239239

@@ -248,7 +248,7 @@ impl SettingsManager {
248248
})
249249
.map_err(|err| SettingsManagerError::FailedToReadFromFile {
250250
message: "(archive, read into)".to_string(),
251-
from: from.to_owned(),
251+
from: from.into(),
252252
source: err.into(),
253253
})?;
254254

@@ -348,7 +348,7 @@ impl SettingsManager {
348348
None => OsString::from(test.to_lowercase()),
349349
};
350350

351-
broken.backup(&file.1.with_extension(ext), import_error_folder.to_owned())?;
351+
broken.backup(&file.1.with_extension(ext), import_error_folder.as_path())?;
352352
}
353353

354354
// Replace broken with default, and remove everything else.
@@ -381,7 +381,7 @@ impl SettingsManager {
381381
None => OsString::from(test.to_lowercase()),
382382
};
383383

384-
broken.backup(&file.1.with_extension(ext), import_error_folder.to_owned())?;
384+
broken.backup(&file.1.with_extension(ext), import_error_folder.as_path())?;
385385
}
386386

387387
builder = test_builder.tracker_settings.clean().into();
@@ -415,7 +415,7 @@ impl SettingsManager {
415415
None => OsString::from(test.to_lowercase()),
416416
};
417417

418-
broken.backup(&file.1.with_extension(ext), import_error_folder)?;
418+
broken.backup(&file.1.with_extension(ext), import_error_folder.as_path())?;
419419

420420
return Err(SettingsManagerError::FailedToImportOldSettings {
421421
from: file.1,
@@ -448,7 +448,7 @@ impl SettingsManager {
448448
}
449449
Err(err) => Err(SettingsManagerError::FailedToMoveFile {
450450
from: file.1,
451-
to: backup,
451+
to: backup.into_boxed_path(),
452452
source: IoError::from(err).into(),
453453
}),
454454
}
@@ -459,13 +459,13 @@ impl SettingsManager {
459459
if path.is_dir() {
460460
return Ok(());
461461
} else {
462-
return Err(SettingsManagerError::FailedToResolveDirectory { at: folder.to_owned() });
462+
return Err(SettingsManagerError::FailedToResolveDirectory { at: folder.into() });
463463
}
464464
}
465465
match fs::create_dir(folder) {
466466
Ok(_) => Ok(()),
467467
Err(err) => Err(SettingsManagerError::FailedToPrepareDirectory {
468-
at: folder.to_owned(),
468+
at: folder.into(),
469469
source: IoError::from(err).into(),
470470
}),
471471
}

src/settings/mod.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::hash_map::RandomState;
33
use std::collections::{BTreeMap, HashSet};
44
use std::fs::OpenOptions;
55
use std::net::{IpAddr, SocketAddr};
6-
use std::path::{Path, PathBuf};
6+
use std::path::Path;
77
use std::str::FromStr;
88
use std::sync::Arc;
99

@@ -407,9 +407,7 @@ impl TrackerSettingsBuilder {
407407
if let Some(val) = old_settings.db_path.as_ref() {
408408
match driver {
409409
DatabaseDriversOld::Sqlite3 => {
410-
if let Ok(path) = PathBuf::from_str(val) {
411-
self.tracker_settings.database.as_mut().unwrap().sql_lite_3_db_file_path = Some(path);
412-
}
410+
self.tracker_settings.database.as_mut().unwrap().sql_lite_3_db_file_path = Some(Path::new(val).into());
413411
}
414412
DatabaseDriversOld::MySQL => {
415413
self.tracker_settings.database.as_mut().unwrap().my_sql_connection_url = Some(val.to_owned())
@@ -700,15 +698,15 @@ impl CommonSettingsBuilder {
700698
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Debug, Clone, Hash)]
701699
pub struct DatabaseSettings {
702700
driver: Option<DatabaseDrivers>,
703-
sql_lite_3_db_file_path: Option<PathBuf>,
701+
sql_lite_3_db_file_path: Option<Box<Path>>,
704702
my_sql_connection_url: Option<String>,
705703
}
706704

707705
impl Default for DatabaseSettings {
708706
fn default() -> Self {
709707
Self {
710708
driver: Some(Default::default()),
711-
sql_lite_3_db_file_path: Some(PathBuf::from_str("data.db").unwrap()),
709+
sql_lite_3_db_file_path: Some(Path::new("data.db").into()),
712710
my_sql_connection_url: None,
713711
}
714712
}
@@ -744,11 +742,11 @@ impl DatabaseSettings {
744742
Ok(self.driver.unwrap())
745743
}
746744

747-
pub fn get_slq_lite_3_file_path(&self) -> Result<PathBuf, DatabaseSettingsError> {
748-
check_field_is_not_empty!(self.to_owned() => DatabaseSettingsError; sql_lite_3_db_file_path: PathBuf);
745+
pub fn get_slq_lite_3_file_path(&self) -> Result<Box<Path>, DatabaseSettingsError> {
746+
check_field_is_not_none!(self.to_owned() => DatabaseSettingsError; sql_lite_3_db_file_path);
749747

750748
// todo: more checks here.
751-
Ok(Path::new(self.sql_lite_3_db_file_path.as_ref().unwrap()).to_path_buf())
749+
Ok(self.sql_lite_3_db_file_path.as_deref().unwrap().into())
752750
}
753751

754752
pub fn get_my_sql_connection_url(&self) -> Result<String, DatabaseSettingsError> {
@@ -1077,8 +1075,8 @@ impl ServicesBuilder {
10771075

10781076
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Debug, Clone, Hash)]
10791077
pub struct TlsSettings {
1080-
pub certificate_file_path: Option<PathBuf>,
1081-
pub key_file_path: Option<PathBuf>,
1078+
pub certificate_file_path: Option<Box<Path>>,
1079+
pub key_file_path: Option<Box<Path>>,
10821080
}
10831081

10841082
impl Empty for TlsSettings {
@@ -1098,9 +1096,8 @@ impl TlsSettings {
10981096
Ok(())
10991097
}
11001098

1101-
pub fn get_certificate_file_path(&self) -> Result<PathBuf, TlsSettingsError> {
1102-
check_field_is_not_empty!(self.to_owned() => TlsSettingsError;
1103-
certificate_file_path: PathBuf);
1099+
pub fn get_certificate_file_path(&self) -> Result<Box<Path>, TlsSettingsError> {
1100+
check_field_is_not_none!(self.to_owned() => TlsSettingsError; certificate_file_path);
11041101

11051102
get_file_at(self.certificate_file_path.as_ref().unwrap(), OpenOptions::new().read(true))
11061103
.map(|at| at.1)
@@ -1110,9 +1107,8 @@ impl TlsSettings {
11101107
})
11111108
}
11121109

1113-
pub fn get_key_file_path(&self) -> Result<PathBuf, TlsSettingsError> {
1114-
check_field_is_not_empty!(self.to_owned() => TlsSettingsError;
1115-
key_file_path: PathBuf);
1110+
pub fn get_key_file_path(&self) -> Result<Box<Path>, TlsSettingsError> {
1111+
check_field_is_not_none!(self.to_owned() => TlsSettingsError; key_file_path);
11161112

11171113
get_file_at(self.key_file_path.as_ref().unwrap(), OpenOptions::new().read(true))
11181114
.map(|at| at.1)

0 commit comments

Comments
 (0)