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
//! The single canonical JSON writer.
//!
//! Canonical form is defined in exactly one place — [`crate::fmt`] — and this is
//! the typed door to it: serialize with serde, then put the bytes through the
//! formatter. Full rules and rationale: `docs/reference/compiler.md` §9.
//!
//! **Why it delegates rather than defining its own form**. Before
//! `delvec fmt` existed this function *was* the canonical form, and its form was
//! serde's — struct-declaration field order, whatever `PrettyFormatter` emits.
//! `delvec edit apply` writes `world-edits.json` with it. Had `fmt` shipped with
//! a second, key-sorted form, the compiler would have written a file its own
//! `fmt --check` immediately rejected, and an author who ran both tools in the
//! same loop could not have satisfied both. Two writers claiming "canonical" for
//! one file class is the capability-duplication defect (`CLAUDE.md`,
//! Methodology): the form belongs to the *format*, not to whichever writer
//! needed it first. So there is one definition and this routes through it.
//!
//! The round-trip fixture gate (`tests/roundtrip.rs`) is unchanged and now
//! proves both properties at once: serde loses no field, **and** the fixture on
//! disk is in `delvec fmt` canonical form.
use Serialize;
/// Serialize `value` to canonical JSON: [`crate::fmt`]'s form, with the trailing
/// newline it includes.
///
/// # Errors
/// Serialization failure. The re-format step cannot fail on serde's own output —
/// it is valid JSON with no duplicate keys by construction — so a formatter
/// refusal here is an internal invariant break and panics rather than inventing
/// an error variant callers would have to interpret.