serde_store 0.2.0

A Rust implementation of the Haskell store binary serialization format using Serde
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
# Haskell Interoperability Guide

This guide explains how to use `serde_store` for data exchange between Rust and Haskell programs using the `store` library.

## Quick Start

### Haskell Side

```haskell
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

import Data.Store
import Data.Text (Text)
import qualified Data.ByteString as BS
import GHC.Generics

-- Define your data type
data Person = Person
  { personName :: Text
  , personAge :: Word32
  , personEmail :: Maybe Text
  } deriving (Generic, Show, Eq)

-- Derive Store instance automatically
instance Store Person

-- Encode data to binary
main :: IO ()
main = do
  let person = Person "Alice" 30 (Just "alice@example.com")
  let encoded = encode person
  BS.writeFile "person.bin" encoded
  putStrLn "Encoded person to person.bin"
```

### Rust Side

```rust
use serde::{Serialize, Deserialize};
use serde_store::from_bytes;
use std::fs;

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Person {
    person_name: String,
    person_age: u32,
    person_email: Option<String>,
}

fn main() {
    // Read the binary file created by Haskell
    let bytes = fs::read("person.bin").expect("Failed to read file");
    
    // Deserialize
    let person: Person = from_bytes(&bytes).expect("Failed to deserialize");
    
    println!("Decoded person: {:?}", person);
    // Output: Person { person_name: "Alice", person_age: 30, person_email: Some("alice@example.com") }
}
```

## Type Correspondence

### Basic Types

| Haskell | Rust | Notes |
|---------|------|-------|
| `Bool` | `bool` | |
| `Word8` | `u8` | |
| `Word16` | `u16` | |
| `Word32` | `u32` | |
| `Word64` | `u64` | |
| `Int8` | `i8` | |
| `Int16` | `i16` | |
| `Int32` | `i32` | |
| `Int64` | `i64` | |
| `Float` | `f32` | |
| `Double` | `f64` | |
| `Char` | `char` | UTF-8 encoded |
| `Text` | `String` | Requires text >= 2.0 (UTF-8) |
| `ByteString` | `Vec<u8>` | |

### Container Types

| Haskell | Rust | Notes |
|---------|------|-------|
| `Maybe a` | `Option<T>` | |
| `[a]` | `Vec<T>` | Lists → Vectors |
| `Vector a` | `Vec<T>` | |
| `Seq a` | `Vec<T>` | |
| `Map k v` | `BTreeMap<K, V>` | Ordered |
| `HashMap k v` | `HashMap<K, V>` | Unordered |
| `Set a` | `BTreeSet<T>` | Ordered |
| `HashSet a` | `HashSet<T>` | Unordered |
| `IntMap v` | `HashMap<i32, V>` | Or BTreeMap |
| `IntSet` | `HashSet<i32>` | Or BTreeSet |

### Composite Types

| Haskell | Rust | Example |
|---------|------|---------|
| `(a, b)` | `(A, B)` | Tuples |
| `(a, b, c)` | `(A, B, C)` | |
| Record | Struct | See below |
| ADT | Enum | See below |

## Field Naming Conventions

**Important:** Haskell and Rust have different naming conventions. You need to ensure field names match exactly in the binary format.

### Haskell (camelCase)
```haskell
data Person = Person
  { personName :: Text
  , personAge :: Word32
  } deriving (Generic)
```

### Rust (snake_case with field renaming)
```rust
#[derive(Serialize, Deserialize)]
struct Person {
    #[serde(rename = "personName")]
    person_name: String,
    #[serde(rename = "personAge")]
    person_age: u32,
}
```

**OR** use Rust field names that match Haskell:

```rust
#[derive(Serialize, Deserialize)]
struct Person {
    personName: String,  // Violates Rust conventions but matches Haskell
    personAge: u32,
}
```

**OR** configure Haskell to use snake_case (not recommended):

```haskell
{-# LANGUAGE DeriveAnyClass #-}
data Person = Person
  { person_name :: Text
  , person_age :: Word32
  } deriving (Generic, Store)
```

## Enums and ADTs

### Simple Enum

**Haskell:**
```haskell
data Status = Active | Inactive | Pending
  deriving (Generic, Show)

instance Store Status
```

**Rust:**
```rust
#[derive(Serialize, Deserialize, Debug)]
enum Status {
    Active,    // variant index 0
    Inactive,  // variant index 1
    Pending,   // variant index 2
}
```

### Enum with Data

**Haskell:**
```haskell
data Result = Success Text | Error Word32 Text
  deriving (Generic)

instance Store Result
```

**Rust:**
```rust
#[derive(Serialize, Deserialize)]
enum Result {
    Success(String),           // variant 0
    Error(u32, String),        // variant 1
}
```

### Enum with Named Fields

**Haskell:**
```haskell
data Event = 
    Click { x :: Int32, y :: Int32 }
  | KeyPress { key :: Text }
  deriving (Generic)

instance Store Event
```

**Rust:**
```rust
#[derive(Serialize, Deserialize)]
enum Event {
    Click { x: i32, y: i32 },
    KeyPress { key: String },
}
```

## Complete Example: Bidirectional Communication

### Shared Data Schema

**Haskell:** `Types.hs`
```haskell
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

module Types where

import Data.Store
import Data.Text (Text)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Word
import GHC.Generics

data User = User
  { userId :: Word64
  , userName :: Text
  , userEmail :: Maybe Text
  , userTags :: [Text]
  } deriving (Generic, Show, Eq)

instance Store User

data Message = Message
  { msgId :: Word64
  , msgFrom :: User
  , msgTo :: [User]
  , msgContent :: Text
  , msgMetadata :: Map Text Text
  } deriving (Generic, Show, Eq)

instance Store Message
```

**Rust:** `types.rs`
```rust
use serde::{Serialize, Deserialize};
use std::collections::BTreeMap;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct User {
    #[serde(rename = "userId")]
    pub user_id: u64,
    #[serde(rename = "userName")]
    pub user_name: String,
    #[serde(rename = "userEmail")]
    pub user_email: Option<String>,
    #[serde(rename = "userTags")]
    pub user_tags: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Message {
    #[serde(rename = "msgId")]
    pub msg_id: u64,
    #[serde(rename = "msgFrom")]
    pub msg_from: User,
    #[serde(rename = "msgTo")]
    pub msg_to: Vec<User>,
    #[serde(rename = "msgContent")]
    pub msg_content: String,
    #[serde(rename = "msgMetadata")]
    pub msg_metadata: BTreeMap<String, String>,
}
```

### Haskell: Encode and Send

```haskell
import qualified Data.ByteString as BS
import Types

sendMessage :: IO ()
sendMessage = do
  let alice = User 1 "Alice" (Just "alice@example.com") ["admin", "user"]
  let bob = User 2 "Bob" Nothing ["user"]
  
  let msg = Message
        { msgId = 1001
        , msgFrom = alice
        , msgTo = [bob]
        , msgContent = "Hello from Haskell!"
        , msgMetadata = Map.fromList [("priority", "high")]
        }
  
  let encoded = encode msg
  BS.writeFile "message.bin" encoded
  putStrLn "Message sent to message.bin"
```

### Rust: Receive and Decode

```rust
use serde_store::from_bytes;
use std::fs;

mod types;
use types::Message;

fn main() {
    let bytes = fs::read("message.bin").expect("Failed to read message");
    let msg: Message = from_bytes(&bytes).expect("Failed to decode");
    
    println!("Received message #{}", msg.msg_id);
    println!("From: {} (ID: {})", msg.msg_from.user_name, msg.msg_from.user_id);
    println!("To: {} recipient(s)", msg.msg_to.len());
    println!("Content: {}", msg.msg_content);
    println!("Metadata: {:?}", msg.msg_metadata);
}
```

## Common Pitfalls

### 1. Text Encoding Version Mismatch

**Problem:** Haskell `text < 2.0` uses UTF-16, `text >= 2.0` uses UTF-8

**Solution:** Ensure you're using `text >= 2.0` in Haskell, or handle the conversion manually.

```haskell
-- Check your text version
-- In cabal file or stack.yaml, ensure:
-- text >= 2.0
```

### 2. Field Order Matters

**Problem:** Fields must be in the same order

**Haskell:**
```haskell
data Point = Point { x :: Int32, y :: Int32 }
```

**Rust:** ✅ Correct
```rust
struct Point { x: i32, y: i32 }
```

**Rust:** ❌ Wrong (different order)
```rust
struct Point { y: i32, x: i32 }
```

### 3. Enum Variant Order

**Problem:** Variant indices must match

**Haskell:**
```haskell
data Color = Red | Green | Blue  -- Red=0, Green=1, Blue=2
```

**Rust:** ✅ Correct
```rust
enum Color {
    Red,    // 0
    Green,  // 1
    Blue,   // 2
}
```

**Rust:** ❌ Wrong
```rust
enum Color {
    Blue,   // 0 - doesn't match!
    Red,    // 1
    Green,  // 2
}
```

### 4. Collection Type Mismatch

**Problem:** Using unordered HashMap when Haskell uses ordered Map

**Haskell:**
```haskell
import Data.Map (Map)  -- Ordered

data Config = Config { settings :: Map Text Int }
```

**Rust:** ✅ Use BTreeMap (ordered)
```rust
use std::collections::BTreeMap;

struct Config {
    settings: BTreeMap<String, i32>,
}
```

**Rust:** ⚠️ HashMap works but order differs
```rust
use std::collections::HashMap;

struct Config {
    settings: HashMap<String, i32>,  // May serialize differently
}
```

## Testing Interoperability

### Create Test Files in Both Languages

**Haskell:** Generate test data
```haskell
-- generate_test_data.hs
import Data.Store
import qualified Data.ByteString as BS

main = do
  -- Write various test cases
  BS.writeFile "test_int.bin" (encode (42 :: Int32))
  BS.writeFile "test_string.bin" (encode ("Hello" :: Text))
  BS.writeFile "test_list.bin" (encode ([1,2,3,4,5] :: [Int32]))
```

**Rust:** Verify decoding
```rust
// verify_test_data.rs
use serde_store::from_bytes;
use std::fs;

fn main() {
    let int_val: i32 = from_bytes(&fs::read("test_int.bin").unwrap()).unwrap();
    assert_eq!(int_val, 42);
    
    let str_val: String = from_bytes(&fs::read("test_string.bin").unwrap()).unwrap();
    assert_eq!(str_val, "Hello");
    
    let list_val: Vec<i32> = from_bytes(&fs::read("test_list.bin").unwrap()).unwrap();
    assert_eq!(list_val, vec![1, 2, 3, 4, 5]);
    
    println!("All tests passed!");
}
```

## Performance Considerations

1. **Binary Size:** Store format is compact but not as small as specialized formats
2. **Speed:** Very fast - direct memory operations for primitives
3. **Memory:** Single buffer allocation for serialization
4. **Zero-copy:** Not yet optimized (future improvement)

## Debugging Tips

### Inspect Binary Format

**Rust:**
```rust
let bytes = to_bytes(&value).unwrap();
println!("Encoded to {} bytes: {:02x?}", bytes.len(), bytes);
```

**Haskell:**
```haskell
import qualified Data.ByteString as BS
import Text.Printf (printf)

let encoded = encode value
BS.putStr $ BS.concatMap (BS.pack . printf "%02x ") encoded
```

### Validate Roundtrip

**Test in same language first:**

```rust
let original = MyStruct { ... };
let bytes = to_bytes(&original).unwrap();
let decoded: MyStruct = from_bytes(&bytes).unwrap();
assert_eq!(original, decoded);
```

```haskell
let original = MyStruct { ... }
let decoded = decode (encode original) :: Either PeekException MyStruct
decoded `shouldBe` Right original
```

## Version Compatibility

- **Haskell store:** >= 0.7.0 recommended
- **Rust serde_store:** 0.1.0
- **Haskell text:** >= 2.0 required for UTF-8
- **Endianness:** Little-endian only (x86, ARM modern)

## Further Resources

- [Haskell store library]https://github.com/mgsloan/store
- [Serde documentation]https://serde.rs/
- [Store format specification]https://hackage.haskell.org/package/store

## Getting Help

If data doesn't deserialize correctly:

1. Check field names and order
2. Verify type mappings
3. Confirm text package version (>= 2.0)
4. Test with simple types first
5. Use binary inspection to debug