jmcnamara / rust_xlsxwriter

A Rust library for creating Excel XLSX files.
https://crates.io/crates/rust_xlsxwriter
Apache License 2.0
316 stars 25 forks source link

feature request: make `Workbook::new` accept `&Path` #3

Closed Chronophylos closed 1 year ago

Chronophylos commented 1 year ago

Feature Request

Workbook::new should accept &Path or PathBuf instead of &str.

jmcnamara commented 1 year ago

Good suggestion. I'll look into it.

jmcnamara commented 1 year ago

I thought about this for a while and I'd prefer to keep new() as constructor from simple strings since that will work for most people, most of the time, and it simplifies the documentation.

On the other hand Path is better for the times when a simple string path doesn't work and in particular for folks on Windows who can encounter different hurdles with back/forward slashes and network paths.

So as a compromise I've added a new_from_path() constructor. I've pushed this to main and it works as follows:

use rust_xlsxwriter::{Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    let path = std::path::Path::new("workbook.xlsx");
    let mut workbook = Workbook::new_from_path(&path);

    _ = workbook.add_worksheet();

    workbook.close()?;

    Ok(())
}

Try it out and let me know what you think.

I'm also going to try to add a new_from_file() constructor for File type instances. However, that is trickier from a lifetime/ownership point of view.

jmcnamara commented 1 year ago

This feature is now in rust_xlsxwriter version 0.8.0: https://rustxlsxwriter.github.io/changelog.html

Thanks for the suggestion. Closing.