Skip to main content

datagrid_export/
lib.rs

1//! Writes what a grid shows as a file.
2//!
3//! The table comes from [`datagrid_core::export_table`], which takes the
4//! visible columns in the order the grid lays them out and the rows as sorted,
5//! filtered and grouped. This crate only writes it: each format is a feature, so
6//! that the ones with heavy dependencies cost nothing to anyone who does not
7//! ask for them. CSV and HTML need no dependency at all; Excel (`xlsx`) needs
8//! `rust_xlsxwriter`.
9//!
10//! ```
11//! use datagrid_core::{
12//!     CellValue, ColumnSpec, ExportScope, GridLocale, GridState, export_table,
13//! };
14//! use datagrid_export::{CsvOptions, to_csv};
15//!
16//! struct Person {
17//!     name: &'static str,
18//!     age: i64,
19//! }
20//!
21//! let rows = [Person { name: "Zoe", age: 31 }, Person { name: "Adi", age: 27 }];
22//! let columns = [
23//!     ColumnSpec::new("name").value(|row: &Person| CellValue::Text(row.name)),
24//!     ColumnSpec::new("age").value(|row: &Person| CellValue::Int(row.age)),
25//! ];
26//! let table = export_table(
27//!     &rows,
28//!     &columns,
29//!     |id| id.as_str().to_owned(),
30//!     &GridState::new(),
31//!     &GridLocale::english(),
32//!     ExportScope::Filtered,
33//! );
34//!
35//! assert_eq!(to_csv(&table, &CsvOptions::default()), "name,age\r\nZoe,31\r\nAdi,27\r\n");
36//! ```
37
38#![forbid(unsafe_code)]
39
40#[cfg(feature = "csv")]
41mod csv;
42#[cfg(feature = "html")]
43mod html;
44#[cfg(feature = "xlsx")]
45mod xlsx;
46
47#[cfg(feature = "csv")]
48pub use csv::{CsvOptions, CsvValues, LineEnding, to_csv};
49
50#[cfg(feature = "html")]
51pub use html::{HtmlOptions, PRINT_CSS, to_html};
52
53#[cfg(feature = "xlsx")]
54pub use xlsx::{XlsxError, XlsxOptions, to_xlsx};