openprxl 0.1.0

A Rust spreadsheet library inspired by Python's openpyxl
Documentation
//! Serialization of `xl/workbook.xml`.

use crate::error::Result;
use crate::workbook::Workbook;
use crate::xml;
use quick_xml::events::{BytesEnd, BytesStart, Event};

pub fn write<W: std::io::Write>(sink: W, wb: &Workbook) -> Result<()> {
    let mut writer = xml::writer(sink);

    let mut root = BytesStart::new("workbook");
    root.push_attribute(("xmlns", xml::NS_MAIN));
    root.push_attribute(("xmlns:r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"));
    writer.write_event(Event::Start(root))?;

    writer.write_event(Event::Start(BytesStart::new("sheets")))?;
    for (idx, sheet) in wb.sheets.iter().enumerate() {
        let mut elem = BytesStart::new("sheet");
        elem.push_attribute(("name", sheet.name.as_str()));
        elem.push_attribute(("sheetId", sheet.sheet_id.to_string().as_str()));
        elem.push_attribute(("r:id", format!("rId{}", idx + 1).as_str()));
        writer.write_event(Event::Empty(elem))?;
    }
    writer.write_event(Event::End(BytesEnd::new("sheets")))?;

    if wb.active_sheet > 0 || !wb.sheets.is_empty() {
        let book_views = BytesStart::new("bookViews");
        writer.write_event(Event::Start(book_views))?;
        let mut workbook_view = BytesStart::new("workbookView");
        workbook_view.push_attribute(("activeTab", wb.active_sheet.to_string().as_str()));
        writer.write_event(Event::Empty(workbook_view))?;
        writer.write_event(Event::End(BytesEnd::new("bookViews")))?;
    }

    writer.write_event(Event::End(BytesEnd::new("workbook")))?;
    Ok(())
}