Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust): Make parse_url work on windows with object_store #15191

Conversation

mickvangelderen
Copy link
Contributor

No description provided.

@mickvangelderen
Copy link
Contributor Author

mickvangelderen commented Mar 20, 2024

This works on windows, need to see what happens on non windows.

fn parse_path(input: &str) -> Result<Url, url::ParseError> {
    match input.split_once("://") {
        Some(("file", path)) => Url::options()
            .base_url(
                Path::new(path)
                    .is_relative()
                    .then(|| Url::from_file_path(std::env::current_dir().unwrap()).unwrap())
                    .as_ref(),
            )
            .parse(path),
        Some((_, _)) => Url::parse(input),
        None => {
            let path = Path::new(input);
            let mut tmp;
            Ok(Url::from_file_path(if path.is_relative() {
                tmp = std::env::current_dir().unwrap();
                tmp.push(path);
                tmp.as_path()
            } else {
                path
            })
            .unwrap())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_path() {
        assert_eq!(
            parse_path(r"http://Users/Jane Doe/data.csv")
                .unwrap()
                .as_str(),
            "http://users/Jane%20Doe/data.csv"
        );
        assert_eq!(
            parse_path(r"http://Users/Jane Doe/data.csv")
                .unwrap()
                .as_str(),
            "http://users/Jane%20Doe/data.csv"
        );
        assert_eq!(
            parse_path(r"file:///c:/Users/Jane Doe/data.csv")
                .unwrap()
                .as_str(),
            "file:///c:/Users/Jane%20Doe/data.csv"
        );
        assert_eq!(
            parse_path(r"file://\c:\Users\Jane Doe\data.csv")
                .unwrap()
                .as_str(),
            "file:///c:/Users/Jane%20Doe/data.csv"
        );
        assert_eq!(
            parse_path(r"c:\Users\Jane Doe\data.csv").unwrap().as_str(),
            "file:///C:/Users/Jane%20Doe/data.csv"
        );
        assert_eq!(
            parse_path(r"\Users\Jane Doe\data.csv").unwrap().as_str(),
            Url::from_file_path(std::env::current_dir().unwrap())
                .unwrap()
                .join("/Users/Jane%20Doe/data.csv")
                .unwrap()
                .as_str()
        );
    }
}

@ritchie46 ritchie46 merged commit efcb8d3 into pola-rs:main Mar 21, 2024
23 checks passed
@mickvangelderen mickvangelderen deleted the make-parse-url-work-on-windows-with-object-store branch March 21, 2024 12:14
@c-peters c-peters added the accepted Ready for implementation label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation fix Bug fix rust Related to Rust Polars
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants