office_oxide 0.1.1

The fastest Office document processing library — DOCX, XLSX, PPTX, DOC, XLS, PPT
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
//! C Foreign Function Interface (FFI) for office_oxide.
//!
//! Provides `#[no_mangle] pub extern "C"` functions that Go (CGo), Node.js (N-API),
//! and C# (P/Invoke) bindings can link against. The compiled `liboffice_oxide.so` /
//! `.dylib` / `.dll` / `.a` exports these symbols.
//!
//! # Error Convention
//! Most functions accept an `error_code: *mut i32` out-parameter:
//! - 0 = success
//! - 1 = invalid argument / path
//! - 2 = IO error
//! - 3 = parse error
//! - 4 = extraction failed
//! - 5 = internal error
//! - 6 = unsupported format / feature
//!
//! # Memory Convention
//! - Strings returned as `*mut c_char` are heap-allocated and must be freed with
//!   `office_oxide_free_string`.
//! - Byte buffers returned as `*mut u8` (with an `out_len`) must be freed with
//!   `office_oxide_free_bytes(ptr, len)`.
//! - Opaque handles (`*mut OfficeDocumentHandle`, `*mut OfficeEditableHandle`)
//!   must be freed with their corresponding `*_free` function.
#![allow(missing_docs)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::not_unsafe_ptr_arg_deref)]
#![allow(clippy::too_many_arguments)]

use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::path::PathBuf;
use std::ptr;
use std::slice;

use crate::Document;
use crate::edit::EditableDocument;
use crate::format::DocumentFormat;

// ─── Error codes ────────────────────────────────────────────────────────────

pub const OFFICE_OK: i32 = 0;
pub const OFFICE_ERR_INVALID_ARG: i32 = 1;
pub const OFFICE_ERR_IO: i32 = 2;
pub const OFFICE_ERR_PARSE: i32 = 3;
pub const OFFICE_ERR_EXTRACTION: i32 = 4;
pub const OFFICE_ERR_INTERNAL: i32 = 5;
pub const OFFICE_ERR_UNSUPPORTED: i32 = 6;

fn set_err(ptr: *mut i32, code: i32) {
    if !ptr.is_null() {
        unsafe { *ptr = code };
    }
}

fn classify_error(e: &crate::OfficeError) -> i32 {
    match e {
        crate::OfficeError::UnsupportedFormat(_) => OFFICE_ERR_UNSUPPORTED,
        _ => {
            let msg = format!("{e}").to_lowercase();
            if msg.contains("not found") || msg.contains("no such file") || msg.contains("io") {
                OFFICE_ERR_IO
            } else if msg.contains("parse") || msg.contains("invalid") || msg.contains("xml") {
                OFFICE_ERR_PARSE
            } else {
                OFFICE_ERR_INTERNAL
            }
        },
    }
}

fn to_c_string(s: &str) -> *mut c_char {
    // Replace NUL bytes (invalid in C strings) with replacement char.
    let cleaned: String = s.replace('\0', "\u{FFFD}");
    match CString::new(cleaned) {
        Ok(cs) => cs.into_raw(),
        Err(_) => ptr::null_mut(),
    }
}

fn cstr_to_str<'a>(ptr: *const c_char) -> Option<&'a str> {
    if ptr.is_null() {
        return None;
    }
    unsafe { CStr::from_ptr(ptr).to_str().ok() }
}

fn cstr_to_pathbuf(ptr: *const c_char) -> Option<PathBuf> {
    cstr_to_str(ptr).map(PathBuf::from)
}

// ─── Version / memory ──────────────────────────────────────────────────────

/// Return the library version as a NUL-terminated C string. Do not free.
#[unsafe(no_mangle)]
pub extern "C" fn office_oxide_version() -> *const c_char {
    static VERSION: &[u8] = concat!(env!("CARGO_PKG_VERSION"), "\0").as_bytes();
    VERSION.as_ptr() as *const c_char
}

/// Free a string returned by any FFI function.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn office_oxide_free_string(ptr: *mut c_char) {
    if !ptr.is_null() {
        drop(unsafe { CString::from_raw(ptr) });
    }
}

/// Free a byte buffer returned by an FFI function.
///
/// `len` must match the `out_len` returned alongside the pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn office_oxide_free_bytes(ptr: *mut u8, len: usize) {
    if !ptr.is_null() && len > 0 {
        drop(unsafe { Vec::from_raw_parts(ptr, len, len) });
    }
}

// ─── Format detection ──────────────────────────────────────────────────────

/// Detect document format from a file path. Returns the extension as a static
/// C string ("docx", "xlsx", etc.) or NULL if unsupported. Do not free.
#[unsafe(no_mangle)]
pub extern "C" fn office_oxide_detect_format(path: *const c_char) -> *const c_char {
    let Some(path) = cstr_to_pathbuf(path) else {
        return ptr::null();
    };
    match DocumentFormat::from_path(&path) {
        Some(f) => format_to_cstr(f),
        None => ptr::null(),
    }
}

fn format_to_cstr(f: DocumentFormat) -> *const c_char {
    static DOCX: &[u8] = b"docx\0";
    static XLSX: &[u8] = b"xlsx\0";
    static PPTX: &[u8] = b"pptx\0";
    static DOC: &[u8] = b"doc\0";
    static XLS: &[u8] = b"xls\0";
    static PPT: &[u8] = b"ppt\0";
    let s: &[u8] = match f {
        DocumentFormat::Docx => DOCX,
        DocumentFormat::Xlsx => XLSX,
        DocumentFormat::Pptx => PPTX,
        DocumentFormat::Doc => DOC,
        DocumentFormat::Xls => XLS,
        DocumentFormat::Ppt => PPT,
    };
    s.as_ptr() as *const c_char
}

fn parse_format(s: &str) -> Option<DocumentFormat> {
    DocumentFormat::from_extension(s)
}

// ─── Document (read-only) ───────────────────────────────────────────────────

/// Opaque handle for a read-only Document.
pub struct OfficeDocumentHandle {
    _doc: Document,
}

/// Open a document from a file path. Format is detected from the extension.
#[unsafe(no_mangle)]
pub extern "C" fn office_document_open(
    path: *const c_char,
    error_code: *mut i32,
) -> *mut OfficeDocumentHandle {
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    };
    match Document::open(&path) {
        Ok(doc) => {
            set_err(error_code, OFFICE_OK);
            Box::into_raw(Box::new(OfficeDocumentHandle { _doc: doc })) as *mut _
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

/// Open a document from an in-memory byte buffer.
///
/// `format` must be one of "docx", "xlsx", "pptx", "doc", "xls", "ppt".
#[unsafe(no_mangle)]
pub extern "C" fn office_document_open_from_bytes(
    data: *const u8,
    len: usize,
    format: *const c_char,
    error_code: *mut i32,
) -> *mut OfficeDocumentHandle {
    if data.is_null() || len == 0 {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let Some(fmt_str) = cstr_to_str(format) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    };
    let Some(fmt) = parse_format(fmt_str) else {
        set_err(error_code, OFFICE_ERR_UNSUPPORTED);
        return ptr::null_mut();
    };
    let bytes = unsafe { slice::from_raw_parts(data, len) }.to_vec();
    let cursor = std::io::Cursor::new(bytes);
    match Document::from_reader(cursor, fmt) {
        Ok(doc) => {
            set_err(error_code, OFFICE_OK);
            Box::into_raw(Box::new(OfficeDocumentHandle { _doc: doc })) as *mut _
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

/// Free a document handle.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn office_document_free(handle: *mut OfficeDocumentHandle) {
    if !handle.is_null() {
        drop(unsafe { Box::from_raw(handle) });
    }
}

/// Return the document format as a static C string. Do not free. Returns NULL on invalid handle.
#[unsafe(no_mangle)]
pub extern "C" fn office_document_format(handle: *const OfficeDocumentHandle) -> *const c_char {
    if handle.is_null() {
        return ptr::null();
    }
    let h = unsafe { &*handle };
    format_to_cstr(h._doc.format())
}

/// Extract plain text. Returns a heap-allocated C string — free with `office_oxide_free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn office_document_plain_text(
    handle: *const OfficeDocumentHandle,
    error_code: *mut i32,
) -> *mut c_char {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let h = unsafe { &*handle };
    let s = h._doc.plain_text();
    set_err(error_code, OFFICE_OK);
    to_c_string(&s)
}

/// Convert to Markdown. Free with `office_oxide_free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn office_document_to_markdown(
    handle: *const OfficeDocumentHandle,
    error_code: *mut i32,
) -> *mut c_char {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let h = unsafe { &*handle };
    let s = h._doc.to_markdown();
    set_err(error_code, OFFICE_OK);
    to_c_string(&s)
}

/// Convert to HTML fragment. Free with `office_oxide_free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn office_document_to_html(
    handle: *const OfficeDocumentHandle,
    error_code: *mut i32,
) -> *mut c_char {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let h = unsafe { &*handle };
    let s = h._doc.to_html();
    set_err(error_code, OFFICE_OK);
    to_c_string(&s)
}

/// Convert to the document IR, serialized as JSON. Free with `office_oxide_free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn office_document_to_ir_json(
    handle: *const OfficeDocumentHandle,
    error_code: *mut i32,
) -> *mut c_char {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let h = unsafe { &*handle };
    let ir = h._doc.to_ir();
    match serde_json::to_string(&ir) {
        Ok(s) => {
            set_err(error_code, OFFICE_OK);
            to_c_string(&s)
        },
        Err(_) => {
            set_err(error_code, OFFICE_ERR_INTERNAL);
            ptr::null_mut()
        },
    }
}

/// Save/convert the document to a file. Target format is detected from the extension.
/// Returns 0 on success, nonzero error code on failure.
#[unsafe(no_mangle)]
pub extern "C" fn office_document_save_as(
    handle: *const OfficeDocumentHandle,
    path: *const c_char,
    error_code: *mut i32,
) -> i32 {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    }
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let h = unsafe { &*handle };
    match h._doc.save_as(&path) {
        Ok(()) => {
            set_err(error_code, OFFICE_OK);
            OFFICE_OK
        },
        Err(e) => {
            let c = classify_error(&e);
            set_err(error_code, c);
            c
        },
    }
}

// ─── EditableDocument ──────────────────────────────────────────────────────

/// Opaque handle for an editable document.
pub struct OfficeEditableHandle {
    doc: EditableDocument,
}

/// Open a document for editing. Supports DOCX, XLSX, PPTX.
#[unsafe(no_mangle)]
pub extern "C" fn office_editable_open(
    path: *const c_char,
    error_code: *mut i32,
) -> *mut OfficeEditableHandle {
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    };
    match EditableDocument::open(&path) {
        Ok(doc) => {
            set_err(error_code, OFFICE_OK);
            Box::into_raw(Box::new(OfficeEditableHandle { doc })) as *mut _
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

/// Open an editable document from a byte buffer.
#[unsafe(no_mangle)]
pub extern "C" fn office_editable_open_from_bytes(
    data: *const u8,
    len: usize,
    format: *const c_char,
    error_code: *mut i32,
) -> *mut OfficeEditableHandle {
    if data.is_null() || len == 0 {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let Some(fmt_str) = cstr_to_str(format) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    };
    let Some(fmt) = parse_format(fmt_str) else {
        set_err(error_code, OFFICE_ERR_UNSUPPORTED);
        return ptr::null_mut();
    };
    let bytes = unsafe { slice::from_raw_parts(data, len) }.to_vec();
    let cursor = std::io::Cursor::new(bytes);
    match EditableDocument::from_reader(cursor, fmt) {
        Ok(doc) => {
            set_err(error_code, OFFICE_OK);
            Box::into_raw(Box::new(OfficeEditableHandle { doc })) as *mut _
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

/// Free an editable document handle.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn office_editable_free(handle: *mut OfficeEditableHandle) {
    if !handle.is_null() {
        drop(unsafe { Box::from_raw(handle) });
    }
}

/// Replace every occurrence of `find` with `replace` in text content.
/// Returns the number of replacements, or -1 on error.
#[unsafe(no_mangle)]
pub extern "C" fn office_editable_replace_text(
    handle: *mut OfficeEditableHandle,
    find: *const c_char,
    replace: *const c_char,
    error_code: *mut i32,
) -> i64 {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return -1;
    }
    let Some(find_s) = cstr_to_str(find) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return -1;
    };
    let Some(replace_s) = cstr_to_str(replace) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return -1;
    };
    let h = unsafe { &mut *handle };
    let n = h.doc.replace_text(find_s, replace_s);
    set_err(error_code, OFFICE_OK);
    n as i64
}

/// Set a cell value in an XLSX document.
///
/// `value_type` is one of: 0 = empty, 1 = string, 2 = number, 3 = boolean.
/// `value_str` is used for strings (types 1) and ignored otherwise (pass NULL).
/// `value_num` is used for numbers (type 2) and booleans (type 3, nonzero = true).
#[unsafe(no_mangle)]
pub extern "C" fn office_editable_set_cell(
    handle: *mut OfficeEditableHandle,
    sheet_index: u32,
    cell_ref: *const c_char,
    value_type: i32,
    value_str: *const c_char,
    value_num: f64,
    error_code: *mut i32,
) -> i32 {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    }
    let Some(cell) = cstr_to_str(cell_ref) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let value = match value_type {
        0 => crate::xlsx::edit::CellValue::Empty,
        1 => {
            let Some(s) = cstr_to_str(value_str) else {
                set_err(error_code, OFFICE_ERR_INVALID_ARG);
                return OFFICE_ERR_INVALID_ARG;
            };
            crate::xlsx::edit::CellValue::String(s.to_string())
        },
        2 => crate::xlsx::edit::CellValue::Number(value_num),
        3 => crate::xlsx::edit::CellValue::Boolean(value_num != 0.0),
        _ => {
            set_err(error_code, OFFICE_ERR_INVALID_ARG);
            return OFFICE_ERR_INVALID_ARG;
        },
    };
    let h = unsafe { &mut *handle };
    match h.doc.set_cell(sheet_index as usize, cell, value) {
        Ok(()) => {
            set_err(error_code, OFFICE_OK);
            OFFICE_OK
        },
        Err(e) => {
            let c = classify_error(&e);
            set_err(error_code, c);
            c
        },
    }
}

/// Save the edited document to a file.
#[unsafe(no_mangle)]
pub extern "C" fn office_editable_save(
    handle: *const OfficeEditableHandle,
    path: *const c_char,
    error_code: *mut i32,
) -> i32 {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    }
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let h = unsafe { &*handle };
    match h.doc.save(&path) {
        Ok(()) => {
            set_err(error_code, OFFICE_OK);
            OFFICE_OK
        },
        Err(e) => {
            let c = classify_error(&e);
            set_err(error_code, c);
            c
        },
    }
}

/// Save the edited document into a heap-allocated byte buffer.
/// Returns a pointer and writes the length to `out_len`. Free with `office_oxide_free_bytes`.
#[unsafe(no_mangle)]
pub extern "C" fn office_editable_save_to_bytes(
    handle: *const OfficeEditableHandle,
    out_len: *mut usize,
    error_code: *mut i32,
) -> *mut u8 {
    if handle.is_null() || out_len.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let h = unsafe { &*handle };
    let buf: Vec<u8> = Vec::new();
    let mut cursor = std::io::Cursor::new(buf);
    match h.doc.write_to(&mut cursor) {
        Ok(()) => {
            let mut bytes = cursor.into_inner();
            bytes.shrink_to_fit();
            let len = bytes.len();
            let ptr = bytes.as_mut_ptr();
            std::mem::forget(bytes);
            unsafe { *out_len = len };
            set_err(error_code, OFFICE_OK);
            ptr
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

// ─── Convenience one-shot helpers ───────────────────────────────────────────

/// One-shot: open a file, extract plain text, return. Free the result with
/// `office_oxide_free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn office_extract_text(path: *const c_char, error_code: *mut i32) -> *mut c_char {
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    };
    match crate::extract_text(&path) {
        Ok(s) => {
            set_err(error_code, OFFICE_OK);
            to_c_string(&s)
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

/// One-shot: open a file, convert to markdown, return. Free with `office_oxide_free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn office_to_markdown(path: *const c_char, error_code: *mut i32) -> *mut c_char {
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    };
    match crate::to_markdown(&path) {
        Ok(s) => {
            set_err(error_code, OFFICE_OK);
            to_c_string(&s)
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

/// One-shot: open a file, convert to HTML, return. Free with `office_oxide_free_string`.
#[unsafe(no_mangle)]
pub extern "C" fn office_to_html(path: *const c_char, error_code: *mut i32) -> *mut c_char {
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    };
    match crate::to_html(&path) {
        Ok(s) => {
            set_err(error_code, OFFICE_OK);
            to_c_string(&s)
        },
        Err(e) => {
            set_err(error_code, classify_error(&e));
            ptr::null_mut()
        },
    }
}

// ─── XlsxWriter ─────────────────────────────────────────────────────────────

/// Opaque handle wrapping an XlsxWriter.
pub struct OfficeXlsxWriterHandle {
    writer: crate::xlsx::write::XlsxWriter,
}

/// Create a new XLSX writer. Free with `office_xlsx_writer_free`.
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_writer_new() -> *mut OfficeXlsxWriterHandle {
    Box::into_raw(Box::new(OfficeXlsxWriterHandle {
        writer: crate::xlsx::write::XlsxWriter::new(),
    }))
}

/// Free an XLSX writer handle.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn office_xlsx_writer_free(handle: *mut OfficeXlsxWriterHandle) {
    if !handle.is_null() {
        drop(unsafe { Box::from_raw(handle) });
    }
}

/// Add a sheet by name; returns its 0-based index, or u32::MAX on null handle.
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_writer_add_sheet(
    handle: *mut OfficeXlsxWriterHandle,
    name: *const c_char,
) -> u32 {
    if handle.is_null() {
        return u32::MAX;
    }
    let name = cstr_to_str(name).unwrap_or("Sheet");
    let h = unsafe { &mut *handle };
    h.writer.add_sheet_get_index(name) as u32
}

/// Set a cell value. value_type: 0=empty, 1=string (value_str), 2=number (value_num).
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_sheet_set_cell(
    handle: *mut OfficeXlsxWriterHandle,
    sheet: u32,
    row: u32,
    col: u32,
    value_type: i32,
    value_str: *const c_char,
    value_num: f64,
) {
    if handle.is_null() {
        return;
    }
    use crate::xlsx::write::CellData;
    let data = match value_type {
        1 => CellData::String(cstr_to_str(value_str).unwrap_or("").to_string()),
        2 => CellData::Number(value_num),
        _ => CellData::Empty,
    };
    let h = unsafe { &mut *handle };
    h.writer
        .sheet_set_cell(sheet as usize, row as usize, col as usize, data);
}

/// Set a cell with styling. bold applies bold weight; bg_color is a 6-char hex
/// string ("D3D3D3") or NULL for no background fill.
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_sheet_set_cell_styled(
    handle: *mut OfficeXlsxWriterHandle,
    sheet: u32,
    row: u32,
    col: u32,
    value_type: i32,
    value_str: *const c_char,
    value_num: f64,
    bold: bool,
    bg_color: *const c_char,
) {
    if handle.is_null() {
        return;
    }
    use crate::xlsx::write::{CellData, CellStyle};
    let data = match value_type {
        1 => CellData::String(cstr_to_str(value_str).unwrap_or("").to_string()),
        2 => CellData::Number(value_num),
        _ => CellData::Empty,
    };
    let mut style = CellStyle::new();
    if bold {
        style = style.bold();
    }
    if let Some(bg) = cstr_to_str(bg_color) {
        if !bg.is_empty() {
            style = style.background(bg.to_string());
        }
    }
    let h = unsafe { &mut *handle };
    h.writer
        .sheet_set_cell_styled(sheet as usize, row as usize, col as usize, data, style);
}

/// Merge a rectangular range. row_span / col_span must be >= 1.
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_sheet_merge_cells(
    handle: *mut OfficeXlsxWriterHandle,
    sheet: u32,
    row: u32,
    col: u32,
    row_span: u32,
    col_span: u32,
) {
    if handle.is_null() {
        return;
    }
    let h = unsafe { &mut *handle };
    h.writer.sheet_merge_cells(
        sheet as usize,
        row as usize,
        col as usize,
        row_span as usize,
        col_span as usize,
    );
}

/// Set column width in Excel character units (e.g. 20.0).
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_sheet_set_column_width(
    handle: *mut OfficeXlsxWriterHandle,
    sheet: u32,
    col: u32,
    width: f64,
) {
    if handle.is_null() {
        return;
    }
    let h = unsafe { &mut *handle };
    h.writer
        .sheet_set_column_width(sheet as usize, col as usize, width);
}

/// Save to a file. Returns OFFICE_OK (0) on success.
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_writer_save(
    handle: *const OfficeXlsxWriterHandle,
    path: *const c_char,
    error_code: *mut i32,
) -> i32 {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    }
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let h = unsafe { &*handle };
    match h.writer.save(&path) {
        Ok(()) => {
            set_err(error_code, OFFICE_OK);
            OFFICE_OK
        },
        Err(_) => {
            set_err(error_code, OFFICE_ERR_INTERNAL);
            OFFICE_ERR_INTERNAL
        },
    }
}

/// Serialize to a heap byte buffer. Free with office_oxide_free_bytes.
#[unsafe(no_mangle)]
pub extern "C" fn office_xlsx_writer_to_bytes(
    handle: *const OfficeXlsxWriterHandle,
    out_len: *mut usize,
    error_code: *mut i32,
) -> *mut u8 {
    if handle.is_null() || out_len.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let h = unsafe { &*handle };
    let mut cursor = std::io::Cursor::new(Vec::new());
    match h.writer.write_to(&mut cursor) {
        Ok(()) => {
            let mut bytes = cursor.into_inner();
            bytes.shrink_to_fit();
            let len = bytes.len();
            let raw_ptr = bytes.as_mut_ptr();
            std::mem::forget(bytes);
            unsafe { *out_len = len };
            set_err(error_code, OFFICE_OK);
            raw_ptr
        },
        Err(_) => {
            set_err(error_code, OFFICE_ERR_INTERNAL);
            ptr::null_mut()
        },
    }
}

// ─── PptxWriter ──────────────────────────────────────────────────────────────

/// Opaque handle wrapping a PptxWriter.
pub struct OfficePptxWriterHandle {
    writer: crate::pptx::write::PptxWriter,
}

/// Create a new PPTX writer. Free with `office_pptx_writer_free`.
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_writer_new() -> *mut OfficePptxWriterHandle {
    Box::into_raw(Box::new(OfficePptxWriterHandle {
        writer: crate::pptx::write::PptxWriter::new(),
    }))
}

/// Free a PPTX writer handle.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn office_pptx_writer_free(handle: *mut OfficePptxWriterHandle) {
    if !handle.is_null() {
        drop(unsafe { Box::from_raw(handle) });
    }
}

/// Override presentation canvas size. 914400 EMU = 1 inch.
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_writer_set_presentation_size(
    handle: *mut OfficePptxWriterHandle,
    cx: u64,
    cy: u64,
) {
    if handle.is_null() {
        return;
    }
    let h = unsafe { &mut *handle };
    h.writer.set_presentation_size(cx, cy);
}

/// Add a slide; returns its 0-based index, or u32::MAX on null handle.
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_writer_add_slide(handle: *mut OfficePptxWriterHandle) -> u32 {
    if handle.is_null() {
        return u32::MAX;
    }
    let h = unsafe { &mut *handle };
    h.writer.add_slide_get_index() as u32
}

/// Set the slide title.
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_slide_set_title(
    handle: *mut OfficePptxWriterHandle,
    slide: u32,
    title: *const c_char,
) {
    if handle.is_null() {
        return;
    }
    let title = cstr_to_str(title).unwrap_or("");
    let h = unsafe { &mut *handle };
    h.writer.slide_set_title(slide as usize, title);
}

/// Add a plain text paragraph to the slide body.
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_slide_add_text(
    handle: *mut OfficePptxWriterHandle,
    slide: u32,
    text: *const c_char,
) {
    if handle.is_null() {
        return;
    }
    let text = cstr_to_str(text).unwrap_or("");
    let h = unsafe { &mut *handle };
    h.writer.slide_add_text(slide as usize, text);
}

fn parse_image_format(s: &str) -> Option<crate::ir::ImageFormat> {
    match s.to_ascii_lowercase().as_str() {
        "png" => Some(crate::ir::ImageFormat::Png),
        "jpeg" | "jpg" => Some(crate::ir::ImageFormat::Jpeg),
        "gif" => Some(crate::ir::ImageFormat::Gif),
        _ => None,
    }
}

/// Embed an image on a slide.
///
/// `data`/`len` are the raw image bytes (PNG, JPEG, or GIF).
/// `format` is "png", "jpeg"/"jpg", or "gif".
/// `x`, `y`, `cx`, `cy` are in EMU (914400 = 1 inch).
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_slide_add_image(
    handle: *mut OfficePptxWriterHandle,
    slide: u32,
    data: *const u8,
    len: usize,
    format: *const c_char,
    x: i64,
    y: i64,
    cx: u64,
    cy: u64,
) {
    if handle.is_null() || data.is_null() || len == 0 {
        return;
    }
    let Some(fmt_str) = cstr_to_str(format) else {
        return;
    };
    let Some(fmt) = parse_image_format(fmt_str) else {
        return;
    };
    let bytes = unsafe { slice::from_raw_parts(data, len) }.to_vec();
    let h = unsafe { &mut *handle };
    h.writer
        .slide_add_image(slide as usize, bytes, fmt, x, y, cx, cy);
}

/// Save to a file. Returns OFFICE_OK (0) on success.
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_writer_save(
    handle: *const OfficePptxWriterHandle,
    path: *const c_char,
    error_code: *mut i32,
) -> i32 {
    if handle.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    }
    let Some(path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let h = unsafe { &*handle };
    match h.writer.save(&path) {
        Ok(()) => {
            set_err(error_code, OFFICE_OK);
            OFFICE_OK
        },
        Err(_) => {
            set_err(error_code, OFFICE_ERR_INTERNAL);
            OFFICE_ERR_INTERNAL
        },
    }
}

/// Serialize to a heap byte buffer. Free with office_oxide_free_bytes.
#[unsafe(no_mangle)]
pub extern "C" fn office_pptx_writer_to_bytes(
    handle: *const OfficePptxWriterHandle,
    out_len: *mut usize,
    error_code: *mut i32,
) -> *mut u8 {
    if handle.is_null() || out_len.is_null() {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return ptr::null_mut();
    }
    let h = unsafe { &*handle };
    let mut cursor = std::io::Cursor::new(Vec::new());
    match h.writer.write_to(&mut cursor) {
        Ok(()) => {
            let mut bytes = cursor.into_inner();
            bytes.shrink_to_fit();
            let len = bytes.len();
            let raw_ptr = bytes.as_mut_ptr();
            std::mem::forget(bytes);
            unsafe { *out_len = len };
            set_err(error_code, OFFICE_OK);
            raw_ptr
        },
        Err(_) => {
            set_err(error_code, OFFICE_ERR_INTERNAL);
            ptr::null_mut()
        },
    }
}

/// One-shot: convert a Markdown string to an Office document at `path`.
///
/// `format` must be one of `"docx"`, `"xlsx"`, or `"pptx"` (case-insensitive).
/// Returns `OFFICE_OK` (0) on success, a negative error code on failure.
#[unsafe(no_mangle)]
pub extern "C" fn office_create_from_markdown(
    markdown: *const c_char,
    format: *const c_char,
    path: *const c_char,
    error_code: *mut i32,
) -> i32 {
    let Some(md) = cstr_to_str(markdown) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let Some(fmt_str) = cstr_to_str(format) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let Some(out_path) = cstr_to_pathbuf(path) else {
        set_err(error_code, OFFICE_ERR_INVALID_ARG);
        return OFFICE_ERR_INVALID_ARG;
    };
    let doc_format = match fmt_str.to_ascii_lowercase().as_str() {
        "docx" => crate::DocumentFormat::Docx,
        "xlsx" => crate::DocumentFormat::Xlsx,
        "pptx" => crate::DocumentFormat::Pptx,
        _ => {
            set_err(error_code, OFFICE_ERR_INVALID_ARG);
            return OFFICE_ERR_INVALID_ARG;
        },
    };
    match crate::create::create_from_markdown(md, doc_format, &out_path) {
        Ok(()) => {
            set_err(error_code, OFFICE_OK);
            OFFICE_OK
        },
        Err(e) => {
            let code = classify_error(&e);
            set_err(error_code, code);
            code
        },
    }
}