Skip to content

Commit

Permalink
[FEAT]: tilde expansion (#2277)
Browse files Browse the repository at this point in the history
closes #2205
  • Loading branch information
universalmind303 authored May 14, 2024
1 parent 4e2c954 commit 4160091
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions src/daft-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ common-io-config = {path = "../common/io-config", default-features = false}
daft-core = {path = "../daft-core", default-features = false}
futures = {workspace = true}
globset = "0.4"
home = "0.5.9"
hyper = "0.14.27"
hyper-tls = "0.5.0"
itertools = {workspace = true}
Expand Down
17 changes: 17 additions & 0 deletions src/daft-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub enum Error {
#[snafu(display("Invalid Argument: {:?}", msg))]
InvalidArgument { msg: String },

#[snafu(display("Unable to expand home dir"))]
HomeDirError { path: String },

#[snafu(display("Unable to open file {}: {:?}", path, source))]
UnableToOpenFile { path: String, source: DynError },

Expand Down Expand Up @@ -300,11 +303,25 @@ impl std::fmt::Display for SourceType {

pub fn parse_url(input: &str) -> Result<(SourceType, Cow<'_, str>)> {
let mut fixed_input = Cow::Borrowed(input);
// handle tilde `~` expansion
if input.starts_with("~/") {
return home::home_dir()
.and_then(|home_dir| {
let expanded = home_dir.join(&input[2..]);
let input = expanded.to_str()?;

Some((SourceType::File, Cow::Owned(format!("file://{}", input))))
})
.ok_or_else(|| crate::Error::InvalidArgument {
msg: "Could not convert expanded path to string".to_string(),
});
}

let url = match url::Url::parse(input) {
Ok(url) => Ok(url),
Err(ParseError::RelativeUrlWithoutBase) => {
fixed_input = Cow::Owned(format!("file://{input}"));

url::Url::parse(fixed_input.as_ref())
}
Err(err) => Err(err),
Expand Down

0 comments on commit 4160091

Please sign in to comment.