Skip to content

Commit

Permalink
Move to homedir since tilde_expand is old
Browse files Browse the repository at this point in the history
  • Loading branch information
alisomay committed Nov 30, 2024
1 parent c6db32a commit 2842060
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 26 deletions.
100 changes: 90 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions externals/rytm/rytm-external/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rytm-external"
version = "0.1.0"
authors = [ "Ali Somay <[email protected]>" ]
authors = ["Ali Somay <[email protected]>"]
edition = "2021"

[lib]
Expand All @@ -25,5 +25,4 @@ tracing-core = "0.1"
parking_lot = "0.12"
tracing-error = "0.2"
camino = "1"
tilde-expand = "0.1.1"

homedir = "0.3"
20 changes: 12 additions & 8 deletions externals/rytm/rytm-external/src/load_save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
file::{FilePathExt, RytmProjectFileType},
traits::Post,
types::{SaveTarget, SaveTargetIndex},
utils::make_utf8_path_buf_respect_tilde,
RytmExternal,
};
use camino::Utf8PathBuf;
Expand Down Expand Up @@ -59,8 +58,8 @@ impl RytmExternal {
.last()
.map(median::atom::Atom::get_symbol)
.unwrap_or_default();
let maybe_path =
make_utf8_path_buf_respect_tilde(&maybe_path_symbol.to_string().unwrap_or_default());
let maybe_path = self
.make_utf8_path_buf_respect_tilde(&maybe_path_symbol.to_string().unwrap_or_default());

let path_is_provided = !maybe_path.as_str().is_empty();
if path_is_provided {
Expand All @@ -71,8 +70,12 @@ impl RytmExternal {
debug!("Trying to load from: {}.", maybe_path);

// Check existence and file-ness.
let path_exists = make_utf8_path_buf_respect_tilde(maybe_path.as_str()).exists();
let path_is_file = make_utf8_path_buf_respect_tilde(maybe_path.as_str()).is_file();
let path_exists = self
.make_utf8_path_buf_respect_tilde(maybe_path.as_str())
.exists();
let path_is_file = self
.make_utf8_path_buf_respect_tilde(maybe_path.as_str())
.is_file();
let path = if path_exists && path_is_file {
maybe_path_symbol
} else {
Expand Down Expand Up @@ -200,7 +203,8 @@ impl RytmExternal {
Some(RytmValue::Symbol(path_or_save_target)) => {
if values.len() == 1 {
// Consider this as a file path or user didn't pass anything in, act accordingly.
let maybe_valid_path = make_utf8_path_buf_respect_tilde(path_or_save_target);
let maybe_valid_path =
self.make_utf8_path_buf_respect_tilde(path_or_save_target);
let save_file_type = if path_or_save_target.is_empty() {
RytmProjectFileType::Rytm
} else {
Expand Down Expand Up @@ -235,7 +239,7 @@ impl RytmExternal {
} else if values.len() > 1 && values.len() <= 3 {
// Check if the last argument is path like and has an extension.
if let RytmValue::Symbol(maybe_path) = values_b.next().unwrap() {
let maybe_valid_path = make_utf8_path_buf_respect_tilde(maybe_path);
let maybe_valid_path = self.make_utf8_path_buf_respect_tilde(maybe_path);
if let Some(ext) = maybe_valid_path.extension() {
if ext.parse::<RytmProjectFileType>().is_err() {
return Err(RytmExternalError::from("File Error: Invalid file type. Only .rytm or .sysex files are allowed.")).inspect_err(|err| error!("{}", err));
Expand Down Expand Up @@ -290,7 +294,7 @@ impl RytmExternal {

if let Some(RytmValue::Symbol(must_be_path)) = values_f.next() {
// Consider this as a file path or user didn't pass anything in, act accordingly.
let maybe_valid_path = make_utf8_path_buf_respect_tilde(must_be_path);
let maybe_valid_path = self.make_utf8_path_buf_respect_tilde(must_be_path);
let save_file_type = if must_be_path.is_empty() {
None
} else {
Expand Down
55 changes: 50 additions & 5 deletions externals/rytm/rytm-external/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
use camino::Utf8PathBuf;
use tilde_expand::tilde_expand;
use homedir::my_home;
use median::object::MaxObj;
use tracing::{debug, instrument, warn};

pub fn make_utf8_path_buf_respect_tilde(path_candidate: &str) -> Utf8PathBuf {
Utf8PathBuf::from(
String::from_utf8_lossy(&tilde_expand(path_candidate.as_bytes())).into_owned(),
)
use crate::{traits::Post, RytmExternal};

impl RytmExternal {
#[instrument(
skip(self),
fields(
path_candidate,
home_dir,
home_dir_str,
path_without_tilde,
expanded_path
)
)]
pub fn make_utf8_path_buf_respect_tilde(&self, path_candidate: &str) -> Utf8PathBuf {
let span = tracing::Span::current();
if path_candidate.starts_with('~') {
// Attempt to get the user's home directory
if let Some(home_dir) = my_home().ok().flatten() {
span.record("home_dir", home_dir.to_string_lossy().to_string());

if let Some(home_dir_str) = home_dir.to_str() {
span.record("home_dir_str", home_dir_str);
// Replace the leading '~' with the home directory

let path_without_tilde = path_candidate.trim_start_matches('~');
span.record("path_without_tilde", path_without_tilde);

let expanded_path = format!("{home_dir_str}{path_without_tilde}");
span.record("expanded_path", &expanded_path);

debug!("Expanded path with home directory");
return Utf8PathBuf::from(expanded_path);
}
}

let warning = "Failed to get home directory, the path will be returned as is";
warn!("{}", warning);
warning.obj_warn(self.max_obj());
self.send_status_warning();

// If we can't get the home directory, return the original path
return Utf8PathBuf::from(path_candidate);
}

debug!("Path does not start with '~', returning as is");
Utf8PathBuf::from(path_candidate)
}
}

0 comments on commit 2842060

Please sign in to comment.