miniexcel 0.4.0

Streaming XLSX and CSV reader and writer for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use std::path::{Path, PathBuf};
use std::sync::Arc;

use futures_core::Stream;
use futures_util::{FutureExt, StreamExt, pin_mut, select_biased};
use serde::Serialize;

use super::atomic::{AtomicCommitStage, export_to_path_with_hook};
use super::donor::{save_dynamic_iter_to_writer, save_dynamic_iter_to_writer_with_progress};
use crate::{CancellationToken, CellValue, DynamicRow, Error, Result, WriteOptions};

const ROW_CHANNEL_CAPACITY: usize = 16;
type ProgressCallback = Arc<dyn Fn(usize) + Send + Sync>;

enum RowMessage {
    Row(Result<DynamicRow>),
    End,
}

enum WorkerStatus {
    Ready,
    Finished(Result<usize>),
}

enum ExportSchema {
    Explicit(Vec<String>),
    Inferred,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum AsyncExportStage {
    BeforePreflight,
    ReadyForRows,
    Writing,
    Validation,
    BeforeCommit,
    Finished,
}

struct FutureCancellationGuard {
    cancellation: CancellationToken,
}

impl Drop for FutureCancellationGuard {
    fn drop(&mut self) {
        self.cancellation.cancel();
    }
}

pub(crate) async fn save_with_schema_async<S>(
    path: PathBuf,
    schema: Vec<String>,
    rows: S,
    options: WriteOptions,
    cancellation: CancellationToken,
) -> Result<usize>
where
    S: Stream<Item = Result<DynamicRow>>,
{
    save_async_with_hook(
        path,
        ExportSchema::Explicit(schema),
        rows,
        options,
        cancellation,
        Arc::new(|_| {}),
        Arc::new(|_| {}),
    )
    .await
}

pub(crate) async fn save_with_schema_async_with_progress<S>(
    path: PathBuf,
    schema: Vec<String>,
    rows: S,
    options: WriteOptions,
    cancellation: CancellationToken,
    progress: ProgressCallback,
) -> Result<usize>
where
    S: Stream<Item = Result<DynamicRow>>,
{
    save_async_with_hook(
        path,
        ExportSchema::Explicit(schema),
        rows,
        options,
        cancellation,
        Arc::new(|_| {}),
        progress,
    )
    .await
}

pub(crate) async fn save_serialized_async<T, S>(
    path: PathBuf,
    rows: S,
    options: WriteOptions,
    cancellation: CancellationToken,
) -> Result<usize>
where
    T: Serialize,
    S: Stream<Item = Result<T>>,
{
    let rows = rows.map(|row| row.and_then(|row| serialized_row_to_dynamic(&row)));
    save_async_with_hook(
        path,
        ExportSchema::Inferred,
        rows,
        options,
        cancellation,
        Arc::new(|_| {}),
        Arc::new(|_| {}),
    )
    .await
}

pub(crate) async fn save_serialized_async_with_progress<T, S>(
    path: PathBuf,
    rows: S,
    options: WriteOptions,
    cancellation: CancellationToken,
    progress: ProgressCallback,
) -> Result<usize>
where
    T: Serialize,
    S: Stream<Item = Result<T>>,
{
    let rows = rows.map(|row| row.and_then(|row| serialized_row_to_dynamic(&row)));
    save_async_with_hook(
        path,
        ExportSchema::Inferred,
        rows,
        options,
        cancellation,
        Arc::new(|_| {}),
        progress,
    )
    .await
}

async fn save_async_with_hook<S>(
    path: PathBuf,
    schema: ExportSchema,
    rows: S,
    options: WriteOptions,
    cancellation: CancellationToken,
    phase_hook: Arc<dyn Fn(AsyncExportStage) + Send + Sync>,
    progress: ProgressCallback,
) -> Result<usize>
where
    S: Stream<Item = Result<DynamicRow>>,
{
    if cancellation.is_cancelled() {
        return Err(Error::cancelled());
    }
    let operation_cancellation = CancellationToken::new();
    let _guard = FutureCancellationGuard { cancellation: operation_cancellation.clone() };
    let (row_sender, row_receiver) = async_channel::bounded(ROW_CHANNEL_CAPACITY);
    let (status_sender, status_receiver) = async_channel::bounded(2);
    let worker_cancellation = cancellation.clone();
    let worker_operation_cancellation = operation_cancellation.clone();
    let worker_hook = Arc::clone(&phase_hook);
    let worker_progress = Arc::clone(&progress);

    std::thread::Builder::new().name("miniexcel-async-export".to_owned()).spawn(move || {
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            run_worker(
                &path,
                &schema,
                &options,
                row_receiver,
                &worker_cancellation,
                &worker_operation_cancellation,
                &status_sender,
                &worker_hook,
                &worker_progress,
            )
        }))
        .unwrap_or_else(|_| Err(Error::insert_package("async export worker panicked")));
        let _ = status_sender.send_blocking(WorkerStatus::Finished(result));
        worker_hook(AsyncExportStage::Finished);
    })?;

    let status = match wait_for_status(&status_receiver, &cancellation).await {
        Ok(status) => status,
        Err(error) if error.is_cancelled() => {
            operation_cancellation.cancel();
            drop(row_sender);
            return wait_for_finished(&status_receiver).await;
        }
        Err(error) => return Err(error),
    };
    if let WorkerStatus::Finished(result) = status {
        return result;
    }

    pin_mut!(rows);
    loop {
        let next_row = rows.next().fuse();
        let next_status = status_receiver.recv().fuse();
        let cancelled = cancellation.cancelled().fuse();
        pin_mut!(next_row, next_status, cancelled);
        select_biased! {
            _ = cancelled => {
                operation_cancellation.cancel();
                drop(row_sender);
                return wait_for_finished(&status_receiver).await;
            }
            worker_status = next_status => {
                let worker_status = worker_status.map_err(|_| Error::insert_package("async export worker stopped without a result"))?;
                if let WorkerStatus::Finished(result) = worker_status {
                    return result;
                }
            }
            row = next_row => {
                let (message, finished) = match row {
                    Some(Err(error)) => (RowMessage::Row(Err(error)), true),
                    Some(row) => (RowMessage::Row(row), false),
                    None => (RowMessage::End, true),
                };
                let send = row_sender.send(message).fuse();
                let cancelled = cancellation.cancelled().fuse();
                pin_mut!(send, cancelled);
                select_biased! {
                    _ = cancelled => {
                        operation_cancellation.cancel();
                        drop(row_sender);
                        return wait_for_finished(&status_receiver).await;
                    }
                    result = send => {
                        if result.is_err() {
                            drop(row_sender);
                            return wait_for_finished(&status_receiver).await;
                        }
                    }
                }
                if finished {
                    drop(row_sender);
                    return wait_for_finished_or_cancel(
                        &status_receiver,
                        &cancellation,
                        &operation_cancellation,
                    ).await;
                }
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn run_worker(
    path: &Path,
    schema: &ExportSchema,
    options: &WriteOptions,
    row_receiver: async_channel::Receiver<RowMessage>,
    cancellation: &CancellationToken,
    operation_cancellation: &CancellationToken,
    status_sender: &async_channel::Sender<WorkerStatus>,
    phase_hook: &Arc<dyn Fn(AsyncExportStage) + Send + Sync>,
    progress: &ProgressCallback,
) -> Result<usize> {
    phase_hook(AsyncExportStage::BeforePreflight);
    check_cancelled(cancellation, operation_cancellation)?;
    let mut ready_sent = false;
    export_to_path_with_hook(
        path,
        options.sheet_name(),
        options.overwrite_file(),
        |writer| {
            phase_hook(AsyncExportStage::Writing);
            let mut ended = false;
            let rows = std::iter::from_fn(|| {
                if ended {
                    return None;
                }
                if let Err(error) = check_cancelled(cancellation, operation_cancellation) {
                    ended = true;
                    return Some(Err(error));
                }
                match row_receiver.recv_blocking() {
                    Ok(RowMessage::Row(row)) => Some(row),
                    Ok(RowMessage::End) => {
                        ended = true;
                        None
                    }
                    Err(_) => {
                        ended = true;
                        Some(Err(Error::cancelled()))
                    }
                }
            });
            match schema {
                ExportSchema::Explicit(schema) => save_dynamic_iter_to_writer_with_progress(
                    writer,
                    schema,
                    rows,
                    options,
                    |cells| progress(cells),
                ),
                ExportSchema::Inferred => {
                    save_inferred_iter_to_writer(writer, rows, options, progress)
                }
            }
        },
        |stage| {
            let phase = match stage {
                AtomicCommitStage::Preflight => AsyncExportStage::BeforePreflight,
                AtomicCommitStage::RowGeneration => {
                    if !ready_sent {
                        ready_sent = true;
                        phase_hook(AsyncExportStage::ReadyForRows);
                        check_cancelled(cancellation, operation_cancellation)?;
                        status_sender
                            .send_blocking(WorkerStatus::Ready)
                            .map_err(|_| Error::cancelled())?;
                    }
                    return check_cancelled(cancellation, operation_cancellation);
                }
                AtomicCommitStage::ZipCopy | AtomicCommitStage::ZipFinish => {
                    AsyncExportStage::Writing
                }
                AtomicCommitStage::Validation => AsyncExportStage::Validation,
                AtomicCommitStage::Commit => AsyncExportStage::BeforeCommit,
            };
            phase_hook(phase);
            check_cancelled(cancellation, operation_cancellation)
        },
    )
}

fn save_inferred_iter_to_writer<I>(
    writer: &mut std::fs::File,
    mut rows: I,
    options: &WriteOptions,
    progress: &ProgressCallback,
) -> Result<usize>
where
    I: Iterator<Item = Result<DynamicRow>>,
{
    let Some(first) = rows.next().transpose()? else {
        if options.print_header() {
            return Err(Error::missing_schema());
        }
        return save_dynamic_iter_to_writer(writer, &[], std::iter::empty(), options);
    };
    let schema = first.keys().cloned().collect::<Vec<_>>();
    if schema.is_empty() {
        return Err(Error::missing_schema());
    }
    let rows = std::iter::once(Ok(first)).chain(rows).enumerate().map(|(index, row)| {
        let row = row?;
        if row.len() != schema.len() || schema.iter().any(|field| !row.contains_key(field)) {
            return Err(Error::invalid_write_options(format!(
                "serialized row {} fields do not match the inferred schema",
                index + 1
            )));
        }
        Ok(row)
    });
    save_dynamic_iter_to_writer_with_progress(writer, &schema, rows, options, |cells| {
        progress(cells);
    })
}

fn serialized_row_to_dynamic<T>(row: &T) -> Result<DynamicRow>
where
    T: Serialize,
{
    let value = serde_json::to_value(row).map_err(|error| {
        Error::invalid_write_options(format!("cannot serialize XLSX row: {error}"))
    })?;
    let fields = value.as_object().ok_or_else(|| {
        Error::invalid_write_options("typed writing requires rows serialized as structs or maps")
    })?;
    fields.iter().map(|(name, value)| Ok((name.clone(), serialized_cell(name, value)?))).collect()
}

fn serialized_cell(field: &str, value: &serde_json::Value) -> Result<CellValue> {
    match value {
        serde_json::Value::Null => Ok(CellValue::Empty),
        serde_json::Value::Bool(value) => Ok(CellValue::Bool(*value)),
        serde_json::Value::Number(value) => {
            if let Some(value) = value.as_i64() {
                Ok(CellValue::Int(value))
            } else if let Some(value) = value.as_u64() {
                Ok(i64::try_from(value).map_or(CellValue::Float(value as f64), CellValue::Int))
            } else {
                value.as_f64().map(CellValue::Float).ok_or_else(|| {
                    Error::invalid_write_options(format!(
                        "serialized field '{field}' is not a finite number"
                    ))
                })
            }
        }
        serde_json::Value::String(value) => Ok(CellValue::String(value.clone())),
        serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
            Err(Error::invalid_write_options(format!(
                "serialized field '{field}' must be a scalar value"
            )))
        }
    }
}

fn check_cancelled(
    cancellation: &CancellationToken,
    operation_cancellation: &CancellationToken,
) -> Result<()> {
    if cancellation.is_cancelled() || operation_cancellation.is_cancelled() {
        Err(Error::cancelled())
    } else {
        Ok(())
    }
}

async fn wait_for_status(
    status_receiver: &async_channel::Receiver<WorkerStatus>,
    cancellation: &CancellationToken,
) -> Result<WorkerStatus> {
    let status = status_receiver.recv().fuse();
    let cancelled = cancellation.cancelled().fuse();
    pin_mut!(status, cancelled);
    select_biased! {
        _ = cancelled => Err(Error::cancelled()),
        status = status => status.map_err(|_| Error::insert_package("async export worker stopped without a result")),
    }
}

async fn wait_for_finished(
    status_receiver: &async_channel::Receiver<WorkerStatus>,
) -> Result<usize> {
    loop {
        match status_receiver
            .recv()
            .await
            .map_err(|_| Error::insert_package("async export worker stopped without a result"))?
        {
            WorkerStatus::Ready => {}
            WorkerStatus::Finished(result) => return result,
        }
    }
}

async fn wait_for_finished_or_cancel(
    status_receiver: &async_channel::Receiver<WorkerStatus>,
    cancellation: &CancellationToken,
    operation_cancellation: &CancellationToken,
) -> Result<usize> {
    let finished = wait_for_finished(status_receiver).fuse();
    let cancelled = cancellation.cancelled().fuse();
    pin_mut!(finished, cancelled);
    select_biased! {
        _ = cancelled => {
            operation_cancellation.cancel();
            wait_for_finished(status_receiver).await
        },
        result = finished => result,
    }
}