serde_csv_core/lib.rs
1//! CSV serialization and deserialization for `no_std` crates.
2//!
3//! `serde-csv-core` builds upon [`csv-core`](https://crates.io/crates/csv-core) crate.
4//! It doesn't require any memory allocations, which means that it's well-suited for embedded environments.
5//!
6//! # Serialization
7//! [`Writer`] serializes one record at a time.
8//! ```
9//! use heapless::String;
10//! use serde::Serialize;
11//!
12//! #[derive(Serialize)]
13//! struct Record {
14//! pub country: String<32>,
15//! pub city: String<32>,
16//! pub population: u32,
17//! }
18//!
19//! let records = [
20//! Record {
21//! country: "Poland".into(),
22//! city: "Cracow".into(),
23//! population: 766_683,
24//! },
25//! Record {
26//! country: "Japan".into(),
27//! city: "Tokyo".into(),
28//! population: 13_515_271,
29//! },
30//! ];
31//!
32//! let mut writer = serde_csv_core::Writer::new();
33//! let mut csv = [0; 128];
34//! let mut nwritten = 0;
35//! for record in &records {
36//! nwritten += writer.serialize(&record, &mut csv[nwritten..])?;
37//! }
38//!
39//! assert_eq!(&csv[..nwritten], b"Poland,Cracow,766683\nJapan,Tokyo,13515271\n");
40//!
41//! # Ok::<(), serde_csv_core::ser::Error>(())
42//! ```
43//!
44//! # Deserialization
45//! [`Reader<N>`] deserializes one record at a time.
46//! `N` is a capacity of an internal buffer that's used to temporarily store unescaped fields.
47//! ```
48//! use heapless::{String, Vec};
49//! use serde::Deserialize;
50//!
51//! #[derive(Debug, PartialEq, Eq, Deserialize)]
52//! struct Record {
53//! pub country: String<32>,
54//! pub city: String<32>,
55//! pub population: u32,
56//! }
57//!
58//! let csv = b"Poland,Cracow,766683\nJapan,Tokyo,13515271\n";
59//!
60//! let mut reader = serde_csv_core::Reader::<32>::new();
61//! let mut records: Vec<Record, 2> = Vec::new();
62//! let mut nread = 0;
63//! while nread < csv.len() {
64//! let (record, n) = reader.deserialize::<Record>(&csv[nread..])?;
65//! nread += n;
66//! records.push(record);
67//! }
68//!
69//! assert_eq!(records, &[
70//! Record {
71//! country: "Poland".into(),
72//! city: "Cracow".into(),
73//! population: 766_683,
74//! },
75//! Record {
76//! country: "Japan".into(),
77//! city: "Tokyo".into(),
78//! population: 13_515_271,
79//! },
80//! ]);
81//! # Ok::<(), serde_csv_core::de::Error>(())
82//! ```
83//!
84//! # Configuration
85//! Both [`Writer`] and [`Reader`] are wrappers for [`csv_core::Writer`]
86//! and [`csv_core::Reader`], respectively. You can use [`csv_core::WriterBuilder`]
87//! and [`csv_core::ReaderBuilder`] in combination with `from_builder` constructors
88//! to tweak things like field delimiters etc.
89//! ```
90//! use serde_csv_core::csv_core;
91//!
92//! let writer = serde_csv_core::Writer::from_builder(
93//! csv_core::WriterBuilder::new()
94//! .delimiter(b'-')
95//! );
96//! ```
97#![no_std]
98
99pub mod de;
100pub mod ser;
101
102#[doc(inline)]
103pub use de::Reader;
104#[doc(inline)]
105pub use ser::Writer;
106
107pub use csv_core;
108#[cfg(feature = "heapless")]
109pub use heapless;