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
Add basename, extension, filename, parent #456
Conversation
They're not short, and they're not pretty, but they seem to work, they avoid mutability, and they should handle invalid paths without panicking. Trim of trailing slashes based on answers in issue #441. Implemented them like this for uniformity. Hopefully, if you need to change something, you won't have to do more work than if you just wrote these methods from scratch. Sorry for the lengthiness. I'm not very experienced with Rust, so maybe there are tricks I don't know, that could save a few lines.
The Rust standard library ships abstractions for this purpose, to handle paths in a cross-platform manner. use std::path::Path;
fn main() {
let path = "/grand_parent/parent/filename.ext";
let basename = Path::new(path).file_name()
.and_then(|os_str| os_str.to_str())
.unwrap_or(path);
let extension = Path::new(path).extension()
.and_then(|os_str| os_str.to_str())
.unwrap_or(path);
let file_stem = Path::new(path).file_stem()
.and_then(|os_str| os_str.to_str())
.unwrap_or(path);
let parent = Path::new(path).parent()
.and_then(|os_str| os_str.to_str())
.unwrap_or(path);
println!("basename: {}\nextension: {}\nfile_stem: {}\nparent: {}", basename, extension, file_stem, parent);
} https://play.rust-lang.org/?gist=1e6605eb6806c41cefaf1949aff214aa&version=stable You might want to use those instead. |
Absolutely. Lemme do this one over. |
No need to close the PR. You can perform a |
Ok, looks like I managed to update the branch with the new methods, but there are some other changes that I don't know why happened. |
Thanks for the work! |
No, thank you. In the end I didn't really do anything of value to the project, since you gave me the solution, but I did learn! |
@pithonsmear I wouldn't say that what you did wasn't valuable to the project. Gaining experience with Rust, and familiarity with the Ion codebase, gives value to the project in itself -- especially if you continue to submit PRs. Plus, you did the work in integrating the logic needed to get this pushed through, and that's what matters. |
Problem: Missing string methods
Solution: Add some of them
Changes introduced by this pull request: