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 needs no dependency at all.
8//!
9//! ```
10//! use datagrid_core::{
11//!     CellValue, ColumnSpec, ExportScope, GridLocale, GridState, export_table,
12//! };
13//! use datagrid_export::{CsvOptions, to_csv};
14//!
15//! struct Person {
16//!     name: &'static str,
17//!     age: i64,
18//! }
19//!
20//! let rows = [Person { name: "Zoe", age: 31 }, Person { name: "Adi", age: 27 }];
21//! let columns = [
22//!     ColumnSpec::new("name").value(|row: &Person| CellValue::Text(row.name)),
23//!     ColumnSpec::new("age").value(|row: &Person| CellValue::Int(row.age)),
24//! ];
25//! let table = export_table(
26//!     &rows,
27//!     &columns,
28//!     |id| id.as_str().to_owned(),
29//!     &GridState::new(),
30//!     &GridLocale::english(),
31//!     ExportScope::Filtered,
32//! );
33//!
34//! assert_eq!(to_csv(&table, &CsvOptions::default()), "name,age\r\nZoe,31\r\nAdi,27\r\n");
35//! ```
36
37#![forbid(unsafe_code)]
38
39#[cfg(feature = "csv")]
40mod csv;
41#[cfg(feature = "html")]
42mod html;
43
44#[cfg(feature = "csv")]
45pub use csv::{CsvOptions, CsvValues, LineEnding, to_csv};
46
47#[cfg(feature = "html")]
48pub use html::{HtmlOptions, PRINT_CSS, to_html};