jasn 0.2.0

A Rust library for parsing and formatting JASN (Just Another Serialization Notation)
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
//! Format a [`Value`] into JASN text.
//!
//! The main entry points are [`format()`] and [`format_pretty()`] for common use cases.
//! For custom formatting options, use [`format_with_opts()`] with [`Options`].
//!
//! ```
//! use jasn::{Value, format};
//!
//! let value = Value::String("hello".to_string());
//! assert_eq!(format(&value), r#""hello""#);
//!
//! // Custom formatting with advanced options
//! use jasn::formatter::{Options, format_with_opts};
//! let opts = Options::pretty().with_indent("\t");
//! let formatted = format_with_opts(&value, &opts);
//! ```

use std::collections::BTreeMap;

use time::{format_description, macros::format_description as fd};

use crate::{Binary, Value};

/// Formatting options and configuration.
mod options;
pub use options::{BinaryEncoding, Options, QuoteStyle, TimestampPrecision};

/// Formats a JASN [`Value`] into a compact string (no unnecessary whitespace).
pub fn format(value: &Value) -> String {
    format_impl(value, &Options::compact(), 0)
}

/// Formats a JASN [`Value`] into a pretty-printed string with indentation and newlines.
pub fn format_pretty(value: &Value) -> String {
    format_impl(value, &Options::pretty(), 0)
}

/// Formats a JASN [`Value`] with custom formatting options.
pub fn format_with_opts(value: &Value, opts: &Options) -> String {
    format_impl(value, opts, 0)
}

fn format_impl(value: &Value, opts: &Options, depth: usize) -> String {
    match value {
        Value::Null => "null".to_string(),
        Value::Bool(b) => b.to_string(),
        Value::Int(i) => format_int(*i, opts),
        Value::Float(f) => format_float(*f, opts),
        Value::String(s) => {
            let quote = match opts.quote_style {
                QuoteStyle::Double => '"',
                QuoteStyle::Single => '\'',
                QuoteStyle::PreferDouble => {
                    if s.contains('"') && !s.contains('\'') {
                        '\''
                    } else {
                        '"'
                    }
                }
            };
            format_string(s, quote, opts.escape_unicode)
        }
        Value::Binary(b) => format_binary(b, opts.binary_encoding),
        Value::Timestamp(t) => format_timestamp(t, opts),
        Value::List(items) => {
            if opts.indent.is_empty() {
                format_list_compact(items, opts)
            } else {
                format_list_pretty(items, opts, depth)
            }
        }
        Value::Map(map) => {
            if opts.indent.is_empty() {
                format_map_compact(map, opts)
            } else {
                format_map_pretty(map, opts, depth)
            }
        }
    }
}

fn format_int(i: i64, opts: &Options) -> String {
    if opts.leading_plus && i >= 0 {
        format!("+{}", i)
    } else {
        i.to_string()
    }
}

fn format_float(f: f64, opts: &Options) -> String {
    let base_string = if f.is_infinite() {
        if f.is_sign_negative() {
            "-inf".to_string()
        } else {
            "inf".to_string()
        }
    } else if f.is_nan() {
        "nan".to_string()
    } else if f.fract() == 0.0 && f.abs() < 1e15 {
        // Ensure we always have a decimal point to distinguish from integers
        format!("{:.1}", f)
    } else {
        f.to_string()
    };

    // Add leading plus for positive numbers (including +inf, but not nan)
    if opts.leading_plus && !f.is_nan() && !base_string.starts_with('-') {
        format!("+{}", base_string)
    } else {
        base_string
    }
}

const TIMESTAMP_FORMAT_SECONDS: &[format_description::FormatItem<'static>] = fd!(
    "[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]"
);

const TIMESTAMP_FORMAT_MILLIS: &[format_description::FormatItem<'static>] = fd!(
    "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3][offset_hour sign:mandatory]:[offset_minute]"
);

const TIMESTAMP_FORMAT_MICROS: &[format_description::FormatItem<'static>] = fd!(
    "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:6][offset_hour sign:mandatory]:[offset_minute]"
);

const TIMESTAMP_FORMAT_NANOS: &[format_description::FormatItem<'static>] = fd!(
    "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:9][offset_hour sign:mandatory]:[offset_minute]"
);

fn format_timestamp(t: &crate::Timestamp, opts: &Options) -> String {
    // Select format descriptor based on precision
    let format: &[format_description::FormatItem<'_>] = match opts.timestamp_precision {
        TimestampPrecision::Auto => {
            // Use RFC3339 which includes fractional seconds when present
            let formatted = t
                .format(&time::format_description::well_known::Rfc3339)
                .unwrap_or_else(|_| t.to_string());

            // RFC3339 uses Z for UTC, convert to +00:00 if needed
            let final_str = if !opts.use_zulu && formatted.ends_with('Z') {
                let mut s = formatted;
                s.pop();
                s.push_str("+00:00");
                s
            } else {
                formatted
            };
            return format!("ts\"{}\"", final_str);
        }
        TimestampPrecision::Seconds => TIMESTAMP_FORMAT_SECONDS,
        TimestampPrecision::Milliseconds => TIMESTAMP_FORMAT_MILLIS,
        TimestampPrecision::Microseconds => TIMESTAMP_FORMAT_MICROS,
        TimestampPrecision::Nanoseconds => TIMESTAMP_FORMAT_NANOS,
    };

    // Custom formats output +00:00, convert to Z if needed
    let formatted = t.format(format).unwrap_or_else(|_| t.to_string());
    let final_str = if opts.use_zulu && formatted.ends_with("+00:00") {
        let mut s = formatted;
        s.truncate(s.len() - 6);
        s.push('Z');
        s
    } else {
        formatted
    };

    format!("ts\"{}\"", final_str)
}

fn format_string(s: &str, quote: char, escape_unicode: bool) -> String {
    let mut result = String::with_capacity(s.len() + 2);
    result.push(quote);

    for ch in s.chars() {
        match ch {
            '"' if quote == '"' => result.push_str("\\\""),
            '\'' if quote == '\'' => result.push_str("\\'"),
            '\\' => result.push_str("\\\\"),
            '/' => result.push_str("\\/"),
            '\n' => result.push_str("\\n"),
            '\t' => result.push_str("\\t"),
            '\r' => result.push_str("\\r"),
            '\x08' => result.push_str("\\b"),
            '\x0C' => result.push_str("\\f"),
            c if c.is_control() => {
                use std::fmt::Write;
                write!(&mut result, "\\u{:04x}", c as u32).unwrap();
            }
            c if escape_unicode && !c.is_ascii() => {
                use std::fmt::Write;
                let code = c as u32;
                if code <= 0xFFFF {
                    // BMP character - single escape sequence
                    write!(&mut result, "\\u{:04x}", code).unwrap();
                } else {
                    // Non-BMP character - use UTF-16 surrogate pair
                    let adjusted = code - 0x10000;
                    let high = 0xD800 + (adjusted >> 10);
                    let low = 0xDC00 + (adjusted & 0x3FF);
                    write!(&mut result, "\\u{:04x}\\u{:04x}", high, low).unwrap();
                }
            }
            c => result.push(c),
        }
    }

    result.push(quote);
    result
}

fn format_binary(binary: &Binary, encoding: BinaryEncoding) -> String {
    match encoding {
        BinaryEncoding::Base64 => {
            use base64::{Engine as _, engine::general_purpose};
            let encoded = general_purpose::STANDARD.encode(&binary.0);
            format!("b64\"{}\"", encoded)
        }
        BinaryEncoding::Hex => {
            let hex: String = binary.0.iter().map(|b| format!("{:02x}", b)).collect();
            format!("hex\"{}\"", hex)
        }
    }
}

fn format_list_compact(items: &[Value], opts: &Options) -> String {
    if items.is_empty() {
        return "[]".to_string();
    }

    let formatted: Vec<String> = items
        .iter()
        .map(|item| format_impl(item, opts, 0))
        .collect();
    format!("[{}]", formatted.join(","))
}

fn format_list_pretty(items: &[Value], opts: &Options, depth: usize) -> String {
    if items.is_empty() {
        return "[]".to_string();
    }

    let indent = opts.indent.repeat(depth);
    let item_indent = opts.indent.repeat(depth + 1);
    let mut result = String::from("[\n");

    for (i, item) in items.iter().enumerate() {
        result.push_str(&item_indent);
        result.push_str(&format_impl(item, opts, depth + 1));
        if i < items.len() - 1 || opts.trailing_commas {
            result.push(',');
        }
        result.push('\n');
    }

    result.push_str(&indent);
    result.push(']');
    result
}

fn format_map_compact(map: &BTreeMap<String, Value>, opts: &Options) -> String {
    if map.is_empty() {
        return "{}".to_string();
    }

    let entries: Vec<_> = if opts.sort_keys {
        let mut sorted: Vec<_> = map.iter().collect();
        sorted.sort_by_key(|(k, _)| *k);
        sorted
    } else {
        map.iter().collect()
    };

    let formatted: Vec<String> = entries
        .iter()
        .map(|(k, v)| {
            let key_str = if opts.unquoted_keys && can_be_unquoted(k) {
                k.to_string()
            } else {
                let quote = match opts.quote_style {
                    QuoteStyle::Double => '"',
                    QuoteStyle::Single => '\'',
                    QuoteStyle::PreferDouble => {
                        if k.contains('"') && !k.contains('\'') {
                            '\''
                        } else {
                            '"'
                        }
                    }
                };
                format_string(k, quote, opts.escape_unicode)
            };
            format!("{}:{}", key_str, format_impl(v, opts, 0))
        })
        .collect();
    format!("{{{}}}", formatted.join(","))
}

fn format_map_pretty(map: &BTreeMap<String, Value>, opts: &Options, depth: usize) -> String {
    if map.is_empty() {
        return "{}".to_string();
    }

    let indent = opts.indent.repeat(depth);
    let item_indent = opts.indent.repeat(depth + 1);
    let mut result = String::from("{\n");

    let entries: Vec<_> = if opts.sort_keys {
        let mut sorted: Vec<_> = map.iter().collect();
        sorted.sort_by_key(|(k, _)| *k);
        sorted
    } else {
        map.iter().collect()
    };
    for (i, (key, value)) in entries.iter().enumerate() {
        result.push_str(&item_indent);

        // Format key (possibly unquoted)
        if opts.unquoted_keys && can_be_unquoted(key) {
            result.push_str(key);
        } else {
            let quote = match opts.quote_style {
                QuoteStyle::Double => '"',
                QuoteStyle::Single => '\'',
                QuoteStyle::PreferDouble => {
                    if key.contains('"') && !key.contains('\'') {
                        '\''
                    } else {
                        '"'
                    }
                }
            };
            result.push_str(&format_string(key, quote, opts.escape_unicode));
        }

        result.push_str(": ");
        result.push_str(&format_impl(value, opts, depth + 1));

        if i < entries.len() - 1 || opts.trailing_commas {
            result.push(',');
        }
        result.push('\n');
    }

    result.push_str(&indent);
    result.push('}');
    result
}

fn can_be_unquoted(key: &str) -> bool {
    if key.is_empty() {
        return false;
    }

    // Reserved keywords cannot be unquoted
    if matches!(key, "null" | "true" | "false" | "inf" | "nan") {
        return false;
    }

    let mut chars = key.chars();
    let first = chars.next().unwrap();

    // Must start with letter or underscore
    if !first.is_ascii_alphabetic() && first != '_' {
        return false;
    }

    // Rest must be alphanumeric or underscore
    chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;
    use crate::parse;

    #[rstest]
    #[case(Value::Null, "null")]
    #[case(Value::Bool(true), "true")]
    #[case(Value::Bool(false), "false")]
    #[case(Value::Int(42), "42")]
    #[case(Value::Int(-123), "-123")]
    fn test_format_primitives(#[case] value: Value, #[case] expected: &str) {
        assert_eq!(format(&value), expected);
    }

    #[rstest]
    #[case(3.0, "3.0")]
    #[case(2.5, "2.5")]
    #[case(f64::INFINITY, "inf")]
    #[case(f64::NEG_INFINITY, "-inf")]
    fn test_format_float(#[case] value: f64, #[case] expected: &str) {
        assert_eq!(format(&Value::Float(value)), expected);
    }

    #[test]
    fn test_format_float_nan() {
        assert!(format(&Value::Float(f64::NAN)).contains("nan"));
    }

    #[test]
    fn test_format_string() {
        assert_eq!(format(&Value::String("hello".to_string())), "\"hello\"");
        assert_eq!(
            format(&Value::String("hello\nworld".to_string())),
            "\"hello\\nworld\""
        );
        assert_eq!(
            format(&Value::String("tab\there".to_string())),
            "\"tab\\there\""
        );
    }

    #[test]
    fn test_format_binary() {
        let binary = Binary(vec![72, 101, 108, 108, 111]); // "Hello"
        assert_eq!(format(&Value::Binary(binary)), "b64\"SGVsbG8=\"");
    }

    #[test]
    fn test_format_list() {
        let list = vec![Value::Int(1), Value::Int(2), Value::Int(3)];
        assert_eq!(format(&Value::List(list)), "[1,2,3]");

        assert_eq!(format(&Value::List(vec![])), "[]");
    }

    #[test]
    fn test_format_map() {
        let mut map = BTreeMap::new();
        map.insert("name".to_string(), Value::String("Alice".to_string()));
        map.insert("age".to_string(), Value::Int(30));

        let formatted = format(&Value::Map(map));
        // Compact format uses unquoted keys to save bytes
        assert!(formatted.contains("age:30"));
        assert!(formatted.contains("name:\"Alice\""));
    }

    #[test]
    fn test_round_trip() {
        // Null
        let null = Value::Null;
        assert_eq!(parse(&format(&null)).unwrap(), null);

        // Bool
        let bool_val = Value::Bool(true);
        assert_eq!(parse(&format(&bool_val)).unwrap(), bool_val);

        // Int
        let int_val = Value::Int(42);
        assert_eq!(parse(&format(&int_val)).unwrap(), int_val);

        // Float
        let float_val = Value::Float(2.5);
        assert_eq!(parse(&format(&float_val)).unwrap(), float_val);

        // String
        let string_val = Value::String("hello world".to_string());
        assert_eq!(parse(&format(&string_val)).unwrap(), string_val);

        // List
        let list_val = Value::List(vec![Value::Int(1), Value::Int(2)]);
        assert_eq!(parse(&format(&list_val)).unwrap(), list_val);

        // Map
        let mut map = BTreeMap::new();
        map.insert("key".to_string(), Value::Int(42));
        let map_val = Value::Map(map);
        assert_eq!(parse(&format(&map_val)).unwrap(), map_val);
    }

    #[test]
    fn test_pretty_format() {
        let mut map = BTreeMap::new();
        map.insert("name".to_string(), Value::String("Alice".to_string()));
        map.insert("age".to_string(), Value::Int(30));

        let pretty = format_pretty(&Value::Map(map));
        assert!(pretty.contains('\n'));
        assert!(pretty.contains("  "));
    }

    #[rstest]
    #[case("hello", true)]
    #[case("_private", true)]
    #[case("key123", true)]
    #[case("_", true)]
    #[case("", false)]
    #[case("123", false)]
    #[case("null", false)]
    #[case("true", false)]
    #[case("false", false)]
    #[case("kebab-case", false)]
    fn test_can_be_unquoted(#[case] input: &str, #[case] expected: bool) {
        assert_eq!(can_be_unquoted(input), expected);
    }

    #[rstest]
    #[case(Value::Int(42), "+42")]
    #[case(Value::Int(0), "+0")]
    #[case(Value::Int(-42), "-42")]
    #[case(Value::Float(2.5), "+2.5")]
    #[case(Value::Float(-2.5), "-2.5")]
    #[case(Value::Float(f64::INFINITY), "+inf")]
    #[case(Value::Float(f64::NEG_INFINITY), "-inf")]
    #[case(Value::Float(f64::NAN), "nan")]
    fn test_leading_plus(#[case] value: Value, #[case] expected: &str) {
        let opts = Options::compact().with_leading_plus(true);
        assert_eq!(format_with_opts(&value, &opts), expected);
    }

    #[rstest]
    #[case(Value::Int(42), "42")]
    #[case(Value::Float(2.5), "2.5")]
    fn test_no_leading_plus(#[case] value: Value, #[case] expected: &str) {
        let opts = Options::compact();
        assert_eq!(format_with_opts(&value, &opts), expected);
    }

    #[test]
    fn test_sort_keys() {
        let mut map = BTreeMap::new();
        map.insert("zebra".to_string(), Value::Int(1));
        map.insert("apple".to_string(), Value::Int(2));
        map.insert("banana".to_string(), Value::Int(3));

        // With sort_keys enabled
        let sorted_opts = Options::compact().with_sort_keys(true);
        let sorted = format_with_opts(&Value::Map(map), &sorted_opts);

        // Should be alphabetically ordered
        assert_eq!(sorted, "{apple:2,banana:3,zebra:1}");

        // Pretty mode with sort_keys
        let pretty_sorted = Options::pretty().with_sort_keys(true);
        let mut map2 = BTreeMap::new();
        map2.insert("z".to_string(), Value::Int(1));
        map2.insert("a".to_string(), Value::Int(2));
        let result = format_with_opts(&Value::Map(map2), &pretty_sorted);
        assert!(result.find("a").unwrap() < result.find("z").unwrap());
    }

    #[test]
    fn test_escape_unicode() {
        let opts = Options::compact().with_escape_unicode(true);

        // ASCII characters should not be escaped
        let ascii = Value::String("hello".to_string());
        assert_eq!(format_with_opts(&ascii, &opts), "\"hello\"");

        // Non-ASCII characters should be escaped
        let unicode = Value::String("café".to_string());
        assert_eq!(format_with_opts(&unicode, &opts), "\"caf\\u00e9\"");

        // Emoji should be escaped using UTF-16 surrogate pairs (U+1F30D => D83C DF0D)
        let emoji = Value::String("Hello 🌍".to_string());
        assert_eq!(format_with_opts(&emoji, &opts), "\"Hello \\ud83c\\udf0d\"");

        // Chinese characters
        let chinese = Value::String("你好".to_string());
        assert_eq!(format_with_opts(&chinese, &opts), "\"\\u4f60\\u597d\"");

        // Without escape_unicode should keep Unicode literal
        let no_escape = Options::compact().with_escape_unicode(false);
        let result = format_with_opts(&unicode, &no_escape);
        assert_eq!(result, "\"café\"");
    }

    #[rstest]
    #[case("😀", "\"\\ud83d\\ude00\"")]
    #[case("👍", "\"\\ud83d\\udc4d\"")]
    #[case("𝄞", "\"\\ud834\\udd1e\"")]
    #[case("😀😁😂", "\"\\ud83d\\ude00\\ud83d\\ude01\\ud83d\\ude02\"")]
    #[case("Hello 😀 World", "\"Hello \\ud83d\\ude00 World\"")]
    #[case("中文", "\"\\u4e2d\\u6587\"")]
    fn test_surrogate_pair_encoding(#[case] input: &str, #[case] expected: &str) {
        let opts = Options::compact().with_escape_unicode(true);
        let value = Value::String(input.to_string());
        assert_eq!(format_with_opts(&value, &opts), expected);
    }

    #[rstest]
    #[case("😀")]
    #[case("🌍")]
    #[case("👍")]
    #[case("𝄞")]
    #[case("Hello 😀 World")]
    #[case("😀😁😂")]
    fn test_surrogate_pair_round_trip(#[case] original: &str) {
        let opts = Options::compact().with_escape_unicode(true);
        let value = Value::String(original.to_string());
        let formatted = format_with_opts(&value, &opts);
        let parsed = crate::parse(&formatted).expect("Failed to parse");

        if let Value::String(s) = parsed {
            assert_eq!(s, original, "Round-trip failed for: {}", original);
        } else {
            panic!("Expected String value");
        }
    }

    #[test]
    fn test_format_timestamp_default() {
        use crate::Timestamp;

        let ts = Timestamp::from_unix_timestamp(1234567890).unwrap();
        let value = Value::Timestamp(ts);

        // Default (use_zulu = true) - should use Z notation
        let result = format(&value);
        assert_eq!(result, "ts\"2009-02-13T23:31:30Z\"");
    }

    #[rstest]
    #[case(true, "ts\"2009-02-13T23:31:30Z\"")]
    #[case(false, "ts\"2009-02-13T23:31:30+00:00\"")]
    fn test_format_timestamp_zulu(#[case] use_zulu: bool, #[case] expected: &str) {
        use crate::Timestamp;

        let ts = Timestamp::from_unix_timestamp(1234567890).unwrap();
        let value = Value::Timestamp(ts);
        let opts = Options::compact().with_use_zulu(use_zulu);
        let result = format_with_opts(&value, &opts);
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(true, "ts\"2009-02-13T23:31:30.123456789Z\"")]
    #[case(false, "ts\"2009-02-13T23:31:30.123456789+00:00\"")]
    fn test_format_timestamp_fractional_zulu(#[case] use_zulu: bool, #[case] expected: &str) {
        use crate::Timestamp;

        let ts = Timestamp::from_unix_timestamp_nanos(1234567890123456789).unwrap();
        let value = Value::Timestamp(ts);
        let opts = Options::compact().with_use_zulu(use_zulu);
        let result = format_with_opts(&value, &opts);
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(TimestampPrecision::Auto, "ts\"2009-02-13T23:31:30.123456789Z\"")]
    #[case(TimestampPrecision::Seconds, "ts\"2009-02-13T23:31:30Z\"")]
    #[case(TimestampPrecision::Milliseconds, "ts\"2009-02-13T23:31:30.123Z\"")]
    #[case(TimestampPrecision::Microseconds, "ts\"2009-02-13T23:31:30.123456Z\"")]
    #[case(
        TimestampPrecision::Nanoseconds,
        "ts\"2009-02-13T23:31:30.123456789Z\""
    )]
    fn test_format_timestamp_precision(
        #[case] precision: TimestampPrecision,
        #[case] expected: &str,
    ) {
        use crate::Timestamp;

        let ts = Timestamp::from_unix_timestamp_nanos(1234567890123456789).unwrap();
        let value = Value::Timestamp(ts);
        let opts = Options::compact().with_timestamp_precision(precision);
        let result = format_with_opts(&value, &opts);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_format_timestamp_precision_with_offset() {
        use crate::Timestamp;

        let ts = Timestamp::from_unix_timestamp_nanos(1234567890123456789).unwrap();
        let value = Value::Timestamp(ts);
        let opts = Options::compact()
            .with_timestamp_precision(TimestampPrecision::Milliseconds)
            .with_use_zulu(false);
        let result = format_with_opts(&value, &opts);
        assert_eq!(result, "ts\"2009-02-13T23:31:30.123+00:00\"");
    }

    #[test]
    fn test_format_timestamp_precision_padding() {
        use crate::Timestamp;

        // Test precision padding (timestamp without fractional seconds)
        // When formatted with higher precision, should add zeros
        let ts = Timestamp::from_unix_timestamp(1234567890).unwrap();
        let value = Value::Timestamp(ts);
        let opts = Options::compact().with_timestamp_precision(TimestampPrecision::Milliseconds);
        let result = format_with_opts(&value, &opts);
        assert_eq!(result, "ts\"2009-02-13T23:31:30.000Z\"");
    }
}