Skip to content

Commit

Permalink
Clean up some StrictPath functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Dec 17, 2024
1 parent f14612a commit 7454d96
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ impl Rclone {
fn run(&self, args: &[String], success: &[i32], privacy: Privacy) -> Result<CommandOutput, CommandError> {
let args = self.args(args);
let args: Vec<_> = args.iter().map(|x| x.as_str()).collect();
run_command(&self.app.path.raw(), &args, success, privacy)
run_command(self.app.path.raw(), &args, success, privacy)
}

fn obscure(&self, credential: &str) -> Result<String, CommandError> {
Expand Down Expand Up @@ -674,7 +674,7 @@ impl Rclone {
}
}

RcloneProcess::launch(self.app.path.raw(), self.args(&args))
RcloneProcess::launch(self.app.path.raw().into(), self.args(&args))
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2152,18 +2152,18 @@ impl App {
Message::SelectedFile(subject, path) => {
match subject {
BrowseFileSubject::RcloneExecutable => {
self.text_histories.rclone_executable.push(&path.raw());
self.text_histories.rclone_executable.push(path.raw());
self.config.apps.rclone.path = path;
}
BrowseFileSubject::RootLutrisDatabase(i) => {
self.text_histories.roots[i].lutris_database.push(&path.raw());
self.text_histories.roots[i].lutris_database.push(path.raw());
if let Root::Lutris(root) = &mut self.config.roots[i] {
root.database = Some(path);
}
}
BrowseFileSubject::SecondaryManifest(i) => {
self.text_histories.secondary_manifests[i].push(&path.raw());
self.config.manifest.secondary[i].set(path.raw());
self.text_histories.secondary_manifests[i].push(path.raw());
self.config.manifest.secondary[i].set(path.raw().into());
}
}
self.save_config();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/shortcuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl TextHistory {

pub fn path(initial: &StrictPath) -> Self {
let mut history = VecDeque::<String>::new();
history.push_back(initial.raw());
history.push_back(initial.raw().into());
Self {
history,
limit: 100,
Expand Down
66 changes: 29 additions & 37 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,18 @@ impl std::fmt::Debug for StrictPath {
}

impl StrictPath {
pub fn new(raw: String) -> Self {
pub fn new(raw: impl Into<String>) -> Self {
Self {
raw,
raw: raw.into(),
basis: None,
canonical: Arc::new(Mutex::new(None)),
}
}

pub fn relative(raw: String, basis: Option<String>) -> Self {
pub fn relative(raw: impl Into<String>, basis: Option<impl Into<String>>) -> Self {
Self {
raw,
basis,
raw: raw.into(),
basis: basis.map(|x| x.into()),
canonical: Arc::new(Mutex::new(None)),
}
}
Expand Down Expand Up @@ -198,8 +198,8 @@ impl StrictPath {
})?))
}

pub fn raw(&self) -> String {
self.raw.to_string()
pub fn raw(&self) -> &str {
&self.raw
}

/// For any paths that we store the entire time the GUI is running, like in the config,
Expand Down Expand Up @@ -463,7 +463,7 @@ impl StrictPath {
if let Ok(access) = self.access() {
access
} else {
self.raw()
self.raw().into()
}
}

Expand Down Expand Up @@ -660,20 +660,14 @@ impl StrictPath {
.map(|x| x.to_string_lossy().to_string())
}

// TODO: Refactor to use `popped()`?
pub fn parent(&self) -> Option<Self> {
self.as_std_path_buf().ok()?.parent().map(Self::from)
let popped = self.popped();
(self != &popped).then_some(popped)
}

// TODO: Refactor to use `popped()`?
pub fn parent_if_file(&self) -> Result<Self, StrictPathError> {
let resolved = self.try_resolve()?;
let pathbuf = std::path::PathBuf::from(&resolved);
if pathbuf.is_file() {
match pathbuf.parent() {
Some(parent) => Ok(Self::from(parent)),
None => Ok(self.clone()),
}
if self.is_file() {
Ok(self.popped())
} else {
Ok(self.clone())
}
Expand Down Expand Up @@ -1027,7 +1021,7 @@ impl<'de> serde::Deserialize<'de> for StrictPath {
where
D: serde::Deserializer<'de>,
{
serde::Deserialize::deserialize(deserializer).map(StrictPath::new)
serde::Deserialize::deserialize(deserializer).map(|raw: String| StrictPath::new(raw))
}
}

Expand Down Expand Up @@ -1170,52 +1164,50 @@ mod tests {
fn can_replace() {
// Identical
assert_eq!(
StrictPath::new("/foo".into()),
StrictPath::new("/foo".into())
.replace(&StrictPath::new("/foo".into()), &StrictPath::new("/foo".into())),
StrictPath::new("/foo"),
StrictPath::new("/foo").replace(&StrictPath::new("/foo"), &StrictPath::new("/foo")),
);

// Match
assert_eq!(
StrictPath::new("/baz/bar".into()),
StrictPath::new("/foo/bar".into())
.replace(&StrictPath::new("/foo".into()), &StrictPath::new("/baz".into())),
StrictPath::new("/baz/bar"),
StrictPath::new("/foo/bar").replace(&StrictPath::new("/foo"), &StrictPath::new("/baz")),
);

// Mismatch
assert_eq!(
StrictPath::new("/a".into()),
StrictPath::new("/a".into()).replace(&StrictPath::new("/ab".into()), &StrictPath::new("/ac".into())),
StrictPath::new("/a"),
StrictPath::new("/a").replace(&StrictPath::new("/ab"), &StrictPath::new("/ac")),
);

// Linux to Windows
assert_eq!(
StrictPath::new("C:/foo".into()),
StrictPath::new("/foo".into()).replace(&StrictPath::new("/".into()), &StrictPath::new("C:".into())),
StrictPath::new("C:/foo"),
StrictPath::new("/foo").replace(&StrictPath::new("/"), &StrictPath::new("C:")),
);

// Windows to Linux
assert_eq!(
StrictPath::new("/foo".into()),
StrictPath::new("C:/foo".into()).replace(&StrictPath::new("C:/".into()), &StrictPath::new("/".into())),
StrictPath::new("/foo"),
StrictPath::new("C:/foo").replace(&StrictPath::new("C:/"), &StrictPath::new("/")),
);

// Empty - original
assert_eq!(
StrictPath::new("".into()),
StrictPath::new("".into()).replace(&StrictPath::new("/foo".into()), &StrictPath::new("/bar".into())),
StrictPath::new(""),
StrictPath::new("").replace(&StrictPath::new("/foo"), &StrictPath::new("/bar")),
);

// Empty - find
assert_eq!(
StrictPath::new("/foo".into()),
StrictPath::new("/foo".into()).replace(&StrictPath::new("".into()), &StrictPath::new("/bar".into())),
StrictPath::new("/foo"),
StrictPath::new("/foo").replace(&StrictPath::new(""), &StrictPath::new("/bar")),
);

// Empty - new
assert_eq!(
StrictPath::new("/foo".into()),
StrictPath::new("/foo".into()).replace(&StrictPath::new("/foo".into()), &StrictPath::new("".into())),
StrictPath::new("/foo"),
StrictPath::new("/foo").replace(&StrictPath::new("/foo"), &StrictPath::new("")),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/resource/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl SecondaryManifestConfig {

pub fn value(&self) -> String {
match self {
Self::Local { path, .. } => path.raw(),
Self::Local { path, .. } => path.raw().into(),
Self::Remote { url, .. } => url.to_string(),
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ impl SecondaryManifestConfig {
match (&self, kind) {
(Self::Local { path, enable }, SecondaryManifestConfigKind::Remote) => {
*self = Self::Remote {
url: path.raw(),
url: path.raw().into(),
enable: *enable,
};
}
Expand Down
56 changes: 28 additions & 28 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,29 +998,29 @@ mod tests {
// No redirects
assert_eq!(
None,
game_file_target(&StrictPath::new("/foo".into()), &[], false, ScanKind::Backup)
game_file_target(&StrictPath::new("/foo"), &[], false, ScanKind::Backup)
);

// Match - backup
assert_eq!(
Some(StrictPath::new("/quux".into())),
Some(StrictPath::new("/quux")),
game_file_target(
&StrictPath::new("/foo".into()),
&StrictPath::new("/foo"),
&[
RedirectConfig {
kind: RedirectKind::Backup,
source: StrictPath::new("/foo".into()),
target: StrictPath::new("/bar".into()),
source: StrictPath::new("/foo"),
target: StrictPath::new("/bar"),
},
RedirectConfig {
kind: RedirectKind::Restore,
source: StrictPath::new("/bar".into()),
target: StrictPath::new("/baz".into()),
source: StrictPath::new("/bar"),
target: StrictPath::new("/baz"),
},
RedirectConfig {
kind: RedirectKind::Bidirectional,
source: StrictPath::new("/bar".into()),
target: StrictPath::new("/quux".into()),
source: StrictPath::new("/bar"),
target: StrictPath::new("/quux"),
},
],
false,
Expand All @@ -1030,24 +1030,24 @@ mod tests {

// Match - restore
assert_eq!(
Some(StrictPath::new("/foo".into())),
Some(StrictPath::new("/foo")),
game_file_target(
&StrictPath::new("/quux".into()),
&StrictPath::new("/quux"),
&[
RedirectConfig {
kind: RedirectKind::Bidirectional,
source: StrictPath::new("/bar".into()),
target: StrictPath::new("/quux".into()),
source: StrictPath::new("/bar"),
target: StrictPath::new("/quux"),
},
RedirectConfig {
kind: RedirectKind::Restore,
source: StrictPath::new("/bar".into()),
target: StrictPath::new("/foo".into()),
source: StrictPath::new("/bar"),
target: StrictPath::new("/foo"),
},
RedirectConfig {
kind: RedirectKind::Backup,
source: StrictPath::new("/foo".into()),
target: StrictPath::new("/baz".into()),
source: StrictPath::new("/foo"),
target: StrictPath::new("/baz"),
},
],
false,
Expand All @@ -1057,24 +1057,24 @@ mod tests {

// Match - restore, reversed
assert_eq!(
Some(StrictPath::new("/bar".into())),
Some(StrictPath::new("/bar")),
game_file_target(
&StrictPath::new("/quux".into()),
&StrictPath::new("/quux"),
&[
RedirectConfig {
kind: RedirectKind::Bidirectional,
source: StrictPath::new("/bar".into()),
target: StrictPath::new("/quux".into()),
source: StrictPath::new("/bar"),
target: StrictPath::new("/quux"),
},
RedirectConfig {
kind: RedirectKind::Restore,
source: StrictPath::new("/bar".into()),
target: StrictPath::new("/foo".into()),
source: StrictPath::new("/bar"),
target: StrictPath::new("/foo"),
},
RedirectConfig {
kind: RedirectKind::Backup,
source: StrictPath::new("/foo".into()),
target: StrictPath::new("/baz".into()),
source: StrictPath::new("/foo"),
target: StrictPath::new("/baz"),
},
],
true,
Expand All @@ -1086,11 +1086,11 @@ mod tests {
assert_eq!(
None,
game_file_target(
&StrictPath::new("/foo".into()),
&StrictPath::new("/foo"),
&[RedirectConfig {
kind: RedirectKind::Backup,
source: StrictPath::new("/f".into()),
target: StrictPath::new("/b".into()),
source: StrictPath::new("/f"),
target: StrictPath::new("/b"),
},],
false,
ScanKind::Backup,
Expand Down
4 changes: 2 additions & 2 deletions src/scan/launchers/lutris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ fn scan_spec(spec: spec::Data, spec_path: &StrictPath) -> Option<Pending> {
let exe = if exe.is_absolute() {
exe
} else if let Some(prefix) = pending.prefix.as_ref() {
prefix.joined(&exe.raw())
prefix.joined(exe.raw())
} else {
log::info!("Lutris game file has relative exe and no prefix: {:?}", spec_path);
break 'wd;
Expand Down Expand Up @@ -467,7 +467,7 @@ mod tests {
fn can_scan_spec_with_relative_exe_but_prefix() {
let spec = spec::Data {
game: spec::Game {
exe: Some(StrictPath::new("drive_c/game/launcher.exe".into())),
exe: Some(StrictPath::new("drive_c/game/launcher.exe")),
prefix: Some(absolute_path("/prefix")),
working_dir: None,
},
Expand Down
Loading

0 comments on commit 7454d96

Please sign in to comment.