rust-openzl 0.1.0

Safe Rust bindings for OpenZL - a graph-based typed compression library
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
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
//! # OpenZL Rust Bindings
//!
//! Safe, ergonomic Rust bindings for OpenZL - a graph-based typed compression library
//! optimized for structured data.
//!
//! ## What is OpenZL?
//!
//! **OpenZL is fundamentally different from generic compressors like zlib or zstd.**
//!
//! It's a **graph-based typed compression library** where compression graphs define
//! *how* to compress specific data structures. This allows OpenZL to apply type-specific
//! optimizations (delta encoding, bitpacking, transpose, etc.) that aren't possible with
//! generic byte-stream compression.
//!
//! ## Quick Start
//!
//! ### Serial Compression (Generic Data)
//!
//! ```
//! use rust_openzl::{compress_serial, decompress_serial};
//!
//! let data = b"Hello, OpenZL!";
//! let compressed = compress_serial(data)?;
//! let decompressed = decompress_serial(&compressed)?;
//! assert_eq!(data.as_slice(), decompressed.as_slice());
//! # Ok::<(), rust_openzl::Error>(())
//! ```
//!
//! ### Numeric Compression (Type-Optimized)
//!
//! ```
//! use rust_openzl::{compress_numeric, decompress_numeric};
//!
//! // Compress numeric arrays with specialized algorithms
//! let data: Vec<u32> = (0..10000).collect();
//! let compressed = compress_numeric(&data)?;
//! let decompressed: Vec<u32> = decompress_numeric(&compressed)?;
//! assert_eq!(data, decompressed);
//! # Ok::<(), rust_openzl::Error>(())
//! ```
//!
//! ### Graph-Based Compression
//!
//! ```
//! use rust_openzl::{compress_with_graph, decompress_serial, ZstdGraph, NumericGraph};
//!
//! let data = b"Repeated data...".repeat(100);
//!
//! // Use specific compression graphs
//! let compressed = compress_with_graph(&data, &ZstdGraph)?;
//! let decompressed = decompress_serial(&compressed)?;
//! # Ok::<(), rust_openzl::Error>(())
//! ```
//!
//! ## Core Concepts
//!
//! ### Compression Graphs
//!
//! Compression graphs are the heart of OpenZL. They define the compression strategy:
//!
//! - **ZSTD**: General-purpose compression (similar to zstd)
//! - **NUMERIC**: Optimized for numeric arrays (delta encoding, bitpacking)
//! - **FIELD_LZ**: Field-level LZ compression for structured data
//! - **STORE**: No compression (useful for testing)
//!
//! ### TypedRef and TypedBuffer
//!
//! - [`TypedRef`]: Borrowed reference to typed input data (with lifetime)
//! - [`TypedBuffer`]: Owned decompression output buffer
//!
//! These provide type information to OpenZL, enabling type-specific optimizations.
//!
//! ## Architecture
//!
//! ```text
//! High-level APIs (compress_numeric, etc.)
//!//! Graph-based compression (compress_with_graph)
//!//! TypedRef compression (compress_typed_ref)
//!//! CCtx + Compressor (graph registration)
//!//! OpenZL C library (via rust-openzl-sys)
//! ```
//!
//! ## Examples
//!
//! See the `examples/` directory for complete examples:
//!
//! - `serial_compress.rs` - Basic serial compression
//! - `numeric_compress.rs` - Numeric array compression with different types
//! - `graph_compression.rs` - Using different compression graphs
//! - `typed_compression.rs` - Advanced TypedRef usage
//!
//! ## Safety
//!
//! This crate provides safe abstractions over the unsafe FFI:
//!
//! - RAII wrappers with `Drop` for resource cleanup
//! - Lifetime-checked `TypedRef` to prevent use-after-free
//! - Type validation for numeric compression
//! - Error handling via `Result<T, Error>`
//!
//! ## Performance
//!
//! OpenZL can achieve excellent compression ratios on structured data:
//!
//! - Sequential numeric data: 0.30% (400:1 ratio)
//! - Timestamps: 2.16% (46:1 ratio)
//! - Repetitive text: 1.96% (51:1 ratio)
//!
//! (Actual ratios depend on data patterns)

use rust_openzl_sys as sys;
use std::ffi::CStr;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("OpenZL error: {code} ({name}){context}")]
    Report {
        code: i32,
        name: String,
        context: String,
    },
}

// ============================================================================
// Helper functions
// ============================================================================

/// Returns the maximum compressed size for a given source size
fn compress_bound(src_size: usize) -> usize {
    unsafe { sys::openzl_compress_bound(src_size) }
}

/// Convert a ZL_Report to a Rust Error
fn report_to_error(r: sys::ZL_Report) -> Error {
    let code = sys::report_code(r);
    let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
        .to_string_lossy()
        .into_owned();
    Error::Report {
        code,
        name,
        context: String::new(),
    }
}

/// OpenZL warning (non-fatal issue during compression/decompression)
#[derive(Debug, Clone)]
pub struct Warning {
    pub code: i32,
    pub name: String,
}

/// Opaque GraphID for identifying compression graphs
#[derive(Debug, Copy, Clone)]
pub struct GraphId(sys::ZL_GraphID);

impl GraphId {
    /// Check if this GraphID is valid
    pub fn is_valid(&self) -> bool {
        unsafe { sys::ZL_GraphID_isValid(self.0) != 0 }
    }

    #[allow(dead_code)] // Used for custom graph registration (Step 12)
    pub(crate) fn as_raw(&self) -> sys::ZL_GraphID {
        self.0
    }

    #[allow(dead_code)] // Used for custom graph registration (Step 12)
    pub(crate) fn from_raw(id: sys::ZL_GraphID) -> Self {
        GraphId(id)
    }
}

impl PartialEq for GraphId {
    fn eq(&self, other: &Self) -> bool {
        self.0.gid == other.0.gid
    }
}

impl Eq for GraphId {}

/// Standard compression graphs provided by OpenZL
pub mod graphs {
    use super::*;

    const fn make_graph_id(id: u32) -> GraphId {
        GraphId(sys::ZL_GraphID { gid: id })
    }

    /// No compression - stores data as-is
    pub const STORE: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_store as u32);

    /// ZSTD compression (general purpose)
    pub const ZSTD: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_zstd as u32);

    /// Optimized for numeric data
    pub const NUMERIC: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_select_numeric as u32);

    /// Field-level LZ compression
    pub const FIELD_LZ: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_field_lz as u32);

    /// FSE entropy encoding
    pub const FSE: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_fse as u32);

    /// Huffman entropy encoding
    pub const HUFFMAN: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_huffman as u32);

    /// Combined entropy encoding (FSE/Huffman selection)
    pub const ENTROPY: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_entropy as u32);

    /// Bitpacking compression
    pub const BITPACK: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_bitpack as u32);

    /// Constant value compression
    pub const CONSTANT: GraphId = make_graph_id(sys::ZL_StandardGraphID::ZL_StandardGraphID_constant as u32);
}

/// Compression graph builder and manager
///
/// Compressor is used to register and manage compression graphs, which define
/// HOW to compress data. This is the heart of OpenZL's typed compression.
pub struct Compressor(*mut sys::ZL_Compressor);

impl Compressor {
    /// Create a new Compressor for graph registration
    pub fn new() -> Self {
        let ptr = unsafe { sys::ZL_Compressor_create() };
        assert!(!ptr.is_null(), "ZL_Compressor_create returned null");
        Compressor(ptr)
    }

    /// Set a global compression parameter
    pub fn set_parameter(&mut self, param: sys::ZL_CParam, value: i32) -> Result<(), Error> {
        let r = unsafe { sys::ZL_Compressor_setParameter(self.0, param, value) };
        if sys::report_is_error(r) {
            let code = sys::report_code(r);
            let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
                .to_string_lossy()
                .into_owned();
            return Err(Error::Report {
                code,
                name,
                context: String::new(),
            });
        }
        Ok(())
    }

    /// Get warnings generated during graph construction/validation
    pub fn warnings(&self) -> Vec<Warning> {
        let arr = unsafe { sys::ZL_Compressor_getWarnings(self.0) };
        let slice = unsafe { std::slice::from_raw_parts(arr.errors, arr.size) };
        slice
            .iter()
            .map(|e| {
                let code = unsafe { sys::openzl_error_get_code(e) };
                let name = unsafe { CStr::from_ptr(sys::openzl_error_get_name(e)) }
                    .to_string_lossy()
                    .into_owned();
                Warning { code, name }
            })
            .collect()
    }

    pub(crate) fn as_ptr(&self) -> *const sys::ZL_Compressor {
        self.0 as *const _
    }

    pub(crate) fn as_mut_ptr(&mut self) -> *mut sys::ZL_Compressor {
        self.0
    }
}

impl Drop for Compressor {
    fn drop(&mut self) {
        unsafe { sys::ZL_Compressor_free(self.0) }
    }
}

impl Default for Compressor {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Graph Function API
// ============================================================================

/// Trait for defining compression graphs.
///
/// Implementors define how to build a compression graph by registering
/// nodes and edges with the Compressor.
pub trait GraphFn {
    /// Build the compression graph using the provided Compressor.
    ///
    /// Returns the starting GraphID for this graph.
    fn build_graph(&self, compressor: &mut Compressor) -> GraphId;
}

/// Standard graph: ZSTD compression (general purpose)
pub struct ZstdGraph;

impl GraphFn for ZstdGraph {
    fn build_graph(&self, _compressor: &mut Compressor) -> GraphId {
        graphs::ZSTD
    }
}

/// Standard graph: Numeric compression (optimized for numeric data)
pub struct NumericGraph;

impl GraphFn for NumericGraph {
    fn build_graph(&self, _compressor: &mut Compressor) -> GraphId {
        graphs::NUMERIC
    }
}

/// Standard graph: Store (no compression, useful for testing)
pub struct StoreGraph;

impl GraphFn for StoreGraph {
    fn build_graph(&self, _compressor: &mut Compressor) -> GraphId {
        graphs::STORE
    }
}

/// Standard graph: Field-level LZ compression
pub struct FieldLzGraph;

impl GraphFn for FieldLzGraph {
    fn build_graph(&self, _compressor: &mut Compressor) -> GraphId {
        graphs::FIELD_LZ
    }
}

// Placeholder trampoline for custom graph functions (used in Step 12)
#[allow(dead_code)]
unsafe extern "C" fn graph_fn_trampoline(_compressor: *mut sys::ZL_Compressor) -> sys::ZL_GraphID {
    // This is a placeholder for custom graph registration (Step 12).
    // For now, standard graphs use dedicated callbacks below.
    // Custom graphs will require thread-local storage or user data passing.
    graphs::ZSTD.0
}

/// Compress data using a graph function.
///
/// This is a stateless compression function that uses the provided GraphFn
/// to define the compression strategy.
pub fn compress_with_graph<G: GraphFn>(src: &[u8], graph: &G) -> Result<Vec<u8>, Error> {
    // Allocate output buffer (use compress_bound to estimate size)
    let max_size = compress_bound(src.len());
    let mut dst = vec![0u8; max_size];

    // Create a temporary compressor to get the GraphID
    let mut compressor = Compressor::new();
    let graph_id = graph.build_graph(&mut compressor);

    // Use ZL_compress_usingGraphFn with a C callback that selects the graph
    // The callback will also set required parameters like format version
    let graph_fn = make_graph_selector_fn(graph_id);

    let r = unsafe {
        sys::ZL_compress_usingGraphFn(
            dst.as_mut_ptr() as *mut _,
            dst.len(),
            src.as_ptr() as *const _,
            src.len(),
            graph_fn,
        )
    };

    if sys::report_is_error(r) {
        return Err(report_to_error(r));
    }

    let compressed_size = sys::report_value(r);
    dst.truncate(compressed_size);
    Ok(dst)
}

// Helper to create a C callback that selects a specific GraphID
fn make_graph_selector_fn(graph_id: GraphId) -> sys::ZL_GraphFn {
    // For standard graphs, we can use dedicated callbacks
    match graph_id.0.gid {
        id if id == graphs::ZSTD.0.gid => Some(zstd_graph_callback),
        id if id == graphs::NUMERIC.0.gid => Some(numeric_graph_callback),
        id if id == graphs::STORE.0.gid => Some(store_graph_callback),
        id if id == graphs::FIELD_LZ.0.gid => Some(field_lz_graph_callback),
        _ => {
            // For custom graphs, we'd need a different mechanism
            // For now, fall back to ZSTD
            Some(zstd_graph_callback)
        }
    }
}

// Dedicated C callbacks for standard graphs
unsafe extern "C" fn zstd_graph_callback(compressor: *mut sys::ZL_Compressor) -> sys::ZL_GraphID {
    // Set format version (required by OpenZL)
    sys::ZL_Compressor_setParameter(compressor, sys::ZL_CParam::ZL_CParam_formatVersion, 21);
    graphs::ZSTD.0
}

unsafe extern "C" fn numeric_graph_callback(compressor: *mut sys::ZL_Compressor) -> sys::ZL_GraphID {
    // Set format version (required by OpenZL)
    sys::ZL_Compressor_setParameter(compressor, sys::ZL_CParam::ZL_CParam_formatVersion, 21);
    graphs::NUMERIC.0
}

unsafe extern "C" fn store_graph_callback(compressor: *mut sys::ZL_Compressor) -> sys::ZL_GraphID {
    // Set format version (required by OpenZL)
    sys::ZL_Compressor_setParameter(compressor, sys::ZL_CParam::ZL_CParam_formatVersion, 21);
    graphs::STORE.0
}

unsafe extern "C" fn field_lz_graph_callback(compressor: *mut sys::ZL_Compressor) -> sys::ZL_GraphID {
    // Set format version (required by OpenZL)
    sys::ZL_Compressor_setParameter(compressor, sys::ZL_CParam::ZL_CParam_formatVersion, 21);
    graphs::FIELD_LZ.0
}

// ============================================================================
// TypedRef and TypedBuffer
// ============================================================================

/// Safe wrapper around ZL_TypedRef for typed input data.
///
/// IMPORTANT: TypedRef borrows the input data. The borrowed data must remain
/// valid for the lifetime of the TypedRef and through any compression call
/// that uses it.
pub struct TypedRef<'a> {
    ptr: *mut sys::ZL_TypedRef,
    _marker: std::marker::PhantomData<&'a [u8]>,
}

impl<'a> TypedRef<'a> {
    /// Create a TypedRef for serial (untyped byte array) data
    pub fn serial(data: &'a [u8]) -> Self {
        let ptr = unsafe {
            sys::ZL_TypedRef_createSerial(data.as_ptr() as *const _, data.len())
        };
        assert!(!ptr.is_null(), "ZL_TypedRef_createSerial returned null");
        TypedRef {
            ptr,
            _marker: std::marker::PhantomData,
        }
    }

    /// Create a TypedRef for numeric data.
    ///
    /// T must have size 1, 2, 4, or 8 bytes (u8, u16, u32, u64, i8, i16, i32, i64, f32, f64)
    pub fn numeric<T: Copy>(data: &'a [T]) -> Result<Self, Error> {
        let width = std::mem::size_of::<T>();
        if !matches!(width, 1 | 2 | 4 | 8) {
            return Err(Error::Report {
                code: -1,
                name: "Invalid numeric type".into(),
                context: format!("\nElement size must be 1, 2, 4, or 8 bytes, got {width}"),
            });
        }
        let ptr = unsafe {
            sys::ZL_TypedRef_createNumeric(
                data.as_ptr() as *const _,
                width,
                data.len(),
            )
        };
        assert!(!ptr.is_null(), "ZL_TypedRef_createNumeric returned null");
        Ok(TypedRef {
            ptr,
            _marker: std::marker::PhantomData,
        })
    }

    /// Create a TypedRef for string data (flat format with lengths array)
    ///
    /// - `flat`: concatenated string bytes
    /// - `lens`: array of string lengths (u32)
    pub fn strings(flat: &'a [u8], lens: &'a [u32]) -> Self {
        let ptr = unsafe {
            sys::ZL_TypedRef_createString(
                flat.as_ptr() as *const _,
                flat.len(),
                lens.as_ptr(),
                lens.len(),
            )
        };
        assert!(!ptr.is_null(), "ZL_TypedRef_createString returned null");
        TypedRef {
            ptr,
            _marker: std::marker::PhantomData,
        }
    }

    /// Create a TypedRef for struct data (concatenated fields)
    ///
    /// - `bytes`: flattened struct data
    /// - `width`: size of each struct element in bytes
    /// - `count`: number of struct elements
    pub fn structs(bytes: &'a [u8], width: usize, count: usize) -> Result<Self, Error> {
        if width == 0 || count == 0 {
            return Err(Error::Report {
                code: -1,
                name: "Invalid struct parameters".into(),
                context: "\nWidth and count must be non-zero".into(),
            });
        }
        if bytes.len() != width * count {
            return Err(Error::Report {
                code: -1,
                name: "Invalid struct buffer size".into(),
                context: format!("\nExpected {} bytes (width={width} * count={count}), got {}", width * count, bytes.len()),
            });
        }
        let ptr = unsafe {
            sys::ZL_TypedRef_createStruct(
                bytes.as_ptr() as *const _,
                width,
                count,
            )
        };
        assert!(!ptr.is_null(), "ZL_TypedRef_createStruct returned null");
        Ok(TypedRef {
            ptr,
            _marker: std::marker::PhantomData,
        })
    }

    pub(crate) fn as_ptr(&self) -> *const sys::ZL_TypedRef {
        self.ptr as *const _
    }
}

impl Drop for TypedRef<'_> {
    fn drop(&mut self) {
        unsafe { sys::ZL_TypedRef_free(self.ptr) }
    }
}

/// Safe wrapper around ZL_TypedBuffer for typed decompression output.
///
/// TypedBuffer owns its internal buffer and frees it on Drop.
pub struct TypedBuffer {
    ptr: *mut sys::ZL_TypedBuffer,
}

impl TypedBuffer {
    /// Create a new TypedBuffer for receiving decompressed data
    pub fn new() -> Self {
        let ptr = unsafe { sys::ZL_TypedBuffer_create() };
        assert!(!ptr.is_null(), "ZL_TypedBuffer_create returned null");
        TypedBuffer { ptr }
    }

    /// Get the data type of this buffer
    pub fn data_type(&self) -> sys::ZL_Type {
        unsafe { sys::ZL_TypedBuffer_type(self.ptr) }
    }

    /// Get the size of the buffer in bytes
    pub fn byte_size(&self) -> usize {
        unsafe { sys::ZL_TypedBuffer_byteSize(self.ptr) }
    }

    /// Get the number of elements
    pub fn num_elts(&self) -> usize {
        unsafe { sys::ZL_TypedBuffer_numElts(self.ptr) }
    }

    /// Get the element width in bytes (for struct/numeric types)
    pub fn elt_width(&self) -> usize {
        unsafe { sys::ZL_TypedBuffer_eltWidth(self.ptr) }
    }

    /// Get read-only access to the buffer as bytes
    pub fn as_bytes(&self) -> &[u8] {
        let ptr = unsafe { sys::ZL_TypedBuffer_rPtr(self.ptr) };
        let len = self.byte_size();
        if ptr.is_null() || len == 0 {
            &[]
        } else {
            unsafe { std::slice::from_raw_parts(ptr as *const u8, len) }
        }
    }

    /// Get read-only access to numeric data as a typed slice.
    ///
    /// Returns None if:
    /// - The data type is not numeric
    /// - The element width doesn't match T's size
    /// - The buffer is not properly aligned for T
    pub fn as_numeric<T: Copy>(&self) -> Option<&[T]> {
        // Check if type is numeric
        if self.data_type() != sys::ZL_Type::ZL_Type_numeric {
            return None;
        }

        let width = std::mem::size_of::<T>();
        if self.elt_width() != width {
            return None;
        }

        let ptr = unsafe { sys::ZL_TypedBuffer_rPtr(self.ptr) };
        if ptr.is_null() {
            return None;
        }

        // Check alignment
        if (ptr as usize) % std::mem::align_of::<T>() != 0 {
            return None;
        }

        let len = self.num_elts();
        if len == 0 {
            return Some(&[]);
        }

        Some(unsafe { std::slice::from_raw_parts(ptr as *const T, len) })
    }

    /// Get the string lengths array (for string type)
    ///
    /// Returns None if the data type is not string
    pub fn string_lens(&self) -> Option<&[u32]> {
        if self.data_type() != sys::ZL_Type::ZL_Type_string {
            return None;
        }

        let ptr = unsafe { sys::ZL_TypedBuffer_rStringLens(self.ptr) };
        if ptr.is_null() {
            return None;
        }

        let len = self.num_elts();
        if len == 0 {
            return Some(&[]);
        }

        Some(unsafe { std::slice::from_raw_parts(ptr, len) })
    }

    pub(crate) fn as_mut_ptr(&mut self) -> *mut sys::ZL_TypedBuffer {
        self.ptr
    }
}

impl Drop for TypedBuffer {
    fn drop(&mut self) {
        unsafe { sys::ZL_TypedBuffer_free(self.ptr) }
    }
}

impl Default for TypedBuffer {
    fn default() -> Self {
        Self::new()
    }
}

fn error_from_report_with_ctx(code: i32, name: String, ctx_str: Option<&CStr>) -> Error {
    let context = match ctx_str.and_then(|s| s.to_str().ok()) {
        Some(s) if !s.is_empty() => format!("\n{s}"),
        _ => String::new(),
    };
    Error::Report { code, name, context }
}

pub struct CCtx(*mut sys::ZL_CCtx);
impl CCtx {
    pub fn new() -> Self {
        let ptr = unsafe { sys::ZL_CCtx_create() };
        assert!(!ptr.is_null(), "ZL_CCtx_create returned null");
        CCtx(ptr)
    }
    pub fn set_parameter(&mut self, p: sys::ZL_CParam, v: i32) -> Result<(), Error> {
        let r = unsafe { sys::ZL_CCtx_setParameter(self.0, p, v) };
        if sys::report_is_error(r) {
            let code = sys::report_code(r);
            let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
                .to_string_lossy()
                .into_owned();
            let ctx = unsafe { CStr::from_ptr(sys::openzl_cctx_error_context(self.0, r)) };
            return Err(error_from_report_with_ctx(code, name, Some(&ctx)));
        }
        Ok(())
    }

    /// Reference a Compressor for graph-based typed compression.
    ///
    /// This enables TypedRef compression by associating the CCtx with a Compressor
    /// that has registered compression graphs.
    ///
    /// IMPORTANT: The Compressor must remain valid for the duration of its usage.
    /// The Compressor must be validated before being referenced.
    pub fn ref_compressor(&mut self, compressor: &Compressor) -> Result<(), Error> {
        let r = unsafe { sys::ZL_CCtx_refCompressor(self.0, compressor.as_ptr()) };
        if sys::report_is_error(r) {
            let code = sys::report_code(r);
            let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
                .to_string_lossy()
                .into_owned();
            let ctx = unsafe { CStr::from_ptr(sys::openzl_cctx_error_context(self.0, r)) };
            return Err(error_from_report_with_ctx(code, name, Some(&ctx)));
        }
        Ok(())
    }

    /// Get warnings generated during compression operations
    pub fn warnings(&self) -> Vec<Warning> {
        let arr = unsafe { sys::openzl_cctx_get_warnings(self.0) };
        let slice = unsafe { std::slice::from_raw_parts(arr.errors, arr.size) };
        slice
            .iter()
            .map(|e| {
                let code = unsafe { sys::openzl_error_get_code(e) };
                let name = unsafe { CStr::from_ptr(sys::openzl_error_get_name(e)) }
                    .to_string_lossy()
                    .into_owned();
                Warning { code, name }
            })
            .collect()
    }

    /// Compress a single typed input
    pub fn compress_typed_ref(&mut self, input: &TypedRef, dst: &mut [u8]) -> Result<usize, Error> {
        let r = unsafe {
            sys::ZL_CCtx_compressTypedRef(
                self.0,
                dst.as_mut_ptr() as *mut _,
                dst.len(),
                input.as_ptr(),
            )
        };
        if sys::report_is_error(r) {
            let code = sys::report_code(r);
            let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
                .to_string_lossy()
                .into_owned();
            let ctx = unsafe { CStr::from_ptr(sys::openzl_cctx_error_context(self.0, r)) };
            return Err(error_from_report_with_ctx(code, name, Some(&ctx)));
        }
        Ok(sys::report_value(r))
    }

    /// Compress multiple typed inputs into a single frame
    pub fn compress_multi_typed_ref(&mut self, inputs: &[&TypedRef], dst: &mut [u8]) -> Result<usize, Error> {
        let mut ptrs: Vec<*const sys::ZL_TypedRef> = inputs.iter().map(|tr| tr.as_ptr()).collect();
        let r = unsafe {
            sys::ZL_CCtx_compressMultiTypedRef(
                self.0,
                dst.as_mut_ptr() as *mut _,
                dst.len(),
                ptrs.as_mut_ptr() as *mut _,
                ptrs.len(),
            )
        };
        if sys::report_is_error(r) {
            let code = sys::report_code(r);
            let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
                .to_string_lossy()
                .into_owned();
            let ctx = unsafe { CStr::from_ptr(sys::openzl_cctx_error_context(self.0, r)) };
            return Err(error_from_report_with_ctx(code, name, Some(&ctx)));
        }
        Ok(sys::report_value(r))
    }
}
impl Drop for CCtx { fn drop(&mut self) { unsafe { sys::ZL_CCtx_free(self.0) } } }

pub struct DCtx(*mut sys::ZL_DCtx);
impl DCtx {
    pub fn new() -> Self {
        let p = unsafe { sys::ZL_DCtx_create() };
        assert!(!p.is_null());
        DCtx(p)
    }

    /// Get warnings generated during decompression operations
    pub fn warnings(&self) -> Vec<Warning> {
        let arr = unsafe { sys::openzl_dctx_get_warnings(self.0) };
        let slice = unsafe { std::slice::from_raw_parts(arr.errors, arr.size) };
        slice
            .iter()
            .map(|e| {
                let code = unsafe { sys::openzl_error_get_code(e) };
                let name = unsafe { CStr::from_ptr(sys::openzl_error_get_name(e)) }
                    .to_string_lossy()
                    .into_owned();
                Warning { code, name }
            })
            .collect()
    }

    /// Decompress into a TypedBuffer (auto-sized, single output)
    pub fn decompress_typed_buffer(&mut self, compressed: &[u8], output: &mut TypedBuffer) -> Result<usize, Error> {
        let r = unsafe {
            sys::ZL_DCtx_decompressTBuffer(
                self.0,
                output.as_mut_ptr(),
                compressed.as_ptr() as *const _,
                compressed.len(),
            )
        };
        if sys::report_is_error(r) {
            let code = sys::report_code(r);
            let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
                .to_string_lossy()
                .into_owned();
            let ctx = unsafe { CStr::from_ptr(sys::openzl_dctx_error_context(self.0, r)) };
            return Err(error_from_report_with_ctx(code, name, Some(&ctx)));
        }
        Ok(sys::report_value(r))
    }

    /// Decompress into multiple TypedBuffers (multi-output frame)
    pub fn decompress_multi_typed_buffer(&mut self, compressed: &[u8], outputs: &mut [&mut TypedBuffer]) -> Result<usize, Error> {
        let mut ptrs: Vec<*mut sys::ZL_TypedBuffer> = outputs.iter_mut().map(|tb| tb.as_mut_ptr()).collect();
        let r = unsafe {
            sys::ZL_DCtx_decompressMultiTBuffer(
                self.0,
                ptrs.as_mut_ptr(),
                ptrs.len(),
                compressed.as_ptr() as *const _,
                compressed.len(),
            )
        };
        if sys::report_is_error(r) {
            let code = sys::report_code(r);
            let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
                .to_string_lossy()
                .into_owned();
            let ctx = unsafe { CStr::from_ptr(sys::openzl_dctx_error_context(self.0, r)) };
            return Err(error_from_report_with_ctx(code, name, Some(&ctx)));
        }
        Ok(sys::report_value(r))
    }
}
impl Drop for DCtx { fn drop(&mut self) { unsafe { sys::ZL_DCtx_free(self.0) } } }

pub fn compress_serial(src: &[u8]) -> Result<Vec<u8>, Error> {
    let mut cctx = CCtx::new();
    // Set format version (required by OpenZL). Use latest version (21).
    cctx.set_parameter(sys::ZL_CParam::ZL_CParam_formatVersion, 21)?;
    // Capacity upper bound
    let cap = unsafe { sys::openzl_compress_bound(src.len()) };
    let mut dst = vec![0u8; cap];
    let r = unsafe {
        sys::ZL_CCtx_compress(
            cctx.0,
            dst.as_mut_ptr() as *mut _,
            dst.len(),
            src.as_ptr() as *const _,
            src.len(),
        )
    };
    if sys::report_is_error(r) {
        let code = sys::report_code(r);
        let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
            .to_string_lossy()
            .into_owned();
        let ctx = unsafe { CStr::from_ptr(sys::openzl_cctx_error_context(cctx.0, r)) };
        return Err(error_from_report_with_ctx(code, name, Some(&ctx)));
    }
    let n = sys::report_value(r) as usize;
    dst.truncate(n);
    Ok(dst)
}

/// Compress a single TypedRef and return the compressed bytes.
///
/// This uses ZSTD graph by default. For better compression on specific data types,
/// consider using type-specific compression functions or creating a custom Compressor
/// with appropriate graphs.
pub fn compress_typed_ref(input: &TypedRef) -> Result<Vec<u8>, Error> {
    let mut cctx = CCtx::new();
    // Set format version (required by OpenZL). Use latest version (21).
    cctx.set_parameter(sys::ZL_CParam::ZL_CParam_formatVersion, 21)?;

    // Create a Compressor with ZSTD graph (reasonable default for general use)
    let mut compressor = Compressor::new();
    compressor.set_parameter(sys::ZL_CParam::ZL_CParam_formatVersion, 21)?;

    // Register ZSTD as the starting graph using the graph callback approach
    let r = unsafe {
        sys::ZL_Compressor_initUsingGraphFn(compressor.as_mut_ptr(), Some(zstd_graph_callback))
    };
    if sys::report_is_error(r) {
        return Err(report_to_error(r));
    }

    // Reference the compressor in the CCtx
    cctx.ref_compressor(&compressor)?;

    // Estimate capacity based on input data (this is conservative)
    // For TypedRef we can't easily get the size, so use a large upper bound
    let cap = 1024 * 1024; // 1MB default, adjust as needed
    let mut dst = vec![0u8; cap];

    let n = cctx.compress_typed_ref(input, &mut dst)?;
    dst.truncate(n);
    Ok(dst)
}

/// Compress multiple TypedRefs into a single frame.
///
/// This uses ZSTD graph by default. For better compression on specific data types,
/// consider creating a custom Compressor with appropriate graphs.
pub fn compress_multi_typed_ref(inputs: &[&TypedRef]) -> Result<Vec<u8>, Error> {
    let mut cctx = CCtx::new();
    // Set format version (required by OpenZL). Use latest version (21).
    cctx.set_parameter(sys::ZL_CParam::ZL_CParam_formatVersion, 21)?;

    // Create a Compressor with ZSTD graph
    let mut compressor = Compressor::new();
    compressor.set_parameter(sys::ZL_CParam::ZL_CParam_formatVersion, 21)?;

    let r = unsafe {
        sys::ZL_Compressor_initUsingGraphFn(compressor.as_mut_ptr(), Some(zstd_graph_callback))
    };
    if sys::report_is_error(r) {
        return Err(report_to_error(r));
    }

    cctx.ref_compressor(&compressor)?;

    // Estimate capacity (conservative)
    let cap = 1024 * 1024; // 1MB default
    let mut dst = vec![0u8; cap];

    let n = cctx.compress_multi_typed_ref(inputs, &mut dst)?;
    dst.truncate(n);
    Ok(dst)
}

pub fn decompress_serial(src: &[u8]) -> Result<Vec<u8>, Error> {
    // Query decompressed size first
    let rsize = unsafe { sys::ZL_getDecompressedSize(src.as_ptr() as *const _, src.len()) };
    if sys::report_is_error(rsize) {
        let code = sys::report_code(rsize);
        let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
            .to_string_lossy()
            .into_owned();
        return Err(error_from_report_with_ctx(code, name, None));
    }
    let mut dst = vec![0u8; sys::report_value(rsize) as usize];
    let r = unsafe { sys::ZL_decompress(dst.as_mut_ptr() as *mut _, dst.len(), src.as_ptr() as *const _, src.len()) };
    if sys::report_is_error(r) {
        let code = sys::report_code(r);
        let name = unsafe { CStr::from_ptr(sys::openzl_error_code_to_string(code)) }
            .to_string_lossy()
            .into_owned();
        // No context without DCtx; use empty
        return Err(error_from_report_with_ctx(code, name, None));
    }
    let n = sys::report_value(r) as usize;
    dst.truncate(n);
    Ok(dst)
}

/// Decompress compressed data into a TypedBuffer (auto-allocates and determines type)
pub fn decompress_typed_buffer(compressed: &[u8]) -> Result<TypedBuffer, Error> {
    let mut dctx = DCtx::new();
    let mut output = TypedBuffer::new();
    dctx.decompress_typed_buffer(compressed, &mut output)?;
    Ok(output)
}

// ============================================================================
// High-level ergonomic APIs (Step 9 - MVP completion)
// ============================================================================

/// Compress numeric data using the NUMERIC graph (optimized for numeric arrays).
///
/// Supports all numeric types: u8, u16, u32, u64, i8, i16, i32, i64, f32, f64.
///
/// # Example
/// ```no_run
/// # use rust_openzl::compress_numeric;
/// let data: Vec<u32> = (0..10000).collect();
/// let compressed = compress_numeric(&data).expect("compression failed");
/// ```
pub fn compress_numeric<T: Copy>(data: &[T]) -> Result<Vec<u8>, Error> {
    // Validate that T is a supported numeric type (1, 2, 4, or 8 bytes)
    let width = std::mem::size_of::<T>();
    if !matches!(width, 1 | 2 | 4 | 8) {
        return Err(Error::Report {
            code: -1,
            name: "Invalid numeric type".into(),
            context: format!("\nElement size must be 1, 2, 4, or 8 bytes, got {width}"),
        });
    }

    // Create TypedRef for numeric data
    let tref = TypedRef::numeric(data)?;

    // Create CCtx and Compressor with NUMERIC graph
    let mut cctx = CCtx::new();
    cctx.set_parameter(sys::ZL_CParam::ZL_CParam_formatVersion, 21)?;

    let mut compressor = Compressor::new();
    compressor.set_parameter(sys::ZL_CParam::ZL_CParam_formatVersion, 21)?;

    // Initialize with NUMERIC graph
    let r = unsafe {
        sys::ZL_Compressor_initUsingGraphFn(compressor.as_mut_ptr(), Some(numeric_graph_callback))
    };
    if sys::report_is_error(r) {
        return Err(report_to_error(r));
    }

    cctx.ref_compressor(&compressor)?;

    // Compress
    let cap = compress_bound(data.len() * width);
    let mut dst = vec![0u8; cap];

    let n = cctx.compress_typed_ref(&tref, &mut dst)?;
    dst.truncate(n);
    Ok(dst)
}

/// Decompress numeric data that was compressed with `compress_numeric`.
///
/// Returns a Vec<T> containing the decompressed numeric values.
///
/// # Example
/// ```no_run
/// # use rust_openzl::{compress_numeric, decompress_numeric};
/// let data: Vec<u32> = (0..10000).collect();
/// let compressed = compress_numeric(&data).expect("compression failed");
/// let decompressed: Vec<u32> = decompress_numeric(&compressed).expect("decompression failed");
/// assert_eq!(data, decompressed);
/// ```
pub fn decompress_numeric<T: Copy>(compressed: &[u8]) -> Result<Vec<T>, Error> {
    let width = std::mem::size_of::<T>();
    if !matches!(width, 1 | 2 | 4 | 8) {
        return Err(Error::Report {
            code: -1,
            name: "Invalid numeric type".into(),
            context: format!("\nElement size must be 1, 2, 4, or 8 bytes, got {width}"),
        });
    }

    // Decompress into TypedBuffer
    let tbuf = decompress_typed_buffer(compressed)?;

    // Verify it's numeric type
    if tbuf.data_type() != sys::ZL_Type::ZL_Type_numeric {
        return Err(Error::Report {
            code: -1,
            name: "Type mismatch".into(),
            context: format!("\nExpected numeric type, got {:?}", tbuf.data_type()),
        });
    }

    // Verify element width matches T
    if tbuf.elt_width() != width {
        return Err(Error::Report {
            code: -1,
            name: "Width mismatch".into(),
            context: format!(
                "\nExpected element width {}, got {}",
                width,
                tbuf.elt_width()
            ),
        });
    }

    // Extract numeric data
    let slice = tbuf.as_numeric::<T>().ok_or_else(|| Error::Report {
        code: -1,
        name: "Failed to extract numeric data".into(),
        context: "\nAlignment or type mismatch".into(),
    })?;

    Ok(slice.to_vec())
}